Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EditText: Center hint but have entered text start from top

Tags:

android

Hi i have a edit text with the hint, i made the hint as middle using android:gravity:"center". when i start typing from in the edittext the typing is starting from the middle, how to make the typing start from leftcorner while still hint is centered

<EditText
        android:id="@+id/edittext"
        android:layout_width="230dp"
        android:layout_height="110dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="33dp"
        android:layout_toLeftOf="@+id/relativeLayout1"
        android:background="@drawable/roundcorners"
        android:ems="10"
        android:gravity="center_horizontal"
        android:hint="@string/fbhint"
        android:lines="6"
        android:paddingRight="5dp"
        android:textSize="14sp"      
        android:windowSoftInputMode="stateHidden" />
like image 651
teekib Avatar asked Jan 22 '13 14:01

teekib


2 Answers

I don't think this is possible "out-of-the box" but you can do it programmatically:

yourEditText.addTextChangedListener(new TextWatcher() {
     public void afterTextChanged(Editable s) {}
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

     public void onTextChanged(CharSequence s, int start, int before, int count) {
          if (s.length() > 0){
               // position the text type in the left top corner
               yourEditText.setGravity(Gravity.LEFT | Gravity.TOP);  
          }else{
               // no text entered. Center the hint text.
               yourEditText.setGravity(Gravity.CENTER);  
          }
     }
}
like image 190
userM1433372 Avatar answered Oct 25 '22 15:10

userM1433372


By default android EditText has center gravity. Just make it android:gravity="top|left"

Also there is no any special attribute for making hint in center and text in top. Both follow the same gravity attribute.

Android hint is a just for something like a label for where user has to start input the characters.

like image 43
user370305 Avatar answered Oct 25 '22 15:10

user370305