Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView showing only one character when gravity style is applied

I have a table with 3 rows of numbers (summary information). When the TextViews are styled with their default gravity, the text shows correctly.

enter image description here

But when they're styled with <item name="android:gravity">right</item>, each TextView shows only the first character of what it should.

enter image description here

This even includes the column headers - not shown here - which I'm not even changing.

I would like the table values to be right-aligned to be consistent with the rest of the app and I'm stumped as to why the values in the table would be behaving like this.

Here's my style (for when it doesn't work):

<style name="summary_values">
    <item name="android:textSize">@dimen/font_size</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">@color/background</item>
    <item name="android:gravity">right</item>
</style>

Here's the XML for one of the cells (they're all basically the same)

<TextView 
    android:id="@+id/value1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="@integer/table_weight_percent_value"
    style="@style/summary_values"/>

Edit

The truncating happens even when the gravity is set to left or center.

Edit 2

I've removed the <item name="android:gravity">right</item> line from the style altogether and it's still truncating sometimes, depending on the values. I've stepped through my code and the values in the code are correct.

Edit 3

After removing all styling except the font colour, the truncating still happens, but I haven't figured out when. If I change numbers in the input table, the summary values are sometimes truncated and sometimes not.

Here is a row from the table. All the rows are similar, apart from id changes.

<TableRow
    android:padding="@dimen/pad_12">
    <TextView 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="@integer/table_weight_percent_label"
        android:text="@string/cho"
        style="@style/label_light"/>
    <TextView
        android:id="@+id/percent_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="@integer/table_weight_percent_value"
        style="@style/summary_values"/>
    <TextView 
        android:id="@+id/val1a"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="@integer/table_weight_percent_value"
        style="@style/summary_values"/>
    <TextView 
        android:id="@+id/val1b"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="@integer/table_weight_percent_value"
        style="@style/summary_values"/>
</TableRow>

Edit 4

After trying a number of things, I changed the containing TableView to a LinearLayout with horizontal orientation. I even left the TableRow views as they were (just adding in layout_width and layout_height). Amazingly, it works now.

To describe the odd behaviour a little more: when it was showing only the first character, if I opened the soft keyboard and closed it again, the table would display correctly, until I changed one of the EditText values. It still behaved in this strange manner even when I removed 2 of the columns from the table, so there was plenty of space for the views.

like image 533
Mike T Avatar asked Jun 17 '14 18:06

Mike T


People also ask

Can you have multiple styles inside TextView?

There are many ways to make multiple styles inside android TextView and in this example I have made multiple style with three different ways using HTML tags and Html. fromHtml().

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

What is the difference between TextView and plain text in android Studio?

Textview : should be used for uneditable text which user wants to read but not to manipulate. e.g. News, Articles, Blogs. Plain text/ Edittext : should be used for taking user input in alphanumeric form. e.g. UserId, Comments.


1 Answers

After trying a number of things, I changed the containing TableView to a LinearLayout with horizontal orientation. I even left the TableRow views as they were (just adding in layout_width and layout_height). Amazingly, it works now.

I have no idea why it didn't work or why it works now. I'd really appreciate if anyone could give some insight into it.

like image 55
Mike T Avatar answered Nov 03 '22 08:11

Mike T