Search code examples
androidxmlandroid-layoutandroid-linearlayout

Android. Align views inside linear layout without push away from screen.


I am creating app with list of items which should look like as follows:

|-----------------------------------------------------|
|TextView--View--View--View---------------------------|
|-----------------------------------------------------|

|-----------------------------------------------------|
|TextViewTextViewTextView--View--View--View-----------|
|-----------------------------------------------------|

|-----------------------------------------------------|
|TextViewTextViewTextViewTextView..--View--View--View-|
|-----------------------------------------------------|

But in my case in third row TextView push away three views:

|--------------------------------------------------|
|TextViewTextViewTextViewTextViewTextView--View--Vi|
|--------------------------------------------------|

My xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

      <TextView
        android:id="@+id/TextViewSubject"
        android:text="SUBJECT"
        android:layout_weight="1"
        android:singleLine="true"
        android:ellipsize="end"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>


      <View
          android:background="#FF0000"
          android:layout_weight="0"
          android:layout_width="10dp"
          android:layout_height="10dp"/>
      <View
          android:layout_weight="0"
          android:background="#00FF00"
          android:layout_width="10dp"
          android:layout_height="10dp"/>
      <View
          android:layout_weight="0"
          android:background="#0000FF"
          android:layout_width="10dp"
          android:layout_height="10dp"/>

  </LinearLayout>

</LinearLayout>

Also I tried use RelativeLayout, but in that way 3 views always align right parent. I want that 3 views follows textview and not been pushed away from screen.

Is that possible to do it? Any help will be appreciated.


Solution

  • <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:text="largeTextlargeTextlargeTextlargeTextlargeTextlargeText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:ellipsize="end"
            android:maxLines="1"
            android:paddingRight="95dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/textView"
            android:orientation="horizontal">
        <View
            android:background="@android:color/black"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/button3" />
        <View
            android:background="@android:color/black"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/button2" />
        <View
            android:background="@android:color/black"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:id="@+id/button" />
        </LinearLayout>
    </RelativeLayout>