DaedalusAlpha DaedalusAlpha - 1 month ago 20
Android Question

Multiline textview getting cut off in height

I have a

TextView
next to an
ImageButton
in a horizontal
LinearLayout
which is then wrapped in a vertical
LinearLayout
. To get the button to show up I had to set a
layout_weight
on the text view and button with the button being the stronger of the two. However this seems to make the button decide the height of both of them, even if the text needs to be higher. Is there any way to get around this?

<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_width="wrap_content">


<LinearLayout
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="@color/text_color"
android:textSize="@dimen/medium_font"
android:layout_gravity="center"
android:text="A long text that wraps correctly but then gets cut off"
android:layout_weight="1" />

<ImageButton
android:contentDescription="@string/share_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/share_button"
android:padding="5dp"
android:layout_weight="0"
android:layout_gravity="center"
android:scaleType="center"
android:background="@android:color/transparent"
android:src="@mipmap/ic_menu_share_holo_dark"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/button_background"
android:id="@+id/distances_parent">
<Spinner
android:layout_margin="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/distance_labels"
android:id="@+id/distances" />
</LinearLayout>

<Space
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp" />

<FrameLayout
android:background="@color/back_end"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

...
</FrameLayout>
</LinearLayout>


This is what it looks like:

Cut off text

As you can see the button decides the height, not the text as should be.

This is what I want it to look like:

Text not cut off

Answer

You can use below code.

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

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_gravity="center"
        android:layout_toLeftOf="@+id/share_button"
        android:text="A long text that wraps correctly but then gets cut off"
        android:textColor="@color/text_color"
        android:textSize="@dimen/medium_font"" />

    <ImageButton
        android:id="@+id/share_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_weight="0"
        android:background="@android:color/transparent"
        android:contentDescription="imagebutton"
        android:padding="5dp"
        android:scaleType="center"
        android:background="@android:color/transparent"
        android:src="@mipmap/ic_menu_share_holo_dark"/>

    <Spinner
        android:id="@+id/distances"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text"
        android:layout_margin="5dp"
        android:entries="@array/distance_labels" />
</RelativeLayout>

If your TextView contains more text. The TextView's height will increase according to that ImageButton and spinner will adjust. Try and check. I hope it will help you. All the best.

Comments