Android: Fully Justify Two Buttons - android-layout

I have a linear layout, with layout parameters of fill parent, and wrap content. The orientation is horizontal.
Inside the linear layout I have two buttons. They appear next to each other on the left hand side.
I would like the two buttons to "fully justify". In other words I want each button to take half the width of the linear layout.
Can anybody help?

use weights like:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="Button1"
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="Button2"
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

define a weight sum for the parent set each children to half
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:text="Button1"
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="Button2"
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>

Related

I want to make a TextView height "match_parent" whose parent height is "wrap_content" in XML (Android Studio)

Inside the layout for my activity i have two TextView inside a RelativeLayout.
The one with id "descriptionfromDB" takes the text from a database while the other one will only show the text Description.
What I want is that the second one matches the height of the parent when the text of the one that gets modified become longer.
I tried to do it with android:layout_height="match_parent" for the first TextView (since parent height grows when the other TextView's text become longer) but with no results.
Here's an example of how it looks at the moment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/description"
android:layout_width="100dp"
android:layout_height="match_parent"
android:textSize="18dp"
android:text="Description"
android:background="#drawable/text"
android:textColor="#color/blue_fontcolor"
android:padding="1dp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
android:layout_toRightOf="#+id/description"
android:text=""
android:background="#drawable/text"
android:textColor="#color/blue_fontcolor"
android:padding="3dp"
/>
</RelativeLayout>
You can achieve the behavior you want using horizontal LinearLayout :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/description"
android:layout_width="100dp"
android:layout_height="match_parent"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
Using weight would be a better idea since there are many different screen sizes. Below attributes determine that the first TextView should take 30% of the parent width and second one 70% which is the remaining width.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.3"
android:text="Description"
android:textSize="18sp" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:textSize="16sp" />
</LinearLayout>
If you are going to add more views and nest this LinearLayout into another ViewGroup, since your layout will no longer be flat and also horizontal LinearLayout has double layout taxation, I recommend ConstraintLayout. Same behavior using ConstraintLayout :
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:text="Description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/descriptionfromDB"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/description"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

EditText wont display at the center of the TextInputLayout

No matter what I do my EditText appears like this :
My XML :
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_search"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="top"
android:gravity="top"
android:layout_height="wrap_content">
<EditText
android:background="#null"
android:textColor="#color/black"
android:layout_centerInParent="true"
android:gravity="top"
android:layout_gravity="top"
android:id="#+id/SearchText"
android:textColorLink="#color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/normatext"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
I changed all possible values for gravity and layout_gravity in every possible variation! Any ideas ?
EDIT :
Full Layout :
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_marginTop="0dp"
android:paddingTop="6dp"
android:paddingBottom="5dp"
android:layout_margin="3dp"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="45dp">
<Button
android:layout_width="43dp"
android:layout_height="wrap_content"
android:background="#drawable/ham"
android:id="#+id/hambtn"
android:textColorHint="#color/black"
android:layout_weight="0"
/>
<android.support.design.widget.TextInputLayout
android:id="#+id/text_input_layout_search"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="top"
android:gravity="top"
android:layout_height="wrap_content">
<EditText
android:background="#null"
android:textColor="#color/black"
android:layout_centerInParent="true"
android:gravity="top"
android:layout_gravity="top"
android:id="#+id/SearchText"
android:textColorLink="#color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/normatext"
android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="55dp"
android:layout_weight="0"
android:layout_height="match_parent"
android:background="#drawable/my_button_bg"
android:text="news"
android:id="#+id/newsbtn"
android:layout_marginLeft="8dp"
android:layout_marginRight="6dp"
android:paddingBottom="5dp"
/>
</LinearLayout>
I think the problem is maybe in your topmost LinearLayout view.
Try removing those lines:
android:layout_marginTop="0dp"
android:paddingTop="6dp"
android:paddingBottom="5dp"
android:layout_margin="3dp"
and add those lines instead to make sure there are no padding and margin at all:
android:padding="0dp"
android:layout_margin="0dp"
If it did solve the problem, you can go ahead and read more about padding and margin here.
If not, I would suspect that your TextInputLayout gets confused because of the inner EditText asking it to be both top and center. remove the "center" part (e.g. remove this - android:layout_centerInParent="true" from your EditText) and check if it solves your issue :)
Never had this problem before but i would make sure explicitly that there is no padding in the parent and no margin in the child. i.e.:
<android.support.design.widget.TextInputLayout
...
android:padding="0dp"
>
<EditText
...
android:layout_margin="0dp"
/>
</android.support.design.widget.TextInputLayout>
Can You remove the background tag of EditText and reply me if it works or not ?
Also provide your full layout file for exact solution.

Listview header formatting does not format buttons

In a listview a header with two buttons is added. You can see the buttons "none" and "all" below right under the actionbar. Then follows the first item of the list. The buttons should be in the centered and take equal space.
I have a similar solution for a footer (snapshot from a different listview) where it works. You can see the bottom of the list and then the two buttons nicely distributed horizontally.
Here is the code from the header:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerVertical"
android:weightSum="1"
android:showDividers="middle"
android:orientation="horizontal">
<Button
android:id="#+id/btn_none"
style="?android:attr/buttonBarButtonStyle"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_marginLeft="16"
android:layout_marginRight="16"
android:text="#string/btn_none" />
<Button
android:id="#+id/btn_all"
style="?android:attr/buttonBarButtonStyle"
android:layout_marginLeft="16"
android:layout_marginRight="16"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="0.5"
android:text="#string/btn_all" />
</LinearLayout>
I also tried Relative Layout but again it works only in the footer and not in the header. How can I adjust the buttons in the header?
Here is the footer code. It contains an additional view to establish a horizontal line.
<LinearLayout
android:id="#+id/dts_id_button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerHorizontal"
android:orientation="vertical"
android:showDividers="middle" >
<View
android:layout_width="match_parent"
android:layout_height="0dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:dividerVertical"
android:orientation="horizontal"
android:showDividers="middle"
android:weightSum="1" >
<Button
android:id="#+id/dts_btn_cancel"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/btn_cancel" />
<Button
android:id="#+id/dts_btn_done"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="#string/btn_done" />
</LinearLayout>
</LinearLayout>

LinearLayout won't fill parent; instead if fits to the smallest width view in it

I have a view that looks like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout android:id="#+id/header"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:paddingTop="5dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<ImageView android:id="#+id/title_icon"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:src="#drawable/title_icon" />
<LinearLayout android:id="#+id/title_layout"
android:layout_toRightOf="#id/title_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
<TextView android:id="#+id/title_text"
android:text="#string/activity_complete"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="15dp"
android:textSize="12dp"
android:textStyle="bold"/>
</LinearLayout>
<TextView android:id="#+id/status_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textSize="8dp" />
</RelativeLayout>
<TextView android:id="#+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="5dp"
android:paddingLeft="15dp"
android:textSize="10dp"/>
<ImageView android:id="#+id/main_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/grey"
android:padding="5dp"/>
</LinearLayout>
My problem is that the main LinearLayout view doesn't match_parent or fill_parent. It seems to want to only contain the smallest item within it. In my case it will result in a width the same as main_image, cutting off the content of everything else. However, if I set main_image to visibility.gone then it will match the width of message_text.
I've tried removing everything except main_image and the main LinearLayout still only fits the image width.
The other thing is that this layout is inflated into another LinearLayout which is set to fill_parent.
Wrapping the ImageView in another LinearLayout fixed the issue.
I just had this problem -- adding a LinearLayout configured to "match_parent" to another LinearLayout, but the inner layout did not fill the outer one properly.
Initially I was doing this to add the layout:
LinearLayout inner = getLayoutInflater().inflate(R.layout.inner, null);
outer.addView(inner, 0);
After a few minutes of exasperation I tried this instead:
LinearLayout inner = getLayoutInflater().inflate(R.layout.inner, outer);
And inner matched the outer parent width and height.

Android float button over scroll view

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

Resources