12

I am trying to make a PIN entry activity, but one little thing is not quite working for me. I can't get the text to center in the TextView's at the top of the screenshot here:

Pin Screenshot

The way it will work is when the user is entering a PIN, I will place a star in each TextView for visible feedback. The problem is, I want the star to be centered. I have tried layout_gravity="center", but it doesn't make a difference. Here's my current layout:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
     <TextView 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_height="wrap_content" 
     android:id="@+id/textView1" 
     android:text="PIN Required" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginBottom="12sp"
     ></TextView>
     <LinearLayout 
     android:layout_width="match_parent" 
     android:id="@+id/linearLayout1" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="12sp" 
     android:layout_gravity="center_horizontal"
     >
         <TextView 
       android:id="@+id/text1" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="48sp" 
       android:background="#FFFFFF" 
       android:text="X" 
       android:layout_gravity="center"
       ></TextView>
         <TextView 
       android:id="@+id/text2" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="2sp" 
       android:background="#FFFFFF"
       ></TextView>
         <TextView 
       android:id="@+id/text3" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="2sp" 
       android:background="#FFFFFF"
       ></TextView>
         <TextView 
       android:id="@+id/text4" 
       android:layout_height="32sp" 
       android:layout_width="48sp" 
       android:layout_weight="1" 
       android:layout_marginRight="48sp" 
       android:layout_marginLeft="2sp" 
       android:background="#FFFFFF"
       ></TextView>
     </LinearLayout>
     <LinearLayout 
     android:layout_width="match_parent" 
     android:id="@+id/linearLayout2" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="12sp"
     >
         <Button 
       android:text="1" 
       android:textColor="#FFFFFF" 
       android:id="@+id/button1" 
       android:layout_height="32sp" 
       android:layout_width="64sp" 
       android:layout_weight="1" 
       android:editable="false" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="48sp" 
       android:background="@drawable/action_background_gradient" 
       android:onClick="ButtonClicked"
       ></Button>
         <Button 
       android:text="2" 
       android:textColor="#FFFFFF" 
       android:id="@+id/button2" 
       android:layout_height="32sp" 
       android:layout_width="64sp" 
       android:layout_weight="1" 
       android:editable="false" 
       android:layout_marginRight="2sp" 
       android:layout_marginLeft="2sp" 
       android:background="@drawable/action_background_gradient" 
       android:onClick="ButtonClicked"
       ></Button>
         <Button 
       android:text="3" 
       android:textColor="#FFFFFF" 
       android:id="@+id/button3" 
       android:layout_height="32sp" 
       android:layout_width="64sp" 
       android:layout_weight="1" 
       android:editable="false" 
       android:layout_marginRight="48sp" 
       android:layout_marginLeft="2sp" 
       android:background="@drawable/action_background_gradient" 
       android:onClick="ButtonClicked"
       ></Button>
     </LinearLayout>

     <!--... and so on for the rest of the buttons ... -->
 </LinearLayout>

I've got that android:text="X" in there for the first textview just so I can see if it is working. When I am done tweaking the layout, that will go away. What simple thing am I missing?

5 Answers 5

44

use android:gravity="center"

although I'm not seeing any EditText element in your code. You sure you posted the right code?

3
  • 1
    That's because I am a knucklehead and I meant to say TextView. Working on two things at once. But I do have the gravity="center" in there and it isn't working.
    – MrGibbage
    Apr 4, 2012 at 16:03
  • 8
    Doh! I have layout_gravity, rather than just gravity. Sigh... Thanks!
    – MrGibbage
    Apr 4, 2012 at 16:05
  • 2
    Aaah! How many times will I make this same mistake!! I should tattoo layout_^H^H^H^H^H^H^Hgravity on my forehead.
    – Synesso
    Jan 15, 2015 at 0:24
15
<TextView 
   android:id="@+id/text1" 
   android:layout_height="32sp" 
   android:layout_width="48sp" 
   android:layout_weight="1" 
   android:layout_marginRight="2sp" 
   android:layout_marginLeft="48sp" 
   android:background="#FFFFFF" 
   android:text="X" 
   android:layout_gravity="center"
   android:gravity="center"
/>

android:layout_gravity="center" relates to this View's position in it's parent. android:gravity="center" relates to where this View's contents are positioned.

1
  • For my particular activity, setting android:gravity alone to center did not resolve the problem, I also needed to set android:layout_gravity to center +1 Aug 16, 2017 at 15:09
1

Instead of android:layout_gravity in text view use android:gravity="center_horizontal"

0

To centre the text, use android:gravity on the TextView, not layout_gravity:

<TextView 
   android:id="@+id/text1" 
   android:layout_height="32sp" 
   android:layout_width="48sp" 
   android:layout_weight="1" 
   android:layout_marginRight="2sp" 
   android:layout_marginLeft="48sp" 
   android:background="#FFFFFF" 
   android:text="X" 
   android:gravity="center" />
0

This can be done programmatically as well, of-course:

textView.setGravity(Gravity.CENTER);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.