Search code examples
androidandroid-layoutandroid-linearlayout

Linear Layout shorten text in textview


I have a status bar with 6 elements in a row in a linear layout. This looks ok on a newer display with high resolution. But when I start the app on my old smartphone only the first text of the Textview in the status bar is shown. The imageViews are cut off and the status bar gets higher without extra content.

What I want is, that if the content does not fit, the text in the first textView is shorten, so that the ImageViews can all be shown. Is this possible?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="2dp"
    android:gravity="right|center_vertical"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/statusbar_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:text="Text der Statusbar" />

    <TextView
        android:id="@+id/statusbar_text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp"
        android:text="TEXT1" />

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

    <ImageView
        android:id="@+id/some_state1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp" 
        android:src="@drawable/stat_some_state1" />

    <ImageView
        android:id="@+id/some_state2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp" 
        android:src="@drawable/stat_some_state2" />

    <ImageView
        android:id="@+id/some_state3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="5dp" 
        android:src="@drawable/stat_some_state3" />

</LinearLayout>

Solution

  • Add weight for all elements . If weights are equal the Layout will be divided equally. if weight is twice the others then the division will be twice. if orientation is vertical and you had given weight then height should be 0dp and if it is horizontal , width should be 0dp

    ?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="2dp"
        android:gravity="right|center_vertical"
        android:orientation="horizontal" >
    
        <TextView
            android:id="@+id/statusbar_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:layout_weight="1"
            android:text="Text der Statusbar" />
    
        <TextView
            android:id="@+id/statusbar_text1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp"
            android:text="TEXT1" />
    
        <ImageView
            android:id="@+id/some_state0"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/stat_some_state0" />
    
        <ImageView
            android:id="@+id/some_state1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp" 
            android:src="@drawable/stat_some_state1" />
    
        <ImageView
            android:id="@+id/some_state2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp" 
            android:src="@drawable/stat_some_state2" />
    
        <ImageView
            android:id="@+id/some_state3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="5dp" 
            android:src="@drawable/stat_some_state3" />
    
    </LinearLayout>