marco
marco

Reputation: 1752

Android: how to programmatically get layout's width

Well, the title is not really exhaustive but it's how I would fix the problem. If there are other solutions, they are welcome.

I'm testing my app in 2 emulators and this is the output:

1024x600 mdpi:

480x800 hdpi:

It's a row of an ExpandableListView and contains a title on the left and a preview on the right. The preview is supposed to be cut if too long. The problem is that in the first case it's too short and in the second one too long.

This is the layout of the row:

<?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="wrap_content"
    android:background="@drawable/elv_title_1_selector"
    android:gravity="fill_horizontal|center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/evl_grp_indicator"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:contentDescription="indDown"
        android:src="@drawable/indicator_selector" />

    <Button
        android:id="@+id/cbWarning"
        android:layout_width="16dp"
        android:layout_height="16dp"
        android:background="@drawable/warning_selector"
        android:clickable="false"
        android:enabled="false"
        android:focusable="false"
        android:textColor="@color/LogoButtonText"/>

    <TextView
        android:id="@+id/txtGroupName"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:enabled="true"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:text=""
        android:textColor="@drawable/elv_title_1_selector"
        android:textSize="16sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtPreview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="40dp"
        android:gravity="center_vertical"
        android:padding="5dp"
        android:textSize="12sp" />
</LinearLayout>

I've set the text size of the preview to 12sp and I programmatically cut the text if longer than n chars. What I need is to cut it larger on the first case and shorter on the second.

Any ideas how to ?

Upvotes: 0

Views: 159

Answers (2)

Kartheek
Kartheek

Reputation: 7214

To fix this issue add an attribute android:singleLine="true"and android:ellipsize="end" for the textview.

<TextView
        android:id="@+id/txtPreview"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:enabled="true"
        android:paddingBottom="15dp"
        android:paddingTop="15dp"
        android:text=""
        android:textColor="@drawable/elv_title_1_selector"
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="16sp"
        android:textStyle="bold" />

Upvotes: 1

N J
N J

Reputation: 27545

First of all add android:singleLine="true" and android:ellipsize="end" in your second TextView

<TextView 
...
 android:singleLine="true"
android:ellipsize="end"
...
/>

for Managing the text size on different device

create folder

values
values-v21
values-sw600

and add dimes.xml and specify your textsize for different resolutions

Upvotes: 1

Related Questions