I have an XML grid table layout menu of equally sized buttons the buttons change their size depending on the phone resolution. The idea is to make the menu fill the screen with equally sized buttons. This works perfect when the buttons don’t have a text label. But as soon as i add text on the button then the width of the button is also affected by the text. If i change the text size to a smaller size, then the button automatically re-sizes to be smaller than the others. For example: Name for Button A is “Home” Name for Button B is “Notifications”
My problem is that Button B becomes bigger than Button A, i want all button to have the same width no mater what the label text size is.
I will also attach the XML layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp"
android:background="#2b2c31"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/layout_topheader" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:stretchColumns="*" >
<TableRow
android:layout_weight="1"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/buttonScan"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn"
android:text="Refill"
android:layout_margin="10dp"
android:textColor="@color/status_text"
android:textSize="30dip"
android:textStyle="bold" />
<Button
android:id="@+id/buttonSetReminder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn"
android:text="Reminders"
android:layout_margin="10dp"
android:textColor="@color/status_text"
android:textSize="30dip"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/buttonRewards"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn"
android:layout_margin="10dp"
android:textSize="30dip"
android:textColorHint="@color/status_text"
android:textColor="@color/status_text"
android:text="Rewards"
android:textStyle="bold" />
<Button
android:id="@+id/buttonTracker"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn"
android:text=" Tracker"
android:layout_margin="10dp"
android:textColor="@color/status_text"
android:textSize="30dip"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_weight="1"
android:gravity="center"
android:padding="10dp" >
<Button
android:id="@+id/buttonProfile"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/btn"
android:text="Profile"
android:layout_margin="10dp"
android:textColor="@color/status_text"
android:textSize="30dip"
android:textStyle="bold" />
<Button
android:id="@+id/buttonChat"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/chaticon"
android:text="Chat"
android:layout_margin="10dp"
android:textColor="@color/status_text"
android:textSize="30dip"
android:textStyle="bold" />
</TableRow>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/add3" />
</LinearLayout>
</TableLayout>
</LinearLayout>
Not sure if it is the best way to do it but, I had a similar problem and solved it by getting the screen dimensions at run time with:
Then setting the button width as:
int buttonWidth = displaymetrics.widthPixels/2;
And finally setting the max and min widths/heights as buttonWidth