Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android LinearLayout Alignment

Being new to android I am still learning the intricacies of layouts. I am trying to create a simple bar on top of a map. For the most part this works fine.

My issue is that I want everything to be right aligned except for the Button which I want left aligned. I have tried quite a few combinations and am unable to get desired layout.

This is beginning to make me believe my structure as a whole is not correct. This seems like there should be an easy fix. What am I missing??

   <LinearLayout
        android:id="@+id/transparent_panel_hud"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right">  
        <Button
            android:text="View"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/arrow_down"
            android:textSize="10sp"
            android:drawablePadding="3dp"/>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingRight="15dp" >

            <TextView
                android:id="@+id/latitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:gravity="right"
                android:text="@string/default_latitude"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/longitude"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:text="@string/default_longitude"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingRight="10dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/speed"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/speed"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/default_speed"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingRight="10dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/heading"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="@string/default_heading"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>![screenie][1]
like image 247
J. Scarbrough Avatar asked Jan 18 '23 19:01

J. Scarbrough


1 Answers

Do the following changes to your XML layout, you will get the output as you mentioned. Try this.

Remove the line android:gravity="right" in LinearLayout with id=transparent_panel_hud

Keep your Button in a LinearLayout as below.

      <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="left" >
            <Button ... as you like />
      </LinearLayout>

Keep your remaing 3 vertical LinearLayouts in a LinearLayout as below.

      <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="right" >
            <LinearLayout vertical 1 ... as you like />
            <LinearLayout vertical 2 ... as you like />
            <LinearLayout vertical 3 ... as you like />
      </LinearLayout>

I tested above changes to your code, its working. You too check it and let me know the result.

like image 168
Yugandhar Babu Avatar answered Feb 01 '23 11:02

Yugandhar Babu