Android linear layout align center and right
Solution 1
try this one
<?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_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:layout_weight="1"
android:gravity="center"/>
<LinearLayout android:id="@+id/LinearLayout01"
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"
android:layout_width="wrap_content">
<Button
android:id="@+id/btnCalls"
android:layout_width="wrap_content"
android:drawableLeft="@drawable/icon"
android:gravity="right|center_vertical"
android:layout_height="wrap_content"
android:layout_margin="5dip"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Solution 2
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.
Solution 3
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>
Solution 4
you have used android:gravity="right|center_vertical"
in your textview. Use android:gravity="center"
.
Solution 5
Try to add empty View inside horizontal LinearLayout between element that you want see left and element that you want to see right, e.g.:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
erdomester
Updated on July 09, 2022Comments
-
erdomester almost 2 years
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.
-
erdomester over 12 yearsNothing. The button became wider.
-
erdomester over 12 yearsAlmost. The textview is in the center of the place left to the button, but not in the center of the width of the display.
-
banzai86 over 12 yearstry setting the width of the textview to fill_parent
-
Vladimir over 12 yearsI missed the part about textView being aligned at the center of display. How should text be aligned if it's wider than width of display minus width of a button?
-
Hiral Vadodaria over 12 yearsplease check the edited answer. it uses some hard fixes like paddingTop="5dip" but it might not be an issue for you.please adjust it as per the image you use in place of android icon.
-
erdomester over 12 yearsNo it doesn't. I figured it out that if the textview and the linearlayout are next to each other that means I can center align the text in the textview but not in the width of the display.
-
erdomester over 12 yearsI figured it out that if the textview and the linearlayout are next to each other that means I can center align the text in the textview but not in the width of the display. So with linear layout this problem cannot be solved.
-
erdomester over 12 yearsThe android:layout_marginLeft="150px" is a solution, however this is a hard fix I don't like. Not it is clear that the problem can only be worked out with relativelayout.
-
Vladimir over 12 yearsit leaves you with to options than - adding left marging to your textView the size of the button (or adding fake invisible button to the left of the text, the same size of the one on the right, which is actually a dirty hack) or using relative layout
-
erdomester over 12 yearsYes with this hacks it can be worked out. I solved it by using relative layout. Notwithstanding I accept your solution considering you were the nearest to the solution and for the idea of hacks.
-
Vladimir over 12 yearsYeah, I believe relative layout is the best solution. Thanks =)
-
Ramakrishna over 12 yearsbetter to use relative layout.If you don't have idea in relative layout i will provide the answer for above layout.
-
George Stocker about 11 yearsPlease do not post the exact same answer to multiple questions. If the questions are duplicates, please leave a comment on the question indicating that. If the questions are not duplicates, then please tailor your code and your answer to the question asked.
-
deimos1988 about 8 years"layout_centerInParent=true" worked for me to center my textview!
-
Harmen over 7 yearsThis is not a solution. Gravity works only on text contents. Don't know how this gets upvoted.
-
Anonymous over 6 yearsyou are adding both gravity, layout_gravity for LinearLayout01.Does it make any effect on view positioning?? @Vladimir