0

I have a Horizontal Layout and I have some views inside it. I want some to start from the left and others to start in the right, but I can't manage to do it. I tried several Gravity configurations but they don't do anything.

That's the case I have:

enter image description here

I want the Flag to be in the right and the Time to be in the left, as pointed by the arrows. I will add some more flags later.

Could anyone help me out with this? Thanks :D

EDIT:

XML so far:

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:id="@+id/hlTopBar"
        android:background="#e6262626"
        android:gravity="center_vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/default_time_date_string"
            android:id="@+id/tvTime"
            android:textStyle="bold"
            android:paddingLeft="10dp"
            android:gravity="left" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibUSA"
            android:src="@drawable/united_states_flag"
            android:background="@android:color/transparent"
            android:adjustViewBounds="true" />
    </LinearLayout>
Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173

3 Answers3

1

Instead of using a horizontal layout use a Relative layout

example:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Time" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Image" />

</RelativeLayout>

result:

enter image description here

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
1

android:layout_gravity works in the direction opposite the orientation of the LinearLayout – the children of a vertical LinearLayout can use android:layout_gravity to control their positioning horizontally (left or right), but not vertically. In the same way children of horizontal LinearLayout can use android:layout_gravity to control their positioning vertically (top or bottom) but not horizontally. As you are using Horizontal LinearLayout you can use android:layout_gravity to position children either top or bottom.For your purpose it is better to go with RelativeLayout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView
        .........
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        />

    <ImageView
        .......
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        />

</RelativeLayout>
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
1

The RelativeLayout is a good answer. If, however, you REALLY want to do it with a LinearLayout, try putting an empty TextView in the middle, with width=0 and weight=1.

This empty view will automatically try to fill up however much space isn't taken up by the other views.

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="30dp"
        android:id="@+id/hlTopBar"
        android:background="#e6262626"
        android:gravity="center_vertical">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/default_time_date_string"
            android:id="@+id/tvTime"
            android:textStyle="bold"
            android:paddingLeft="10dp" />

        <TextView
            android:id="@+id/spacer"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ibUSA"
            android:src="@drawable/united_states_flag"
            android:background="@android:color/transparent"
            android:adjustViewBounds="true" />
    </LinearLayout>
AJ Macdonald
  • 436
  • 2
  • 5