Jan
Jan

Reputation: 2520

TextView truncating number in Android

I'm having trouble with setting a number into the text view, it only shows the first digit and truncates the rest. Example, if i set this string "12345" it only shows "1". Notice that if i hardcode that number in the setText method, it works, but when I set the number through Integer.toString, it doesn't. Also i made a Toast for debugging and the String it shows is the right one.

Here is my code for the Activity:

int inviteCount = inviteArray.size();
String inviteCountString = Integer.toString(inviteCount);
inviteCountTextView.setText(inviteCountString);
showToast(inviteCountString); 

protected void showToast(final String text) {
    //   Creates a Toast when there is an error
    runOnUiThread(new Runnable(){
        @Override
        public void run() {
            Toast.makeText(RootActivity.this, text, Toast.LENGTH_SHORT).show(); 
        }
    });
}

And here is the code for the Layout File

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/inviteImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/friend_invitations_background" />

    <TextView
        android:id="@+id/inviteNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="2dp"
        android:text="0"
        android:textColor="#88ffffff"
        android:textSize="12sp" />
</FrameLayout>

Thanks in advance.

EDIT:

Here is a screenshot of the error. As you can see, the toast is showing a 10. But on the texfield which is at the top is only showing a 1. Again, if I write android:text="10" it shows correctly.

Error

Upvotes: 0

Views: 206

Answers (1)

Paul Lammertsma
Paul Lammertsma

Reputation: 38252

This is undoubtedly a layout problem. You can inspect your Layout Hierarchy using the ADT tool in Eclipse. Likely the TextView's width is being clamped by FrameLayout measuring only its immediate child dimensions, namely that of the ImageView.

To be honest, I'm puzzled why it's initial value is setting the dimension correctly and why the dimensions aren't determined by all children.

I would suggest replacing the inefficient construction of the FrameLayout with just a TextView with a background. Perhaps to narrow down the problem, you can simplify the layout around that view.

For example:

<TextView
    android:id="@+id/inviteNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginBottom="2dp"
    android:text="0"
    android:textColor="#88ffffff"
    android:textSize="12sp"
    android:background="@drawable/friend_invitations_background" />

Upvotes: 1

Related Questions