Android: Edittext with gravity center not working on device

岁酱吖の 提交于 2019-11-29 01:26:15

I finally found the solution to get it working. This EditText works for me: (Tested on Sony Ericsson X10 and HTC Wildfire)

<EditText 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="numberDecimal" 
    android:digits="0123456789.,"
    android:ellipsize="start" 
    android:maxLines="1"
    android:gravity="center"
    android:hint="Some hint text"
></EditText>

Hope it's not too late. I came across this problem and I don't know whether it is the same for every device but it appears EditText by default has bottom padding.

My EditText text value always appeared to be slightly above center despite setting gravity to be CENTER so I additionally set:

myEditText.setPadding(0, 0, 0, 0);

This fixed it for me, now my EditText is perfectly centered :D

i think , you should set the gravity of the parentLayout of your EditText , to center

EDIT :

<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal" 
    android:id="@+id/linearLayout">

   <EditText 
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:inputType="numberDecimal" 
       android:ellipsize="end" 
       android:maxLines="1"
       android:hint="Some hint text"/>
</LinearLayout>

I tried a combination of gravities :

android:gravity="top|center_horizontal"

My previous attempt was to nest the EditText inside a RelativeLayout and set it to center but it did not look that good , this way works best for me.

  • In XML file add gravity CENTER_HORIZONTAL for set hint to center.CURSORVISIBLE=false so cursor not visible when click first time
  • In java use addTextChangedListener, when user add text that time change gravity to left and also visible cursor.

XML :

<EditText android:id="@+id/email"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="EMAIL"
  android:textStyle="bold"
  android:gravity="center_horizontal"
  android:inputType="textEmailAddress"
  android:layout_margin="10dp"
  android:cursorVisible="false" />

JAVA:

email.addTextChangedListener(new TextWatcher() 

{

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    // position the text type in the left top corner
                    email.setGravity(Gravity.LEFT | Gravity.TOP);
                    email.setCursorVisible(true);
                }
                else
                {
                    // no text entered. Center the hint text.
                    email.setGravity(Gravity.CENTER);
                    email.setCursorVisible(false);
                }
            }
        });

Instead of adding the gravity in style tag add that inside your Layout file that will work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!