Android TextView text will not center

I'm trying to do a PIN entry operation, but one little thing does not work for me. I cannot get the text in the center of the TextView at the top of the screen:

Pin screenshots

How it will work, when a user enters a PIN, I put an asterisk in each text file for visible feedback. The problem is that I want the star to be focused. I tried layout_gravity = "center", but that doesn't make any difference. Here is 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 have this android: text = "X" there for the first text view so I can see if it works. When I finish adjusting the layout, it will disappear. What simple thing am I missing?

+8
android layout textview
source share
5 answers

use android:gravity="center"

although I don't see any EditText element in your code. Are you sure you posted the correct code?

+32
source share
 <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" refers to this View position in it parent. android:gravity="center" refers to where this view content is located.

+11
source share

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

+1
source share

To center the text, use android:gravity in 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
source share

This can also be done programmatically, of course:

 textView.setGravity(Gravity.CENTER); 
0
source share

All Articles