This is what I have:
<?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:
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.
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.
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.
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.
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).
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With