Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Layout Right Align

I have the following layout in place

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical" android:layout_width="fill_parent"     android:layout_height="fill_parent">      <LinearLayout android:orientation="vertical"         android:layout_width="fill_parent" android:layout_height="fill_parent"         android:layout_weight="1">           <WebView xmlns:android="http://schemas.android.com/apk/res/android"             android:id="@+id/webview" android:layout_width="fill_parent"             android:layout_height="fill_parent" />     </LinearLayout>      <LinearLayout android:orientation="horizontal"  android:layout_width="fill_parent" android:layout_height="fill_parent"  android:layout_weight="13">         <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">             <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">                     <ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" />             </LinearLayout>              <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">                 <ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />             </LinearLayout>          </LinearLayout>          <RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent"    android:layout_weight="1" >                 <ImageButton android:background="@null" android:id="@+id/special"   android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>         </RelativeLayout>         </LinearLayout>   </LinearLayout> 

For the purpose of this question, I am only concerned about the lower half of the layout. Right now it contains 3 imagebuttons. The first 2, I want right next to each other left aligned. The third one, I want to be aligned to the right side.

As is, the first 2 buttons are where I want them to be, but the 3rd is stubbernly staying left-aligned. How would I make it right aligned.

like image 457
Señor Reginold Francis Avatar asked Nov 29 '10 16:11

Señor Reginold Francis


People also ask

How do you move a linear layout to the right?

Try to change the layout_width to android:layout_width="match_parent" because gravity:"right" aligns the text inside the layout_width, and if you choose wrap content it does not have where to go, but if you choose match parent it can go to the right.

How do you right align text on android?

To right align text in TextView in Kotlin Android, set android:textAlignment attribute with the value “viewEnd” in layout file, or programmatically set the textAlignment property of the TextView object with View.

What is linear layout in android?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

What is RelativeLayout and LinearLayout in android?

Android Layout TypesLinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.


2 Answers

The layout is extremely inefficient and bloated. You don't need that many LinearLayouts. In fact you don't need any LinearLayout at all.

Use only one RelativeLayout. Like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="wrap_content">     <ImageButton android:background="@null"         android:id="@+id/back"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/back"         android:padding="10dip"         android:layout_alignParentLeft="true"/>     <ImageButton android:background="@null"         android:id="@+id/forward"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/forward"         android:padding="10dip"         android:layout_toRightOf="@id/back"/>     <ImageButton android:background="@null"         android:id="@+id/special"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:src="@drawable/barcode"         android:padding="10dip"         android:layout_alignParentRight="true"/> </RelativeLayout> 
like image 62
Octavian A. Damiean Avatar answered Sep 17 '22 10:09

Octavian A. Damiean


You can do all that by using just one RelativeLayout (which, btw, don't need android:orientation parameter). So, instead of having a LinearLayout, containing a bunch of stuff, you can do something like:

<RelativeLayout>     <ImageButton         android:layout_width="wrap_content"         android:id="@+id/the_first_one"         android:layout_alignParentLeft="true"/>     <ImageButton         android:layout_width="wrap_content"         android:layout_toRightOf="@+id/the_first_one"/>     <ImageButton         android:layout_width="wrap_content"         android:layout_alignParentRight="true"/> </RelativeLayout> 

As you noticed, there are some XML parameters missing. I was just showing the basic parameters you had to put. You can complete the rest.

like image 26
Cristian Avatar answered Sep 20 '22 10:09

Cristian