Christine
Christine

Reputation: 329

How to fix width of button in android irrespective of setText Programatically

I have a two buttons in a table row, when I set the text of one button programatically run time, its width changes. Is there any way to fix the width of both button irrespective of text I change via on runtime?

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="bottom">

        <TableLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerInParent="true"
            android:gravity="center" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#092435"
                android:gravity="center" >


                <Button
                    android:id="@+id/previous"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:background="#bdbdbd"
                    android:gravity="center"
                    android:padding="15dip"
                    android:text="Previous"
                    android:textColor="#ffffff"/>

                <View
                    android:layout_width="1dp"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="6dip"
                    android:layout_marginTop="6dip"
                    android:background="#85929B"/>

                <Button
                    android:id="@+id/submitdata"
                    android:layout_width="match_parent"
                    android:layout_weight="1"
                    android:background="#092435"
                    android:gravity="center"
                    android:padding="15dip"
                    android:textColor="#ffffff"/>

              </TableRow>
        </TableLayout>
    </LinearLayout>

Upvotes: 0

Views: 56

Answers (3)

user3528635
user3528635

Reputation:

Button button = new Button(this);
button.setWidth(10);

Or you can do it this way:

LinearLayout.LayoutParams params = button.getLayoutParams();
params.width = 100;
button.setLayoutParams(params);

Upvotes: 3

kgpmurray
kgpmurray

Reputation: 202

try something like

Button exampleButton = (Button) findViewById(R.id.exampleButton);
exampleButton.getLayoutParams().width = 100;

Upvotes: 2

Farhad
Farhad

Reputation: 12986

You can use minEms property of your button. Try setting it to 10 or 12 and play with number till you get the desired width.

android:minEms="10"

Upvotes: 1

Related Questions