Trusha Savsani
Trusha Savsani

Reputation: 479

set cursor position of edittext in center...!

I know this question asked many times. But none of examples work for me.
I want to set cursor position in the center through XML file.
how can I do that??
My current cursor position display in following figure.

My XML file code is following

.XML file

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/sigupDisplayName"
            style="@style/editTextStyle"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@drawable/edittext_style"

            android:ems="10"
            android:gravity="center"
            android:hint="@string/display_name" >

            <requestFocus />
        </EditText>
    </LinearLayout>

Upvotes: 2

Views: 3917

Answers (5)

Himanshu itmca
Himanshu itmca

Reputation: 395

Use TextInputLayout it will work fine with a hint.

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:hint="Email"
        android:gravity="center"
        />

</android.support.design.widget.TextInputLayout>

Upvotes: 0

Akash
Akash

Reputation: 971

try this

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_style"
    android:gravity="center"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/sigupDisplayName"
        style="@style/editTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:gravity="center"
        android:hint="@string/display_name" >

    </EditText>
</LinearLayout>

Upvotes: 0

GIGAMOLE
GIGAMOLE

Reputation: 1266

Just try this:

your_edit_text.setText("bla-bla-bla");
int position = your_edit_text.length();
Editable text = your_edit_text.getText();
Selection.setSelection(text, position / 2);

Or this:

your_edit_text.setSelection(position / 2)

Try this if upper doesn`t work:

your_edit_text.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            your_edit_text.setSelection(your_edit_text.getText().length() / 2);
            return false;
        }
    });

Or this:

yourEditText.requestFocus(Your_Text.length() / 2);

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

try below edittext instead of yours. This will work for you

   <EditText android:id="@+id/sigupDisplayName"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:ems="10"
    android:gravity="center"
    android:hint="@string/display_name" >

EDIT Remove Hint and you will see it in middle of the editText.

Upvotes: 1

Trusha Savsani
Trusha Savsani

Reputation: 479

I found now what is main problem...
Problem is in the API level..
I change API level of my program.
Below API 15, cursor is set in the center in my application..
From API 16, cursor position set to start of the Hint text...!!!

Upvotes: 3

Related Questions