Android linear layout align center and right

50,713

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>
Share:
50,713
erdomester
Author by

erdomester

Updated on July 09, 2022

Comments

  • erdomester
    erdomester almost 2 years

    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.

  • erdomester
    erdomester over 12 years
    Nothing. The button became wider.
  • erdomester
    erdomester over 12 years
    Almost. 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
    banzai86 over 12 years
    try setting the width of the textview to fill_parent
  • Vladimir
    Vladimir over 12 years
    I 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
    Hiral Vadodaria over 12 years
    please 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
    erdomester over 12 years
    No 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
    erdomester over 12 years
    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. So with linear layout this problem cannot be solved.
  • erdomester
    erdomester over 12 years
    The 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
    Vladimir over 12 years
    it 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
    erdomester over 12 years
    Yes 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
    Vladimir over 12 years
    Yeah, I believe relative layout is the best solution. Thanks =)
  • Ramakrishna
    Ramakrishna over 12 years
    better to use relative layout.If you don't have idea in relative layout i will provide the answer for above layout.
  • George Stocker
    George Stocker about 11 years
    Please 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
    deimos1988 about 8 years
    "layout_centerInParent=true" worked for me to center my textview!
  • Harmen
    Harmen over 7 years
    This is not a solution. Gravity works only on text contents. Don't know how this gets upvoted.
  • Anonymous
    Anonymous over 6 years
    you are adding both gravity, layout_gravity for LinearLayout01.Does it make any effect on view positioning?? @Vladimir