Uday
Uday

Reputation: 1719

How to display cursor at the end of text in TextView?

I wanted to display blinking cursor at the end of the text in TextView .

I tried by android:cursorVisible="true" in TextView But no go .

Even i tried text.setCursorVisible(true); Doesn't work .

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:cursorVisible="true"
    android:textCursorDrawable="@null"  />

Does any one know any solution for it ?

Upvotes: 8

Views: 19961

Answers (4)

Xpleria
Xpleria

Reputation: 5853

There is a solution for this.

I had to do this when I was making a terminal app and I used a simple runnable put a cursor at the end and make it blink.

I made 3 class variables:

private boolean displayCursor;
private boolean cursorOn;
private String terminalText;

private TextView terminal; // The TextView Object

terminalText keeps track of the text to be displayed.

Created a class method that runs the runnable the first time

private void runCursorThread() {
    Runnable runnable = new Runnable() {
        public void run() {
            if (displayCursor) {
                if (cursorOn) {
                    terminal.setText(terminalText);
                } else {
                    terminal.setText(terminalText + '_');
                }
                cursorOn = !cursorOn;
            }
            terminal.postDelayed(this, 400);
        }
    };
    runnable.run();
}

And initiated the variables and called runCursorThread() in onCreate()

    cursorOn = false;
    displayCursor = true;
    runCursorThread();

Upvotes: 2

Uday
Uday

Reputation: 1719

Finally Fixed this Using the EditText as per @Chintan Rathod advice.

<EditText
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"/> //reference to @Chintan Rathod.

Code

EditText text=(EditText) findViewById(R.id.text);
text.setText("hello");
text.setSelection(text.getText().length()); // reference to @Umer Farooq code.

Upvotes: 1

Chintan Rathod
Chintan Rathod

Reputation: 26034

I think you should go for EditText. You can set its background and make it appears like TextView with below code.

Step 1

<EditText
    android:id="@+id/edtText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent" >
</EditText>

Step 2

EditText edt = (EditText) findViewById(R.id.edtText);
edt.setSelection(edt.getText().length());

Output

enter image description here

Upvotes: 1

Umer Farooq
Umer Farooq

Reputation: 7486

First of all you should use EditText in place of TextView for taking input. If still the cursor doesn't blink, set the android:cursorVisible="true"attribute in xml file, it should make the cursor blink. If your cursor is not visible in edit text, that's also a reason one can't see the cursor blinking. Set android:textCursorDrawable="@null". This should solve your problem

<EditText
   android:id="@+id/editext1"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:textCursorDrawable="@null"
   android:cursorVisible="true">

</EditText>

In your activity class, add this code as well.

EditText input = (EditText)findViewById(R.id.edittext1);
input.setSelection(input.getText().length());

Upvotes: 6

Related Questions