Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make android edittext hint center and left cursor

How set hint text in the center of EditText and set cursor in the left part of EditText? And when i start input text it start from the center EditText?

enter image description here

Now i have

enter image description here

<LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:background="@drawable/line_white"
                android:layout_marginBottom="25dp"
                android:paddingBottom="16dp"
                android:gravity="center_horizontal">

                <EditText
                    android:id="@+id/loginEmailField"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:autoText="false"
                    android:ems="10"
                    android:hint="Choose an email adress"
                    android:inputType="textEmailAddress"
                    android:paddingLeft="2dp"
                    android:singleLine="false"
                    android:textColor="#ffffff"
                    android:textColorHint="#9377ab"
                    android:textSize="19sp"
                    android:textStyle="bold"
                    android:background="#00000000"
                    android:layout_marginRight="2dp"
                    android:gravity="center"/>
            </LinearLayout>
like image 865
ip696 Avatar asked Aug 03 '15 05:08

ip696


People also ask

How do you center text hint on android?

Actually, android:gravity="center_horizontal" creates the centering effect you're looking for. Similarly, you can use android:gravity="start" to put hint text at the beginning of the EditText view, and so on.

How do I change the cursor position in EditText?

Say in your xml's edittext section, add android:paddingLeft="100dp" This will move your start position of cursor 100dp right from left end. Same way, you can use android:paddingRight="100dp" This will move your end position of cursor 100dp left from right end.

How do you center text in EditText?

Simpler Solution to align text in EditText is to add android:gravity="center" in layout xml. There is no need to add Java code.


2 Answers

I also faced to the same problem and think of 2 possible variants:

1. Pretty simple
Cursor in the middle of the hint looks little unpleasantly.
So i just remove hint whet editText gets focus and set it back otherwise:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        editText.setHint(hasFocus ? "" : "My hint");
    }
});

2. Some hardcode
In this case:
if editText is empty:
- set text to hint
- set textColor to hintColor
- disable text selecting
- set selection before text (setSelection(0))

if user enters any symbol
- replace hint with ""
- set textColor back to textColor
- enable text selecting

Steps:
2.1 Add TextWatcher

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { // empty }

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

    @Override
    public void afterTextChanged(Editable s) { 
        processChangedText(s); 
    }
});

2.2 Set on focus change listener:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
       if (hasFocus) {
           processChangedText(editText.getText());
       }
    }
});

2.3 And a little magic inside method processChangedText(Editable s)

private void processChangedText(Editable s) {
    String str = s.toString();
    final int colorHint = ContextCompat.getColor(getActivity(), R.color.color_hint);
    final int colorText = ContextCompat.getColor(getActivity(), R.color.color_text);

    String hint = getString(R.string.login_login_edit_hint);
    if (str.equals(hint)) {
        editText.setTextColor(colorHint);
        enableCursorMove(editText, false);
        editText.setSelection(0);
        return;
    } else {
        editText.setTextColor(colorText);
        enableCursorMove(editText, true);
    }
    if (str.isEmpty()) {
        editText.setText(hint);
    } else if (str.contains(hint)) {
        editText.setText(str.replace(hint, ""));
    } else if (str.length() == 1) {
        editText.setSelection(1);
    }
}

2.4 Enabler/disabler cursor move method (probably not a good choice, it's better to create own EditText overriding onSelectionChanged method)

private void enableCursorMove(EditText editText, boolean enable) {
    editText.setOnTouchListener(enable ? null : new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            editText.setSelection(0);
            return true;
        }
    });
}

P.S. I really advise you to use first variant, it's quite simple and looks pretty good.

like image 134
repitch Avatar answered Oct 20 '22 13:10

repitch


thats a bit of a stupid issue, i know.. easiest way to solve this is (and keep centered text) is using this simple hack:

        editText.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 != null && s.toString().length() > 0){
                editText.setGravity(Gravity.CENTER);
            }else{
                editText.setGravity(Gravity.START);
            }
        }
    });
like image 32
Wops Avatar answered Oct 20 '22 15:10

Wops