Keep the button aligned to bottom of the dialog - android

I've a layout like design 1 and I want to align cancel button to bottom of the dialog always. recyclerview1 & recyclerview2 have different heights due to display content. if the both recyclerview1 and recyclerview content are small and able to display within screen without scroll it should be like design 2 . If the recyclerviews exceeded the heights dialog should take the device height and align cancel button to bottom of the dialog like design 3.
Current output and final output
Current code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/textview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:text="text1"
android:textSize="10sp" />
<TextView
android:id="#+id/textview2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/textview1"
android:text="text2"
android:textSize="10sp" />
<androidx.core.widget.NestedScrollView
android:id="#+id/scrollView"
android:layout_below="#id/textview2"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutContainer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayoutContainer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/scrollView"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:gravity="right">
<Button
android:id="#+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"/>
</LinearLayout>
</RelativeLayout>

You will have to create a custom dialog
inside onViewCreated() inflate a layout_bottom_button using gravity bottom, so inflated layout will always be at bottom
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
params.gravity = Gravity.BOTTOM;
View bottomButtonView = this.getLayoutInflater().inflate(R.layout.layout_bottom_button, null);
this.getDialog().addContentView(viewM, params);
keep in mind that recyclerView should have bottom margin same as layout_bottom_button layout height, so that recyclerView does not hide below button.

Related

Add linearLayout below listView doesn't work

This is not working on my phone but I can see it in the Android Studio Design mode window. I need to place a LinearLayout or any view actually just below the listView. But I can't see it, only the listView fitting the whole height
<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:id="#+id/local_detalle_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="android.buendia.cl.pritz.fragments.locales.LocalDetalleFragment">
<view
android:id="#+id/banner"
class="com.android.volley.toolbox.NetworkImageView"
android:layout_width="match_parent"
android:layout_height="80dp"
android:scaleType="centerCrop"
app:srcCompat="?android:attr/textSelectHandleLeft" />
<TextView
android:id="#+id/current_category"
style="#style/current_category"
android:layout_below="#+id/banner"
android:text="TextView" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/current_category">
<ListView
android:id="#+id/list_view_local_detalle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/cart_bar"
style="#style/cart_bar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/cart_status"
style="#style/cart_bar_status"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal"
android:text="TextView" />
<Button
android:id="#+id/cart_next"
style="#style/cart_bar_next"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/cart_bar_next_button_text" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Set android:layout_below="#+id/list_view_local_detalle" for LinearLayout
Note : If you'r using weight for vertical linear layout then height must be 0 dp and if using for horizontal linear layout then width must be 0dp.

2 Views in a horizontal LinearLayout

I have this view ->
The view is setup basically like this
LinearLayout -orientation horizontal - Height (wrap)
LinearLayout -orientation vertical - Height (wrap) - weight .65
TextView
RecyclerView
LinearLayout - orientation vertical - Height (matchparent) - weight .35
TextView
TextView
TextView
In most cases the 1st LinearLayout (the left one) would determine the size in height because the RecyclerView would usually have more than 1 entry. But in the case of the image the 1st row this logic doesn't work. If I set the LinearLayout to the right as wrap, then when the left one is bigger then there is white around the gray background. What is the best way to achieve the 2nd layout is always the same height as the its parent and if the parent is too small the parent adjusts to it.
here is the full xml as requested
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:background="?attr/selectableItemBackground"
android:layout_width="match_parent" android:layout_height="wrap_content">
<ProgressBar android:id="#+id/progressBar" android:layout_width="wrap_content" android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".65"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/tv_organization"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textStyle="bold"
tools:text="Duke Hospital"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/availablehours"
android:textAppearance="#android:style/TextAppearance.Small"/>
<TextView
android:id="#+id/rv_available_hours"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:padding="0dp"
android:paddingBottom="4dp"
android:textAppearance="#android:style/TextAppearance.Small"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".35"
android:background="#color/offWhite"
android:gravity="center"
android:orientation="vertical"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin">
<TextView
android:id="#+id/tv_notavailable"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Subhead"
android:textColor="#color/darkerGreyColor"
android:visibility="gone"/>
<TextView
android:id="#+id/tv_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textStyle="bold"
tools:text="30"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textAllCaps="true"
android:layout_gravity="center_horizontal"
android:text="#string/abbrev_min"/>
<TextView
android:id="#+id/tv_labelDuration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/estimatedwaittime"
android:textAppearance="#style/TextAppearance.AppCompat.Caption"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
You can set the minHeight of
LinearLayout -orientation vertical - Height (wrap) - weight .65
so that right side text won't get clipped

Android set max width in percentage of a TextView in LinearLayout of vertical orientation

I'd like to set the layout_weight of the TextView with the tv_long_text to 80% in the following LinearLayout of vertical orientation.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<TextView
android:id="#+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.8"
tools:text="a pretty long text" />
</LinearLayout>
The above is not working because the orientation of the textview's parent is vertical.
So, I tried to set the android:layout_width="match_parent" in the xml and then set the width at run time by getting the measured width and then sets the width to 80% but the getMeasuredWidth is giving me 0.
int measuredWidth = longTextView.getMeasuredWidth();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) longTextView.getLayoutParams();
params.width = (int) (measuredWidth * 0.8);
longTextView.setLayoutParams(params);
I also tried to set the layout_weight at run time but it didn't work either and it's probably because it's parent view is in vertical orientation.
longTextView.setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT,
0.8f)
);
The one that worked for me is by adding some extra views for the long text view. But it's 2 more extra views added for just trying to set the width of this view in percentage. Is there any other efficient way to do this?
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.8"
android:textStyle="bold"
tools:text="a pretty long text" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
</LinearLayout>
</LinearLayout>
I would not try to fiddle with run time measurements. Your solution with a second horizontal Layout is perfectly fine since you have two TextViews expanding horizontally.
Another option would be PercentRelativeLayout from the support library com.android.support:percent:25.1.0 See here
This is from the docs
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
use android:layout_weight
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<android.support.v4.widget.Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"/>
</LinearLayout>
TRY This structure to let TextView flow only upto 75% of the screen width.
you can adjust SPACER width to achieve, what you want like 2 for 50%.
You can use android:weightSum, and achieve the same result without additional View like:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical">
<TextView
android:id="#+id/tv_short_text"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
tools:text="short text" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<TextView
android:id="#+id/tv_long_text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="8"
android:textStyle="bold"
tools:text="a pretty long text a pretty long text a pretty long text" />
</LinearLayout>
</LinearLayout>
Solution with ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content"
android:background="#drawable/selector_rect_white_gray">
<TextView
android:id="#+id/tv_short_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="short text" />
<TextView
android:id="#+id/tv_long_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/tv_short_text"
app:layout_constraintWidth_percent=".8"
tools:text="very long text - 80% of screen width exact" />
</androidx.constraintlayout.widget.ConstraintLayout>

Scrollview does not scroll when keyboard appears

I have a few EditTexts in my RelativeLayout, and a button aligned at the bottom of it. When the keyboard pops up, I want my layout to resize (therefore, I'm using android:windowSoftInputMode="adjustResize") and also want it to be scrollable, because in smaller screens resizing it isn't enough.
The problem is I can't get it to scroll at all!
I do need the Button to stay at the bottom of the screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF">
<!-- some hidden RelativeLayouts, filling the whole screen -->
<RelativeLayout
android:id="#+id/form_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent" android:layout_height="match_parent"
android:fillViewport="true" android:layout_above="#+id/ok_btn">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:background="#99000000" android:visibility="visible">
<View
android:id="#+id/shade"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignTop="#+id/logradouro1"
android:background="#FFF" android:layout_marginTop="-30dp"/>
<!-- some visible EditTexts and TextViews here -->
</RelativeLayout>
</ScrollView>
<Button android:id="#+id/ok_btn"
android:layout_width="match_parent"
android:layout_height="50dp" android:background="#color/verdeBotao" android:text="Cadastrar"
android:textColor="#FFF" android:layout_alignParentBottom="true"/>
</RelativeLayout>
</RelativeLayout>
Below are 2 screenshots to illustrate what is happening. Please notice that I can't scroll when the keyboard is up.
Thanks!
i am posting something similar to your ui. take a RelativeLayout for the button at the bottom. set gravity to bottom and your button view should android:layout_alignParentBottom="true"
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/m_table_menu" >
<LinearLayout
android:id="#+id/layuserdetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:padding="5dp" >
Yourviewss......i guess edittexts
</LinearLayout>
</ScrollView>
//scrollable Button layout
<RelativeLayout
android:id="#+id/m_table_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:padding="0dp" >
<LinearLayout
android:id="#+id/laysharepost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:orientation="vertical"
android:padding="5dp" >
//replace textview with buttonview..
<TextView
android:id="#+id/share_tvcommentdone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/button_selector"
android:padding="10dp"
android:text="POST"
android:textColor="#android:color/white" />
</LinearLayout>
</RelativeLayout>

Top view in a LinearLayout embedded in a ScrollView only half visible

In my app, I insert pairs of spinners into a linearlayout, which is inside a scrollview. Problem is, when I add enough spinners, the top two are partially obscured. Here's what I'm talking about.
<LinearLayout
android:id="#+id/ChordHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:drawable/bottom_bar"
android:orientation="horizontal" >
<Button
android:id="#+id/addChordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Add Chord" />
<TextView
android:id="#+id/spacetext1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/removeChordButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Remove Chord" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:id="#+id/ChordList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:isScrollContainer="true"
android:orientation="vertical"
android:weightSum="100" >
(Spinners get inserted here)
</LinearLayout>
</ScrollView>
</LinearLayout>
And here is the code that inserts spinners into the list, if the problem is with that.
Spinner chordName = new Spinner(this);
Spinner chordType = new Spinner(this);
LinearLayout container = new LinearLayout(this);
container.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout chordList = (LinearLayout) findViewById(R.id.ChordList);
chordName.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 80, 50));
chordName.setAdapter(nameAdapter);
container.addView(chordName); //add name spinner to container
chordType.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 80, 50));
chordType.setAdapter(typeAdapter);
container.addView(chordType); //add type spinner to container
container.setBackgroundResource(android.R.drawable.bottom_bar);
container.setPadding(10, 0, 10, 0);
chordList.addView(container); //add container to list layout
Here is my layout.. try it out :)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/ChordHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/bottom_bar">
<Button
android:id="#+id/addChordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Add Chord" />
<Button
android:id="#+id/removeChordButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ellipsize="middle"
android:text="Remove Chord" />
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/ChordHeader"
>
<LinearLayout
android:id="#+id/ChordList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
Now for the buttons you might want to set a dimen if you really want them the same size.
Here is how to store dimensions in Android : More Resource Types: Dimension
Try getting a reference to the top item in the list and then request focus on it, after you are done adding the spinners.
chordList.getChildAt(0).requestFocus();

Categories

Resources