michael
michael

Reputation: 110570

How to layout view right aligned and bottom of an LinearLayout

I am trying to layout 1 textview (upText) left aligned and 1 textview (downText) and an image view (image) both on the same line and right aligned.

how can I do that? I tried that, but both 'textview' and image view at left aligned.

        <TextView
            android:id="@+id/uptext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"/>
        <TextView
            android:id="@+id/downtext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:gravity="right|bottom"/>
        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right|bottom"/>
    </LinearLayout>

Upvotes: 10

Views: 31849

Answers (3)

Mimu Saha Tishan
Mimu Saha Tishan

Reputation: 2623

Using RelativeLayout

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hi"
        android:textStyle="bold|italic"
        android:gravity="right"/>
    <TextView
        android:id="@+id/text2"
        android:layout_below="@id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="All"
        android:gravity="right"/>

</RelativeLayout>

Upvotes: 0

Corey
Corey

Reputation: 445

I realize this post is a bit old but just in case someone comes across this in their search for clarity;

The parent linear layout is where gravity needs to be specified for the child to align with the desired behavior which is why the above posts are explaining that linear layout is not possible for two separate behaviors to occur since a child cannot decide where to align itself within a linear layout.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="200dp"
android:layout_height="50dp"
android:gravity="bottom|right">

<TextView
    android:layout_width="40dp"
    android:layout_height="20dp" 
    android:text="test"/></LinearLayout>

It should also be said that the parent linear layout must have a defined size and not be wrap-content or this will not work since wrap content implies that there will be no extra space in the layout for positioning, so at least 'match-parent' for width and height is necessary as well as having a parent with a greater size than wrap-content for the child linear layout itself.

Hope this helps.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006744

Don't use a LinearLayout. Use a RelativeLayout, with

  • your first TextView set with android:layout_alignParentLeft="true"
  • your second TextView set with android:layout_alignParentBottom="true" and android:layout_alignParentRight="true"
  • something similar for your ImageView, which presently looks like it is going to overlap the second TextView

Upvotes: 24

Related Questions