idish
idish

Reputation: 3270

Aligning my views to the right

Alright, what I'm trying to do is: setting an imageview to be on the right and the 3 textviews will be located on the left relative to the image. With the following XML code, the imageview is on the left and the 3 textviews are located in the right of the imageview.

Here's the look of it now: enter image description here

As I said, I want the image to be aligned on the right side, and the 3 textviews on the left of the image.

So here's the current xml: http://pastebin.com/r68S1QKv

If I change the LinearLayout to RelativeLayout (so I can use alignParentRight = true), the textviews stack on each other and are not displayed vertically.

Suggestions, please?

EDIT: Looks like I haven't clarify the wanted look, here's an illustration of what I want: enter image description here

Upvotes: 1

Views: 105

Answers (2)

Pavel Dudka
Pavel Dudka

Reputation: 20944

What you can do - is to make outer RelativeLayout, make you image alignParentRight=true, put text views in a separate linear layout (with vertical orientation), align your LinearLayout with text views to the left edge of the image view (layout_toLeftOf="@+id/img_id").

Here is what I manage to do following this approach:

enter image description here

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

    <LinearLayout 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_toLeftOf="@+id/img"
        android:orientation="vertical"
        android:id="@+id/text_container">

        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="text1"/>
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="text2"/>
        <TextView 
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="text3"/>

    </LinearLayout>

    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignParentRight="true"
        android:src="@drawable/icon"
        android:id="@+id/img"/>

</RelativeLayout>

Upvotes: 1

Rishabh Agrawal
Rishabh Agrawal

Reputation: 901

Use This Code.Use RelativeLayout as parent & assign android:layout_alignParentRight="true" to his child this set your all view to right side.

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >

    <LinearLayout
        android:layout_width="wrap_content"
    android:layout_marginRight="20dp"

        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:orientation="horizontal" >


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/row_title"
                style="bold"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hey1" />

            <TextView
                android:id="@+id/row_subtitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hey2" />

            <TextView
                android:id="@+id/row_postcode_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hey3" />
        </LinearLayout>


        <ImageView
            android:id="@+id/icon"
            android:layout_marginRight="20dp"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</RelativeLayout>

Upvotes: 1

Related Questions