Same Size Buttons in Two LinearLayouts - android

I have two LinearLayouts in my Android application. One (the top) holds a TextView and a Button and the one below it holds two buttons. I was wondering if there was a way to make the right button of the upper LinearLayout the same size as the right button of the bottom LinearLayout.
<LinearLayout android:layout_width="fill_parent"
android:id="#+id/linearLayout1" android:layout_height="wrap_content"
android:orientation="horizontal" android:paddingTop="15.0dip">
<TextView
android:paddingLeft="5.0dip"
android:textSize="33px"
android:textStyle="bold"
android:layout_height="wrap_content"
android:id="#+id/textViewPoints"
android:text="Points: 0"
android:layout_width="wrap_content"
android:layout_gravity="center_vertical"
android:paddingRight="25.0dip"
android:layout_weight="0" />
<Button
android:enabled="false"
android:onClick="myClickHandler"
android:layout_height="wrap_content"
android:id="#+id/buttonAddTracker"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
android:text="#string/buttonAddTracker"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/linearLayout2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingTop="15.0dip">
<Button
android:text="Calculate"
android:id="#+id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="myClickHandler" />
<Button
android:text="Reset"
android:id="#+id/button02"
android:layout_toRightOf="#+id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="myClickHandler"></Button>
</LinearLayout>

Well there are two ways how you can do that ...
1) If you are using Linear Layout as you are, then you'll have to fix the WIDTH of the Left TextView and Left Button then can't be wrap content , just give them say 40 dip . After that put Right Buttons Width to fill_parent or fix a WIDTH for them too .
2) Use TableLayout and put each of them in their respective columns and rows and the TableLayout should handle it for you.

Related

TableLayout divide space without stretching the button?

I want the buttons to be their original size (wrap content), but them to be aligned in that logical cell location. Is that possible? I mean, I want something like,
|[Lisa]_______|[Simpson]____|
not,
|[___Lisa____]|[__Simpson__]|
or,
|[Lisa]|[Simpson]___________|
The following code did not work.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Lisa"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Simpson"/>
</TableRow>
TableLayout centers its items by default. What you can use is a LinearLayout, like this:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Lisa"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Simpson"/>
</LinearLayout>
This divides the space of the two buttons evenly, but does not center each button. This is how it looks.

How to set two button properly to each other in android

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>

Layout gravity left-right

I have a linearlayout with two linearlayouts inside. Both linearlayouts have 2 buttons. I want to align the first two buttons to the left, and the other two buttons to the right. Unfortunately I have the four buttons (or the 2 layouts) side by side.
This is my code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:background="#drawable/vat_header_selector"
android:orientation="horizontal" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_numbers"
android:layout_width="90dp"
android:layout_height="30dp"
android:textColor="#drawable/vat_btn_header_textcolor_selector"
android:background="#drawable/vat_btn_header_selector"
android:text="Numbers">
</Button>
<Button
android:id="#+id/btn_text"
android:layout_width="90dp"
android:layout_height="30dp"
android:textColor="#drawable/vat_btn_header_textcolor_selector"
android:background="#drawable/vat_btn_header_selector2"
android:text="Words">
</Button>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_black"
android:layout_width="50dp"
android:layout_height="30dp"
android:textColor="#drawable/vat_btn_header_textcolor_selector"
android:background="#drawable/vat_btn_header_selector"
android:text="B">
</Button>
<Button
android:id="#+id/btn_white"
android:layout_width="50dp"
android:layout_height="30dp"
android:textColor="#drawable/vat_btn_header_textcolor_selector"
android:background="#drawable/vat_btn_header_selector2"
android:text="W">
</Button>
</LinearLayout>
</LinearLayout>
This is how it looks like:
And this is what I want:
Gravity left, right doesn't work when the orientation of the LinearLayout is set to horizontal(as the views are placed along the X axis). One way to do what you want is to insert an empty View between the two LinearLayouts to create a space between them:
<View android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
Apply below properties to Second Linear Layout which contain Button B and Button W.
android:gravity="right"
android:layout_weight="1"

Make a TextView and a Button the same size

I have a TextView and Button in my program and I cannot get the size of the Button and the TextView to be the same. How can I do this?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="67dp"
android:background="#999999"
android:gravity="center"
>
<Button
android:id="#+id/button1"
android:layout_width="130dp"
android:layout_height="60dp"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="130dp"
android:layout_height="60dp"
android:background="#ffffff" android:textColor="#000000"
android:textSize="24dp" android:textStyle="bold"
android:gravity="center"
android:text="0.0" />
</LinearLayout>
Actually, they both have the same size, but the Button uses an image as a background and it seems it has some margins.
To test this, override the background of button with a color and see they are the same size:
android:background="#0F0"
So, the solution would be to provide a custom background for your button, or adapt the TextView to match the Buttons' width and height, minus the margins of Button, which personnaly I don't think is the best approach.
When I want multiple controls to have the same size what i generally do is put them in a TableLayout.
In your case, i'd put a TableLayout inside the linear layout and put the button and textview inside a TableRow, set the weightsum for the TableRow to 2 and set the weights of the individual controls to 1.
This would make the controls take up the same amount of space on the screen. A sample xml is shown below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello_world">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</TableRow>
</TableLayout>
</LinearLayout>

How to make buttons cover full screen width in Android Linear Layout?

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.

Categories

Resources