Android float button over scroll view - android-layout

I have to put a button at a fix position(top-right) so that when I scroll the screen/view, the button is always visible. It's like a button floating over the view. The button is visible over the scroll view all the time.

I made a dummy app and it works
here is the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout android:layout_width="match_parent" android:id="#+id/relativeLayout1" android:layout_height="wrap_content">
<ScrollView android:id="#+id/scrollView1" android:layout_height="fill_parent" android:layout_width="fill_parent">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<RatingBar android:id="#+id/ratingBar1" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></RatingBar>
<Spinner android:layout_width="wrap_content" android:id="#+id/spinner1" android:layout_weight="1" android:layout_height="wrap_content"></Spinner>
<Spinner android:layout_width="wrap_content" android:id="#+id/spinner2" android:layout_height="wrap_content" android:layout_weight="1"></Spinner>
<SeekBar android:id="#+id/seekBar1" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="match_parent"></SeekBar>
<QuickContactBadge android:layout_width="wrap_content" android:id="#+id/quickContactBadge1" android:layout_weight="1" android:layout_height="wrap_content"></QuickContactBadge>
<RatingBar android:id="#+id/ratingBar2" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></RatingBar>
<RadioGroup android:layout_width="wrap_content" android:id="#+id/radioGroup1" android:layout_weight="1" android:layout_height="wrap_content">
<RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="#+id/radio0" android:layout_height="wrap_content" android:checked="true"></RadioButton>
<RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="#+id/radio1" android:layout_height="wrap_content"></RadioButton>
<RadioButton android:text="RadioButton" android:layout_width="wrap_content" android:id="#+id/radio2" android:layout_height="wrap_content"></RadioButton>
</RadioGroup>
<Spinner android:layout_width="match_parent" android:id="#+id/spinner3" android:layout_height="wrap_content" android:layout_weight="1"></Spinner>
<TextView android:layout_width="wrap_content" android:id="#+id/textView1" android:layout_height="wrap_content" android:layout_weight="1" android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium"></TextView>
<Button android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:layout_weight="1" android:id="#+id/button2"></Button>
<ProgressBar android:layout_width="wrap_content" android:layout_weight="1" android:id="#+id/progressBar1" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"></ProgressBar>
<RatingBar android:id="#+id/ratingBar3" android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content"></RatingBar>
<QuickContactBadge android:layout_width="wrap_content" android:id="#+id/quickContactBadge2" android:layout_weight="1" android:layout_height="wrap_content"></QuickContactBadge>
<Button android:layout_width="wrap_content" android:text="Button" android:layout_height="wrap_content" android:layout_weight="1" android:id="#+id/button3"></Button>
<CheckBox android:layout_width="wrap_content" android:text="CheckBox" android:id="#+id/checkBox1" android:layout_height="wrap_content" android:layout_weight="1"></CheckBox>
</LinearLayout>
</ScrollView>
<Button android:layout_height="wrap_content" android:text="Button" android:layout_width="wrap_content" android:id="#+id/button1"></Button>
</RelativeLayout>
</LinearLayout>

You should have a relative layout. Inside the layout you will have a scrollview to fill the parent both in height and with and the button will be also inside the layout but it will have align with parent top to true...
this is the answer, if you have any questions ask again

I guess you are using xml files to define your layout. Then you need to define the button outside the ScrollView definition, for example:
<ScrollView
...>
...
</ScrollView>
<Button
...>
</Button>

Guys I know this is a old post just FYI, Google has come up with a library to support the a lot of cool stuff like float button, etc. Worth a look.
Google Design Support Lib

Related

Android Studio: How to put a Recycler View under a TextView

I'm trying to put a recycler view under a textView. In the design view, the arrangement of the objects is how I want it:
But in the emulator the activity is shown like this:
That is, above are the two textViews and below are the recyclerViews.
But I want: textView - recyclerView - another textView and at the end another recyclerView
This is the code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".richieste.CronologiaRichieste">
<LinearLayout
android:id="#+id/row1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/acquistiText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ACQUISTI"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textSize="#dimen/dim_testo"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/acquisti"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:scrollbarStyle="outsideInset"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
<TextView
android:id="#+id/venditeText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VENDITE"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textSize="#dimen/dim_testo"
android:textStyle="bold" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/vendite"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="horizontal"
android:scrollbarStyle="outsideInset"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp" />
</LinearLayout>

Nested LinearLayout does not align to parent ScrollView?

I have a fragment with two horizontal Buttons (yellow = fragment_test_button_container) at the bottom of my fragment. I would like to use the remaining area (red = fragment_test_scrollview) for a ScrollView It is reuiqred that my ScrollView consist of one layout (turquoise = fragment_test_check) only . This can then in turn have further layouts, as you can see in the attached fragment_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.TestFragment">
<ScrollView
android:id="#+id/fragment_test_scrollview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/fragment_test_button_container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent=".84"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:id="#+id/fragment_test_check"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/fragment_test_front_photo_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".28"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_front_photo_title"
style="#style/myapp_MediumTextStyle"
android:layout_width="#dimen/myapp_test_fragment_textview_width"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp"
android:text="#string/fragment_test_front_photo_title_text" />
<ImageView
android:id="#+id/fragment_test_front_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_test_back_photo_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".28"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_back_photo_title"
style="#style/myapp_MediumTextStyle"
android:layout_width="#dimen/myapp_test_fragment_textview_width"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp"
android:text="#string/fragment_test_back_photo_title_text" />
<ImageView
android:id="#+id/fragment_test_back_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
</LinearLayout>
<!-- ADDITIONAL DATA -->
<LinearLayout
android:id="#+id/fragment_test_data_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".34"
android:orientation="vertical"
android:paddingStart="24dp"
android:paddingEnd="24dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_value1_label"
style="#style/myapp_MediumTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text=„Value1^“ />
<TextView
android:id="#+id/fragment_test_value1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_value2_label"
style="#style/myapp_MediumTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text=„Value2“ />
<TextView
android:id="#+id/fragment_test_value2_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/fragment_test_value3_label"
style="#style/myapp_MediumTextStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Ausstellungsdatum" />
<TextView
android:id="#+id/fragment_test_value3_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="24dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/fragment_test_button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingStart="24dp"
android:paddingEnd="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/fragment_test_scrollview">
<Button
android:id="#+id/fragment_test_back_button"
style="#style/myapp_ContinueBackButtonStyle"
android:layout_width="0dp"
android:layout_height="#dimen/myapp_continueBackButton_height"
android:layout_weight="1"
android:background="#color/myapp_colorPrimary"
android:text=„back“
android:textColor="#FFFFFF" />
<Button
android:id="#+id/fragment_test_next_button"
style="#style/myapp_ContinueBackButtonStyle"
android:layout_width="0dp"
android:layout_height="#dimen/myapp_continueBackButton_height"
android:layout_marginStart="24dpVerySmall"
android:layout_weight="1"
android:background="#color/myapp_colorPrimary"
android:text="continue"
android:textColor="#FFFFFF" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
But my turqoise (fragment_test_check) area is not aligned fully with the blue area (fragment_test_scrollview), altough I wrote match_parent. I would like the heigth of turqoise area as the blue area.
Why is my turqoise area dangling in half the blue area?
Set android:fillViewport="true" in your ScrollView.
fillViewport stretches the content's height to the viewport's boundaries, when set true. In simple words, fillViewport decides whether the nested contents of the layout should match the parent's boundaries or not. Thus, to make the nested layout match the parent's boundary, set fillViewport to true
Moreover, the layout_gravity for nested components should be 1 or whatsoever you define in weight_sum of parent layout, whereas in your case it is 0.90 .

Unable to set text view at the bottom of other text view

I have an ImaveView images and corresponding to each image there is TextView img_name and img_source, I want to align them like 1st image should be placed and at right of image image_name should be displayed and below img_name, img_source should be display.
I have tried with below layout code but couldn't work as expected
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageview_cover_art"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:gravity="center_horizontal"/>
<ImageView
android:id="#+id/imageview_favorite"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/star_disabled"
android:layout_gravity="bottom|right"/>
</FrameLayout>
<TextView
android:id="#+id/textview_book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Are You My Mother"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/textview_book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Dr. Seuss"
android:gravity="center_horizontal"
android:orientation="vertical"/>
</LinearLayout>
It is displaying like
Image > TextView > TextView
but i require like:
Image > TextView
TextView
Also if first TetView is bigger then need to break in another line
Please help.
Just add the two textview into another linearview with vertical orientation :-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="start"
>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageview_cover_art"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:gravity="center_horizontal"/>
<ImageView
android:id="#+id/imageview_favorite"
android:layout_width="24dp"
android:layout_height="24dp"
android:src="#drawable/star_disabled"
android:layout_gravity="bottom|right"/>
</FrameLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textview_book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Are You My Mother"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center_horizontal"/>
<TextView
android:id="#+id/textview_book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Dr. Seuss"
android:gravity="center_horizontal"
android:orientation="vertical"/>
</LinearLayout>
</LinearLayout>
Simply place views in Linear Layout after that add android:orientation="vertical"
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textview_book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Are You My Mother"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
/>
<TextView
android:id="#+id/textview_book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Dr. Seuss" />
</LinearLayout>

How can I align RadioButton and TextView horizontally?

See the image... If I don't Use the RelativeLayout the output is TextView
is below RadioButton
If I use the following code to align radioButton and TextView horizontally, the radioButtons are not identified as a part of RadioGroup, which leads to multiple selections.
<RelativeLayout
android:id="#+id/relative_layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView3">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:drawableLeft="#drawable/laptop_for_gaming"
android:text="#string/gaming" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/radioButton3"
android:text="#string/gaming_desp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/laptop_professional_use"
android:text="#string/business_use" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/radioButton4"
android:text="#string/business_use_desp" />
</RelativeLayout>
</RadioGroup>
</RelativeLayout>
And if I don't use the RelativeLayout they are not align horizontally instead they are one below the other.
you can use android:orientation="horizontal" on your RadioGroup
You have to close your radiogroup adding </RadioGroup> after the RadioButtons :
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView3">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:drawableLeft="#drawable/laptop_for_gaming"
android:text="#string/gaming" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/radioButton3"
android:text="#string/gaming_desp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:drawableLeft="#drawable/laptop_professional_use"
android:text="#string/business_use" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="#+id/radioButton4"
android:text="#string/business_use_desp" />
</RelativeLayout>
</RadioGroup>

Scroll view of center aligned linear layout is wrapping top content in small resolution devices and landscape mode

Scroll view of center aligned linear layout is wrapping top content in small resolution devices as well as landscape mode too.
but when i removed the center_vertical in linear lay out it is working fine, but i want that linear layout to be in center_vertical only.
Below is my code:!
<ScrollView
android:id="#+id/bodyscrollview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fillViewport="true" >
<LinearLayout
style="#style/app_update_alert_bg"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="5dp"
android:orientation="vertical" >
<TextView
style="#style/app_update_alert_bg_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/updateinfo1" />
<TextView
style="#style/app_update_alert_bg_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/updateinfo2" />
<ImageView
style="#style/margin5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/switching_image_border"
android:src="#drawable/switching_image_sb" />
<ImageView
style="#style/margin5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/switching_image_border"
android:src="#drawable/switching_image_ucc" />
<CheckBox
android:id="#+id/chckbox_dontshow_again"
style="#style/margin5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_checkbox"
android:text="#string/updateinfo_dontshow" />
<Button
android:id="#+id/btn_updatealert_confirmation"
style="#style/buttonStyle.signInButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:text="OK" />
</LinearLayout>
</ScrollView>
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ScrollView
android:id="#+id/bodyscrollview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<LinearLayout
style="#style/app_update_alert_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="#style/app_update_alert_bg_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/updateinfo1" />
<TextView
style="#style/app_update_alert_bg_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/updateinfo2"
android:layout_marginTop="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/switching_image_border"
android:src="#drawable/switching_image_sb"
android:layout_marginTop="5dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/switching_image_border"
android:src="#drawable/switching_image_ucc"
android:layout_marginTop="5dp"/>
<CheckBox
android:id="#+id/chckbox_dontshow_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/custom_checkbox"
android:text="#string/updateinfo_dontshow"
android:layout_marginTop="5dp"/>
<Button
android:id="#+id/btn_updatealert_confirmation"
style="#style/buttonStyle.signInButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="OK" />
</LinearLayout>
</ScrollView>
</LinearLayout>

Resources