gigs gigs - 3 months ago 26
Android Question

Android - swap views in LinearLayout via xml

Here is my xml code, it has an ImageView which stands before LinearLayout id = "linearLayout2" :

<?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="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1"
android:background="@drawable/border"
android:paddingBottom="5dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/ic_swap"
android:layout_gravity="center_vertical" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2"
android:paddingBottom="5dp"
android:layout_marginRight="5dp">

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fromStationField"
android:completionThreshold="1"
android:hint="From"
android:textAlignment="textStart"
android:gravity="left|start"
android:layout_gravity="left" />

<AutoCompleteTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toStationField"
android:completionThreshold="1"
android:hint="To"
android:background="@android:color/transparent"
android:paddingRight="4dp"
android:paddingLeft="4dp" />

</LinearLayout>
</LinearLayout>
</RelativeLayout>


What I want to do is to make an ImageView stands after the LinearLayout id = "linearLayout2" like I show in an image bellow.

How to do this using only xml ?

enter image description here

Answer

In the layout below, I'm using negative margins in the ImageView and a positive margin in the LinearLayout container left of ImageView so that we don't need to use layout_weight property and can keep using match_parent. For this you need to fix the size of your ImageView. Check this code out:

<?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="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout1"
        android:background="@drawable/border"
        android:paddingBottom="5dp">



        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout2"
            android:layout_marginRight="55dp"
            android:paddingBottom="5dp">

            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/fromStationField"
                android:completionThreshold="1"
                android:hint="From"
                android:textAlignment="textStart"
                android:gravity="left|start"
                android:layout_gravity="left" />

            <AutoCompleteTextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/toStationField"
                android:completionThreshold="1"
                android:hint="To"
                android:background="@android:color/transparent"
                android:paddingRight="4dp"
                android:paddingLeft="4dp" />

        </LinearLayout>

        <ImageView
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/imageView"
            android:src="@drawable/ic_swap"
            android:layout_marginLeft="-55dp"
            android:layout_gravity="center_vertical" />

    </LinearLayout>
</RelativeLayout>
Comments