Reputation: 31
I'm using xml to draw a spinning progress indicator along with some text. In the bottom of the screen I have a TableLayout with two buttons, which are centered in the page with each text also centered.
<RelativeLayout
android:id="@+id/progresscontainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<ProgressBar android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingRight="10dp" />
<TextView android:id="@+id/progress_text"
android:layout_toRightOf="@id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="First text" />
</RelativeLayout>
<TableLayout
android:id="@+id/buttonbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:paddingTop="3dip"
android:background="@color/buttonbar"
android:stretchColumns="0,1">
<TableRow>
<Button android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button1" />
<Button android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button2" />
</TableRow>
</TableLayout>
In the code I have a runnable that after a couple of seconds changes the text on the progress_text TextView.
private Runnable mTimeTask = new Runnable() {
public void run() {
TextView progressText = (TextView) findViewById(R.id.progress_text);
progressText.setText("Second text");
}
};
The problem is that after this setText(), once I focus on one of the buttons, the text loses it's centered alignment and goes all the way to the left. What am I doing wrong?
Upvotes: 3
Views: 3195
Reputation: 860
None of the answers here worked for me so I tried to find my own way to fix it and was messing with a TextView that has the same alignment problem but when I added the line android:maxLines="1"
it suddenly worked so I tried it on the button and it worked.
Just add the android:maxLines="1"
to the definition of your button and the text alignment should stay after setting a new text.
Upvotes: 1
Reputation: 369
Try to reset it's padding to:
button.setPadding(left, top, right, bottom);
With value:
button.setPadding(0, 0, 0, 0);
Upvotes: 3
Reputation: 717
I had this problem too. It seems that setText()
changes the gravity settings, so I had to reapply them using:
button.setGravity(Gravity.CENTER_HORIZONTAL);
Upvotes: 7