Zoran P
Zoran P

Reputation: 336

Prevent children from going out of screen bounds

I have a layout with textview and another layout inside it. I need the textview as wrap_content but stretchable to parent width without the layout that is next to it going off screen.

I tried with relative and LinearLayout and no matter what I do, the message_indicators layout just goes off screen when message_text stretches to match_parent. What i want here is that message_text stretches with content and message_indicators need to be right next to it without going off screen when message content increases.

Here is my code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10dp" >

<TextView
    android:id="@+id/message_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/message_indicators"
    android:autoLink="all"
    android:background="@drawable/text_bubble_left"
    android:clickable="true"
    android:paddingBottom="10dp"
    android:paddingLeft="20dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:text="@string/message_text_hint"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white" />

<LinearLayout
    android:id="@+id/message_indicators"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/message_text"
    android:layout_alignParentRight="true"
    android:orientation="vertical"
    android:paddingLeft="10dp" >

    <ImageView
        android:id="@+id/message_lock_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_lock"
        android:contentDescription="@string/message_locked_content_description"
        android:paddingBottom="5dp"/>

    <TextView
        android:id="@+id/message_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/message_time_hint"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textColor="@color/gray" />
</LinearLayout>

</RelativeLayout>

Here's the screenshot. Top message represents how it looks like with long messages, middle is short message with current layout and bottom is how it should look like for short messages:

Upvotes: 2

Views: 2053

Answers (2)

Zoran P
Zoran P

Reputation: 336

I have found a best way to do this.

Here is the solution.

<?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:orientation="vertical" >

<include
    android:id="@+id/message_section_header"
    layout="@layout/message_section_header" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <View
        android:id="@+id/message_bubble_arrow"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:background="@drawable/message_bubble_left_arrow"
        android:rotation="-90" />

    <TextView
        android:id="@+id/message_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autoLink="all"
        android:background="@drawable/message_bubble_left"
        android:clickable="true"
        android:padding="10dp"
        android:text="@string/message_text_hint"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/white" />

    <LinearLayout
        android:id="@+id/message_indicators"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:minWidth="60dp"
        android:orientation="vertical"
        android:overScrollMode="always"
        android:paddingLeft="10dp" >

        <ImageView
            android:id="@+id/message_lock_indicator"
            style="@style/Layout.Wrap"
            android:contentDescription="@string/message_locked_content_description"
            android:paddingBottom="5dp"
            android:src="@drawable/ic_lock" />

        <TextView
            android:id="@+id/message_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/message_time_hint"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/gray" />
    </LinearLayout>
</LinearLayout>

Upvotes: 1

Emmanuel Lee
Emmanuel Lee

Reputation: 11

Try android:layout_width="fill_parent" inside the TextView instead.

If that doesn't work, try moving the TextView block below the LinearLayout block.

Upvotes: 0

Related Questions