Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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 ?

like image 986
Uday Avatar asked Aug 27 '13 06:08

Uday


People also ask

How do I change my cursor at the end of EditText?

My solution always puts the cursor in the end of the string whenever the touch occurs on the EditText. // Create a Static boolean flag private static boolean returnNext; // Set caret/cursor to the end on focus change EditTextAmount. setOnFocusChangeListener(new View.

How do you get 3 dots at the end of a TextView text?

You are applying to your TextView a compound Drawable on the right.. to make the three dots appear in this scenario, you have to apply a android:drawablePadding="{something}dp" attribute to the TextView as well. Hope it helps!

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 get to the next line in TextView?

Add Line Breaks to a TextView Just add a \n to your text. This can be done directly in your layout file, or in a string resource and will cleanly break the text in your TextView to the next line.


2 Answers

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());
like image 106
Umer Farooq Avatar answered Nov 03 '22 02:11

Umer Farooq


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();
like image 36
Xpleria Avatar answered Nov 03 '22 02:11

Xpleria