TOMKA
TOMKA

Reputation: 1096

Android textview.setText() loses gravity?

I have a simple layout that contains a TextView between two ImageViews.

When I change the TextView at runtime with 'setText()', it changes but it loses the "center_horizontal" gravity and it is drawn at the top of the linear layout.

How can I make the TextView be drawn at the "center_horizontal" gravity after "setText()"?

</RelativeLayout
    android:id="@+id/rl1"
    style="@style/style1"
    android:layout_width="fill_parent"
    android:orientation="horizontal" >

        ...        

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_margin="5dp"
        android:background="@drawable/frame"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/iv2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:src="@drawable/icon" />

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="0"
            android:textColor="@drawable/white" />

        <ImageView
            android:id="@+id/iv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:src="@drawable/icon" />

    </LinearLayout>
        ...
</RelativeLayout>

Thanks!

Upvotes: 2

Views: 877

Answers (1)

poitroae
poitroae

Reputation: 21367

The setText method normally does not influence the layout ordering. I currently can only think of one possible problem: Your set text is either very long or very short and as you specified the dimensions as "wrap_content" the width and height of your textview aligns to the new content. This results in a new layout merging. To solve this you could do the following:

 <TextView
            android:id="@+id/tv1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="0"
            android:textColor="@drawable/white" />

Upvotes: 3

Related Questions