Nidhoegger
Nidhoegger

Reputation: 5232

Android: Alignment of Elements inside LinearLayout

I am currently building a custom Layout for an item in a ListView. First of all, here is the current XML file:

<?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="horizontal"
    android:gravity="right">

    <LinearLayout
    android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_weight="1">

        <TextView
        android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textGameName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textRuleName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textLanguageName"
            android:singleLine="true" />
    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/imageAction"
        android:src="@mipmap/action_required"
        android:scaleType="center"
        android:layout_gravity="right" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/imageNext"
        android:src="@mipmap/button_list_enter"
        android:scaleType="center"
        android:layout_gravity="right" />

</LinearLayout>

Basically, what i want: I want the two ImageViews always be as far on the right as possible and the LinearLayout with the 3 TextViews inside shall use the whole remaining Space. In the android studio editor I am able to get the desired result, but as soon as I run it on Android (mine is 4.4.2) the LinearLayout is not expanded and the buttons are next to the texts.

Upvotes: 0

Views: 2105

Answers (3)

Jignesh Jain
Jignesh Jain

Reputation: 1568

try this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right" >

    <LinearLayout
        android:id="@+id/linLeft"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/linRight"
        android:layout_weight="1"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textGameName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textRuleName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/textLanguageName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_gravity="left"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageAction"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:scaleType="center"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageNext"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:scaleType="center"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Charaf Eddine Mechalikh
Charaf Eddine Mechalikh

Reputation: 1248

does this help ? if it doesn't give me a picture of how the item should be

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="right">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_toLeftOf="@+id/linearLayout2"
        android:layout_toStartOf="@+id/linearLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textGameName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textRuleName"
            android:singleLine="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textLanguageName"
            android:singleLine="true" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:id="@+id/linearLayout2">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/imageNext"
            android:src="@drawable/ic_launcher"
            android:scaleType="center"
            android:layout_gravity="right"
            android:layout_weight="1" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/imageAction"
            android:src="@drawable/ic_launcher"
            android:scaleType="center"
            android:layout_weight="1" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Catalina
Catalina

Reputation: 2098

You can use this layout to achieve the desired result.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_toLeftOf="@+id/imageAction"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textGameName"
            android:singleLine="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textRuleName"
            android:singleLine="true" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Small Text"
            android:id="@+id/textLanguageName"
            android:singleLine="true" />
    </LinearLayout>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageAction"
        android:src="@mipmap/action_required"
        android:scaleType="center"
        android:layout_toLeftOf="@+id/imageNext" />
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/imageNext"
        android:src="@mipmap/button_list_enter"
        android:scaleType="center"
        android:layout_alignParentRight="true" />
</RelativeLayout>

Upvotes: 1

Related Questions