I have 2 buttons nested inside a Horizontal LinearLayout, and I want to align them both to the bottom of the screen. I've set the LinearLayout's weightsum property to 1 & the 2 buttons' layoutweight property also to 1 & I get this result :
I get this weird space around the buttons and I want to eliminate it. How to ?
Here is the XML code:
<LinearLayout
android:id="#+id/hourlyDailyLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:id="#+id/hourlyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hourly"
android:textColor="#ffffffff"
/>
<Button
android:id="#+id/dailyButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/daily"
android:textColor="#ffffffff"
/>
</LinearLayout>
Buttons have padding by default. They way to combat that is to set negative margins
<Button
android:id="#+id/dailyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/daily"
android:textColor="#ffffffff"
android:layout_marginBottom="-5dp"
/>
<Button
android:id="#+id/hourlyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hourly"
android:textColor="#ffffffff"
/>
<Button
android:id="#+id/dailyButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/daily"
android:textColor="#ffffffff"
/>
set the width to 0dp
Related
I have a LinearLayout with two buttons. But, when a button has some longer text that needs breaking, the button's height does not expand to fit the text. Instead it crops the text.
Here's what I have:
<LinearLayout
style="?android:attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="#+id/previous_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="#string/previous_button"
android:textColor="#color/light_blue" />
<Button
android:id="#+id/next_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:text="#string/next_button"
android:textColor="#color/light_blue" />
</LinearLayout>
Any help on why button crops the text?
Try adding android:singleLine="false" property to the problematic button. Hope this solve your problem.
If it doesn't work, probably you'll need to remove the button's default padding by adding android:padding="0dp"
You can make the text to align in multi-lines.
A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons.
To disable this behaviour, set android:baselineAligned="false" on the LinearLayout
For Example,
<LinearLayout
android:id="#+id/bottom_linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<Button
android:id="#+id/two_board_btn_continue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_blue_button"
android:text="#string/continue_bidding"
android:textColor="#android:color/white" />
<Button
android:id="#+id/two_board_btn_checkOut"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_blue_button"
android:text="#string/checkout"
android:textColor="#android:color/white" />
<Button
android:id="#+id/two_board_btn_cart"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/rounded_corner_blue_button"
android:text="#string/add_to_cart"
android:textColor="#android:color/white" />
</LinearLayout>
Just remove #style from Both the buttons.
How much long your Button name. you have already used the match parent. So, only you can do to reduce the textSize of Button. That's All.
I have two Buttons called btnBeginner and btnAdvanced.
I have divided these two Buttons equally by using the layout_weight property. But the layout_weight is bad for performance.
Because of that, I would like to change my existing code - which is shown below.
<LinearLayout
android:id="#+id/lLayoutBeginAdv"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnBeginner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="#color/color_exam_btn_hlight"
android:text="beginner"
android:textAllCaps="false"
android:textColor="#color/white"
android:textStyle="normal" />
<Button
android:id="#+id/btnAdvanced"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".5"
android:background="#color/color_exam_btn_normal"
android:text="advanced"
android:textAllCaps="false"
android:textColor="#color/white"
android:textStyle="normal" />
</LinearLayout>
Please help me to do the same without using the layout_weight property.
EDIT : PercentRelativeLayout was deprecated in API level 26.1.0.
consider using ConstraintLayout and associated layouts instead.
Nested weights are bad for performance because :
Layout weights require a widget to be measured twice. When a
LinearLayout with non-zero weights is nested inside another
LinearLayout with non-zero weights, then the number of measurements
increase exponentially.
So in your case, weight will not create the performance problem. But still if you want to divide the layout into two equal parts without using weight, you can use PercentRelativeLayout
Sample :
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">
<Button
android:id="#+id/btnBeginner"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/color_exam_btn_hlight"
android:text="beginner"
android:textAllCaps="false"
android:textColor="#color/white"
android:textStyle="normal"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
/>
<Button
android:id="#+id/btnAdvanced"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#color/color_exam_btn_normal"
android:text="advanced"
android:textAllCaps="false"
android:textColor="#color/white"
android:textStyle="normal"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
Visit this Github repo for more information
I'd place a dummy View in the center of a RelativeLayout.
Then set a Button to the right of it and another one to the left of it.
Something like so
<RelativeLayout>
<!-- Dummy in the center -->
<View
android:id="#+id/dummy"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<!-- Left Button -->
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:toLeftOf="#id/dummy"
android:text="New Button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<!-- Right Button -->
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:toRightOf="#id/dummy"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
You should set layout_width = "0dp" for all the buttons in Linear Layout.
As layout_weight, superseding layout_width. So essentially the layout_width is getting ignored.
So basically you code should be like :
<LinearLayout
android:id="#+id/lLayoutBeginAdv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<Button
android:id="#+id/btnBeginner"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
...../>
<Button
android:id="#+id/btnAdvanced"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
..... />
</LinearLayout>
For more reference you should this link :
layout_width and layout_weight - performance
If you don't want use layout_weight use Relative Layout`
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>`
If you are looking out to divide two buttons in single row using constraint layout then below is the example for you.
<?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="wrap_content"
tools:context="com.com.schooldemo.example.MainActivity">
<Button
android:id="#+id/buttonA"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="A"
app:layout_constraintEnd_toStartOf="#+id/buttonD"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/buttonD"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="D"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="#+id/buttonA" />
</android.support.constraint.ConstraintLayout>
I'm attempting to resize buttons on rotate to expand proportionately. Right now, they are a fix length, but I'm unsure of an alternative method to get the buttons to resize (setting the weight doesn't seem to help). I want the buttons to essentially fill the length of the screen both vertically and horizontally. Please let me know if you need more information.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_gravity="center_vertical" >
<Button
android:id="#+id/mainCommentBtn"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="1"
android:text="#string/commentBtn" />
<Button
android:id="#+id/mainProfileBtn"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="1"
android:layout_centerInParent="true"
android:text="#string/profileBtn" />
<Button
android:id="#+id/mainDetailBtn"
android:layout_width="119dp"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/shareBtn" />
</LinearLayout>
Replace your resource with this. Copy the edited changes
Notice how there width is 0dp because they are using weight.
Using Weight:
1. In order to use weight you need to set the weightSum in the parent.
2. Make sure the width or height is set to 0 according to how you are using weight.
(for instance since we are going with horizontal orientation we want to use "0dp" width. This allows weight to control the width of the object.
3. Lastly make sure the weight of all the objects add up to the weightSum.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="5dp"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:id="#+id/mainCommentBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/commentBtn" />
<Button
android:id="#+id/mainProfileBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/profileBtn" />
<Button
android:id="#+id/mainDetailBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_weight="1"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="#string/shareBtn" />
</LinearLayout>
You need to specify an orientation for the LinearLayout.
I guess you want to use android:orientation="horizontal".
Then, in order to make android:weight work, you'd need to specify the width of every button as 0px by using android:layout_width="0px" for every button.
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
These xml properties size the button to fill the screen both vertically and horizontally.
You can create the layout with the help of layout weight so that it will automatically fill the screen in landscape mode also as below:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_weight="1" />
</LinearLayout>
I want to layout 3 element Button , TextView , Button on the bar like this
[Button1] --- TextView --- [Button2]
2 button are always anchor fixed in left and right screen(padding 4dp) ,and TextView is center (change width depend on screen size),They should be apply for all screen size(not scaled in larger screen). How can I do it??
It would look something like this.-
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
Notice that you still need to set other properties such as texts, ...
You can use LinearLayout with android:weightSum
Try Like this, By using android:weightSum, you can divide how much space for Button and textView.. And it will automatically adjust in all the density devices.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="LeftButton" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="4dp"
android:layout_weight="4"
android:gravity="center"
android:text="Center TextView text" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="4dp"
android:layout_weight="3"
android:text="Right Button" />
</LinearLayout>
android:weightSum work fine for me, I resolved all my troubles :)
I have following three button in a Linear Layout with width fill_parent.
How can I set the width of these buttons to equally cover the whole screen area?
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnReplyMessage"
android:gravity="left"
android:text="Reply"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnMarkAsUnread"
android:gravity="left"
android:text="Mark as unread"
/>
<ImageButton
android:id="#+id/btnDeleteMessage"
android:src="#drawable/imgsearch"
android:gravity="right"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
/>
Give all buttons the following properties
android:layout_width="fill_parent"
android:layout_weight="1"
fill_parent tells them to consume as much width as possible, and weight determines how that width shall be distributed, when more than one control are competing for the same space. (Try playing around with different values of weight for each button to see how that works)
You should just specify these attributes for each button:
android:layout_width="fill_parent"
android:layout_weight="1"
So it should be something like that:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
you can use following code:
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
You must to do 0dp in width on every button.