views:

161

answers:

4

Looks like there's a plenty of questions about centering, same size etc, but so far I didn't find the exactly my case so I dare to ask :)

What I need is a layout of three buttons like this:

[ previous ][*select*] [  next  ]

where [previous] and [next] buttons are of the same size (i.e. in this case, size of the [previous] button as it is bigger), and the [*select*] button should stretch to occupy all of the available width.

Following the hints of making two buttons in LinearLayout same sized, i came up with the following xml file:

<LinearLayout
    android:id="@+id/button_bar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Previous" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="Select" />

    <Button
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:text="Next" />

</LinearLayout>

This almost works :) Except one thing: instead of making Next button to match the size of Previous button, android makes Previous button to be the size of the Next :) And because of this the text "Previous" gets wrapped in two lines, like

Previ
ous

Dunno if this is a bug or not, but can you advice me a workaround or some another way to achive the desired layout?

Thank you!

A: 

In that sort of case I would go for a TableLayout, in which you have one row, and a weight of 1 for EACH button. and the stretchColumn attribute put to column 1. Does this make any difference?

Sephy
i tried it. this 0dip trick doesn't work with TableLayout. and if I go without it and do as you suggest, then next and previous buttons end up with a different width and i can't find a way to make them same size :)
dpimka
A: 

I'm not sure if you can match a size to some other besides by doing this in code. The easiest solution is probably adjusting the previous and next button widths in dp/sp units until they look good and have the select button be the only one with the layout_weight attribute.

totramon
This 0dip + layout_weight trick I used is a special one exactly for my case as i learned :) google for it, you'll find it. And yes, I guess I could do this in code, but i just searching for a way to do this in xml :)
dpimka
I mean if you set your prev/next buttons to say 80dp width and your text size is defined in dp also the text should always fit inside the button regardless of screen size. Then you'll know your two buttons are the same size and select button will take up rest of the space as no other view has layout_weight set.Actually, you might still want to use layout_weight for all buttons, just set the widths to a minimum that will allow "previous" to fit on one line.
totramon
the thing is that taking the possible future translations to other languages into account I can not rely on knowing the min width of text... i'd prefer to find some way to make android do all the magic :)
dpimka
A: 

I'd suggest using a RelativeLayout

<RelativeLayout
    android:id="@+id/buttons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:clickable="false">

    <Button
    android:id="@+id/previous"

    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="Previous" 

    android:layout_alignParentLeft="true"/>

    <Button
    android:id="@+id/next"

    android:layout_width="30dp"
    android:layout_height="wrap_content"
    android:text="Next" 

    android:layout_alignParentRight="true"/>

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Select"

    android:layout_toRightOf="@id/previous"
    android:layout_toLeftOf="@id/next" />
</RelativeLayout>

This should work.

Maragues
tried it, but the result is not exactly what i want: Previous and Next buttons are of different widths, while i'd like Next to be the same as Previous :)
dpimka
This is not working either lol... crazy layout question!!
Sephy
You can set the width of the buttons manually (answer updated) and then they would be the same width. I don't know if that helps, maybe you don't want a fixed width.
Maragues
Also, you can try this approach the other way round: set the center button's width and place the other two to its right and left, setting their width to fill_parent. You'll need to change the layout to achieve this, but you'll make sure the two buttons are the same width.The problem, then, is the width to assign to the center button :)
Maragues
Yep, I'm starting to think this is the only way, though not perfect.instead of specifiyng fixed width, i could go with specifying "minWidth" - should be better :)
dpimka
A: 

Hi If you are including "android:layout_weight" you should give either "android:layout_width" or "android:layout_height" as "fill_parent"

modify your code like this

<LinearLayout
android:id="@+id/button_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="Previous" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.5"
    android:text="Select" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="Next" />

Eby
Nope, this doesn't work. this way the select button even dissapears from screen because it tries to match the parent's width twice as much as other two are ;)
dpimka
I have edited the post.. it was the problem in giving weight.. now it will work fine...try it out!!
Eby
You can play around with "android:layout_weight"
Eby
oh, now it works better, but somehow it ends up with the same issue I originally had. here, take a look:http://imagebin.ca/view/IbuT9Pp2.html
dpimka
I have edited the post made android:layout_weight="1.5"i think this is your exact answer !!!
Eby
This solution does seem to work but if you change the length of the string "next" or "previous", it can become quite ugly...
Sephy
Your solution did work for me as long as I have the same weight for previous and Next actually
Sephy
@Sephyu should adjust "layout_weight" to your requirement...
Eby
@Sephy "layout_weight" takes a float value and you can find out solution for you layout using trail and error method
Eby
what's that trail and error method?
Sephy
@Eby, yes, it works, but if you make the "previous" string longer (e.g. "Previous blalblalal" it will break again. Trial and error doesn't apply here because even if i fix it for en_US locale it will easily break for any other :)
dpimka