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.
Related
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
I'm doing my best to center the text vertically but I cannot manage to do it. The problem is that I have rows of buttons with height=fill_parent and weight=1 and as the rows become smaller my text starts touching the bottom of the view as seen here:
I tried removing the padding, margin, changing the height and so on. But nothing seems to do it.
How can I align it vertically even when the text size is close the the view height?
Here's the code for the view containing number 5:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="#dimen/normal_margin"
android:paddingRight="#dimen/normal_margin"
android:padding="0dp" />
As per my advice if you have two options to make such layout and getout from the issue you are having.
1. Use GridView
Using gridview you will have equal space in all four directions(left, right, top, bottom). You dont have to worry about the equal spacing for the grid item.
<GridView
android:id="#+id/album_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:cacheColorHint="#00000000"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="spacingWidthUniform"
android:drawSelectorOnTop="true"/>
Just try above code with which you will have all view equally distributed in the grid.
2. Use TableLayout and TableRow
Please check below code to use TableLayout and TableRow with its attribute to have all view equally arranged. Even if you smaller the height and width of the TableLayout, it will remain equally arranged in that view.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_margin="5dp">
<TableRow android:weightSum="3"
android:layout_weight="1" android:gravity="center">
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
</TableRow>
<TableRow android:weightSum="3" android:layout_weight="1" android:gravity="center">
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
</TableRow>
<TableRow android:weightSum="3" android:layout_weight="1" android:gravity="center">
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
<Button android:gravity="center"
android:textSize="13sp" android:textColor="#000000"
android:text="1" android:layout_weight="1"/>
</TableRow>
</TableLayout>
Please find output of this TableLayout.
Let me know if this issue is resolved. If not, I will be happy to help you again.
Enjoy Coding... :)
If this button is inside a linearlayout with vertical orientation. Since you define the weight, you want to make the height to be "0dp" instead of "fill_parent".
To make the text center vertical. Did you try android:gravity:"center_vertical"?
Try this
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_below="#+id/dialer_title"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btn1"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_1"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_1_no_vm" />
<ImageButton
android:id="#+id/btn2"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_2"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_2" />
<ImageButton
android:id="#+id/btn3"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_3"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_3" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btn4"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_4"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_4" />
<ImageButton
android:id="#+id/btn5"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_5"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_5" />
<ImageButton
android:id="#+id/btn6"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_6"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btn7"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_7"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_7" />
<ImageButton
android:id="#+id/btn8"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_8"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_8" />
<ImageButton
android:id="#+id/btn9"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_9"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_9" />
</LinearLayout>
<LinearLayout
style="#style/DialPadRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btnStar"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_astr"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_star" />
<ImageButton
android:id="#+id/btn0"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_0"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_0" />
<ImageButton
android:id="#+id/btnHash"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_hash"
android:scaleType="centerCrop"
android:soundEffectsEnabled="false"
android:src="#drawable/dial_num_hash" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2" >
<ImageButton
android:id="#+id/btnContacts"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/string_menu_contacts"
android:scaleType="centerCrop"
android:soundEffectsEnabled="true"
android:src="#drawable/selector_dial_contact_b" />
<ImageButton
android:id="#+id/btnCall"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/string_menu_call"
android:scaleType="centerCrop"
android:soundEffectsEnabled="true"
android:src="#drawable/dial_num_call" />
<ImageButton
android:id="#+id/btnDelete"
style="#style/DialPadButton"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:contentDescription="#string/desc_backspace"
android:scaleType="centerCrop"
android:soundEffectsEnabled="true"
android:src="#drawable/dial_num_delete" />
</LinearLayout>
</LinearLayout>
This will create phone dealer with equal button size.
For style="#style/DialPadButton" create style with name DialPadButton and customize buttons as you want. In my case I have added item name="android:layout_margin" with value 5dp.
design this using a grid view you will have some more functionality. else take one Linear Layout inside that set another three linear layout orientation horizontal and weight sum= 3 ,put each button on the child layout and give layout weight =1 for each button.
It does not mean that the text "5" is not centered. The text "5" appears slightly below center because the button is not given enough space to layout its text content. Even though it is provided the adequate height, the text "5" will appear slightly above center. This is because, there are characters('y', 'g', etc) with larger descent than "5" (maximum possible ascent and descent are considered during layouting the text even tough your text contains no taller glyph or glyph with non zero descent). Also see this.
If more height is provided for the buttons in row, they will neatly center align their texts.
I would suggest using a TextView instead of a Button, because that has less tricky margins and gives you more control on how to place your Text. (In general I only seldom use buttons, but tend to make other views TextView, ImageView,... clickable instead)
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="#dimen/normal_margin"
android:paddingRight="#dimen/normal_margin"
android:padding="0dp" />
In case you need some kind of indication on whether or not the TextView was pressed, you can add a selector as a background.
For this, have a look here.
Finally if you want your text to fit for any possible view sizes, have a look at the AutoFitTextView.
I think the simplest way to achieve this by using AutoFitTextView
You can get this library from this link : https://github.com/grantland/android-autofittextview
It will dynamically resize text size with current height and width.
I hope this will help you.
Good Luck.
Try to use a negative top padding, for example:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="5"
android:layout_weight="1"
tools:ignore="HardcodedText"
android:clickable="false"
android:textSize="90dp"
android:textColor="?attr/color1"
android:gravity="center"
android:paddingLeft="#dimen/normal_margin"
android:paddingRight="#dimen/normal_margin"
android:paddingTop="-2dp" />
You can play with that, and get the necessary value for top padding. Of course, you can use different values for different screen densities by using #dimen.
you can't use upper spacing for centering because it belongs to font. I mean every font has ascent and descent metric. See pic below.
so when android centers text it calculates it's overall height (including upper nad bottom spacing). And than centers resulted rect. You can get FontMetrics to see font acsent and descent values.
E.g. calling Paint.FontMetrics fm = textView.getPaint().getFontMetrics();we get:
fm = {android.graphics.Paint$FontMetrics#3668}
ascent = -25.976562
bottom = 7.5878906
descent = 6.8359375
leading = 0.0
top = -29.572266
Intresting? So how to remove top and bottom spacing and use this space to center font? I see two solutions:
create custom component and layout text excluding those spacings. If
you want only single line text it will be easy. See TextView code.
we know spacings, so we can move text accordingly. Set translation to
a Button content. To remove top spacing you can do:
button.setTranslationY(fm.top - fm.ascent); If you want text centered
without spacing, do following: button.setTranslationY((fm.top - fm.ascent) + (fm.bottom - fm.descent));
I need to set two buttons side by side... I have used linearlayout for it... I have given each button to 0.5 layout_weight as i want each one to take equal space..I also make button to take equal height. The problem i am facing that left button will slide down if i give right button to match its height.
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/botomMarginTextView"
android:orientation="horizontal" >
<Button
android:id="#+id/id1"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginLeft="20dp"
android:layout_weight="0.5"
android:background="#drawable/button_back"
android:text="#string/name1" />
<Button
android:id="#+id/id2"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:background="#drawable/button_back"
android:text="#string/name2" />
</LinearLayout>
Edit :
Please give left button longer text and right button smaller
text..otherwise it is producing right layout
I am using #android:style/Theme.Black.NoTitleBar.Fullscreen . If i am going with normal theme then it is working fine
Now how to make this button keep side by side and also to make them of equal height?
To android:layout_height attribute of LinearLayout, give value of 60dp
android:layout_height="60dp"
and to android:layout_height attribute of both Button, give value of match_parent
android:layout_height="match_parent"
So, updated XML snippet will be
<LinearLayout
android:id="#+id/buttonLayout"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:layout_below="#id/botomMarginTextView"
android:orientation="horizontal" >
<Button
android:id="#+id/id1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_weight="0.5"
android:background="#drawable/button_back"
android:text="#string/name1" />
<Button
android:id="#+id/id2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="20dp"
android:layout_weight="0.5"
android:background="#drawable/button_back"
android:text="#string/name2" />
</LinearLayout>
I want to put in the same row a TextView, and Edittext and a button but I am having the problem that the button is not aligned properly to left and in small screens edittext fills entire with.
Small screen:
Big Screen:
My codification is as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right" >
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</RelativeLayout>
</LinearLayout>
Apply a weight to your EditText so it will take up as much room as it can while letting the other two elements do the normal wrap_content. To do this, remove the relative layout container and then change the EditText width to "0dp" and give it a layout_weight of "1" as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</LinearLayout>
First, many people will tell you that hint is Android's solution for not needing the label. I don't care if you use the label or not but it does save you space, especially on smaller screens. That was just an FYI.
Now, your RelativeLayout that only has a Button appears to be useless...I would remove that. You can use layout_weight so that each View takes up the appropriate amount of space. Make sure to make the layout_width="0dp". So it may look something like
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Go" />
</LinearLayout>
Here I used 2,3,1 for the weights of your TextView, EditText, and Button respectively. You may need to change those to get exactly what you want but that should give you a start.
Layout weigth is ideal for designing layouts that adjust to screen size. However, make sure to set layout_width to 0dp, or it won't work properly.
Use like this:
android:layout_width="0dp"
android:layout_weight="1"
I'm going to assume you mean the button is not properly aligned to the right.
It's because your RelativeLayout's android:width="wrap_content", but it should be android:width="match_parent".
Also, you'd be better off setting your EditText's android:width="0dp" and adding android:weight="1" so that it expands/contracts between screen sizes.
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 :)