Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android linear layout align center and right

This is what I have:

enter image description here

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#FFFFFF">
    <LinearLayout android:id="@+id/LinearLayout01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:background="#E30000"
        android:layout_gravity="center_horizontal|center_vertical"
        android:gravity="center_horizontal|center_vertical">
        <TextView 
            android:id="@+id/TextView00"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"
            android:textSize="20px"
            android:textStyle="bold"
            android:text="Something"
            android:gravity="right|center_vertical"
            />
        <LinearLayout android:id="@+id/LinearLayout01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="horizontal"
            android:gravity="right|center_vertical"
            android:layout_gravity="center_vertical"
            android:background="#E30000">
            <Button 
                android:id="@+id/btnCalls"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:drawableLeft="@drawable/icon"
                android:gravity="right|center_vertical"
            />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

And I want this:

enter image description here

So horizontal center align the text and vertical center align the button.

What am I missing? I haven't tried RelativeLayout and I would prefer LinearLayout to work this out.

like image 854
erdomester Avatar asked Oct 06 '11 07:10

erdomester


People also ask

How do you center align in linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

What's the difference between LinearLayout and RelativeLayout?

LinearLayout : 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.

How do I change the position of a button in linear layout?

You can set the android:layout_weight='1' and both buttons will share the screen equally(side by side) or if you want the extra space between them, you can place a button each in a linear layout and set the android:layout_gravity to left and right for each. Save this answer.

What is the difference between linear layout and constraint layout in android?

ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout).


2 Answers

android:gravity="right|center_vertical"

aligns your TextView to the right. How about center_horizontal?

And your LinearLayouts have the same ID, if there is not more code, then I would delete one of them.

like image 72
banzai86 Avatar answered Sep 24 '22 02:09

banzai86


I found none of the answers work exactly as the OP intended, but the RelativeLayout solution was the closest one. Except that the TextView wasn't centered. But, with layout_centerInParent=true, works like a charm:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView 
         android:id="@+id/TextView00"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:textColor="#FFFFFF"
         android:textSize="20px"
         android:textStyle="bold"
         android:text="Something"
         android:layout_centerInParent="true" />

    <Button 
         android:id="@+id/btnCalls"
         android:drawableLeft="@drawable/icon"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"/>
</RelativeLayout>
like image 34
grago Avatar answered Sep 25 '22 02:09

grago