Max Polkovnik
Max Polkovnik

Reputation: 319

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.

Upvotes: 1

Views: 1012

Answers (4)

Dokodyaga
Dokodyaga

Reputation: 26

<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>

Upvotes: 1

F.Dindar
F.Dindar

Reputation: 52

try this, hope this is what you want. I just removed weight lines. Good luck

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

        <TextView
            android:id="@+id/TextViewSubject"
            android:text="SUBJECT"
            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_width="10dp"
            android:layout_height="10dp"/>
        <View
            android:background="#00FF00"
            android:layout_width="10dp"
            android:layout_height="10dp"/>
        <View
            android:background="#0000FF"
            android:layout_width="10dp"
            android:layout_height="10dp"/>

    </LinearLayout>

</LinearLayout>

Upvotes: 0

Ranjan
Ranjan

Reputation: 1356

try using weight in linear layout

here,I have fixed it, you can change the weight to increase the size of button

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

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

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


    <View
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="10dp"
        android:background="#FF0000" />

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

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

</LinearLayout>

Upvotes: 0

Omar Mneimneh
Omar Mneimneh

Reputation: 13

In the second linearLayout add android:weightSum=4 and in each children View make android:layout_weight=1 and make sure the android:layout_width is set to 0dp in child Views

Upvotes: 0

Related Questions