Hint not working in EditText on Android

2.1k Views Asked by At

I am using Editbox in which I have used hints. but when I run application, hints do not show. I have tried setting hints programmatically too, but it doesn't work.

Here is my code:

            <EditText
                android:id="@+id/mobile_registration_txtDomain"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dip"
                android:hint="@string/hint_domain"
                android:lines="1"
                android:background="@drawable/edittxt_drwable"
                android:textSize="14sp"
                android:textColor="#808080" />

here is edittxt_drwable.xml code---- which is stored in drawables, used to change color of edittext when focussed and lost focus.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_window_focused="false"
        android:state_enabled="true"
        android:drawable="@drawable/new_border" />

    <item
        android:state_window_focused="false"
        android:state_enabled="false"
        android:drawable="@drawable/new_border" />

    <item
        android:state_pressed="true"
        android:drawable="@drawable/focussed_border" />

    <item
        android:state_enabled="true"
        android:state_focused="true"
        android:drawable="@drawable/focussed_border" />

    <item
        android:state_enabled="true"
        android:drawable="@drawable/new_border" />

    <item
        android:state_focused="true"
        android:drawable="@drawable/new_border" />

    <item
        android:drawable="@drawable/new_border" />

</selector>

here new_border.xml code which gives black border to edittext when it is unclicked.

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:thickness="0dp"
         android:shape="rectangle">

    <stroke android:width="2dp"
           android:color="#808080"/> 

    <corners android:radius="5dp"    
         />

    <padding android:left="10dp"
           android:top="5dp"   
           android:right="10dp"
           android:bottom="5dp"/>

    </shape> 

here focussed_border.xml code-- which gives orange border to edittxt when edittext is focussed.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:thickness="0dp" 
    android:shape="rectangle">

<stroke android:width="2dp"
       android:color="#FF8C00"/> 

<corners android:radius="5dp"    
     />

<padding android:left="10dp"
       android:top="5dp"   
       android:right="10dp"
       android:bottom="5dp"/>

</shape>
3

There are 3 best solutions below

3
On BEST ANSWER

Try this, Add textColorHint attribute for your EditText

android:textColorHint="#FFFFFF"
1
On

There might be problem in your string.xml. Did you define hint_domain properly in strings.xml. Can you please show your strings.xml file?

1
On

Try changing

android:inputType="text"

with android:singleLine="true"

or do

android:gravity="right" or android:gravity="left"

once using android:ellipsize="end" fixed it for me Weird bug.

You can use

android:ellipsize="end"

android:maxLines="1"

to show your hint text in single line but you wont be able to see the whole text because android:ellipsize="end" truncates the text at the end.