18

In Android I'm using a single line edittext with gravity set to center. In the Emulator this works great both the hint and a blinking cursor shows up. When testing on device (Xperia X10) neither the hint text nor the blinking cursor shows up. The blinking cursor only shows if I enter some text into the edittext.

This is my edittext, can anyone see if something is missing?

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

What I want:

This is what I want

What I get with the above code (empty edittext with no cursor):

The edittext doesn't show the hint text and have no blinking cursor

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Martin
  • 7,190
  • 9
  • 40
  • 48
  • How were you able to get the style to be like that? – Si8 Jan 18 '14 at 03:04
  • possible duplicate of [hint and textview with right gravity and a singleline](http://stackoverflow.com/questions/4649183/hint-and-textview-with-right-gravity-and-a-singleline) – Andrew T. Aug 26 '14 at 08:52

6 Answers6

46

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>
Martin
  • 7,190
  • 9
  • 40
  • 48
6

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

Zhang
  • 11,549
  • 7
  • 57
  • 87
  • 1
    I just killed an hour working my way through this stupid issue. This worked, thanks! I recommend myEditText.setPadding(myEditText.getPaddingLeft(), 0, myEditText.getPaddingRight(), 0); to maintain the left and right padding. – aikmanr Dec 16 '15 at 21:20
4

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>
Houcine
  • 24,001
  • 13
  • 56
  • 83
  • Thanks, I tested that but no difference. Please see the updated question. – Martin May 26 '11 at 09:50
  • see my edit too , i mean not to set the gravity of you editText , i said the gravity of his parentLayout , – Houcine May 26 '11 at 09:57
  • Yes I see, but that didn't make any difference. Is this working when you test on device? – Martin May 26 '11 at 10:03
  • remove the android:layout_gravity="center" and see whetherit works – Karthik May 26 '11 at 10:07
  • @Martin , i will test it now in the emulator and in my device , you want it center Horizontal and vertical ? or just Horizontal ? , see my edit : ( – Houcine May 26 '11 at 10:12
  • Thanks but when testing your solution, I will get something like in this image: http://oi52.tinypic.com/16ifz20.jpg. So when you start typing the text will not be in center of the textfield. What I need is something like in the updated question. – Martin May 26 '11 at 12:26
1

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.

Valer Dragos
  • 416
  • 6
  • 6
0
  • 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);
                }
            }
        });
-1

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

Karthik
  • 391
  • 4
  • 14