How to display cursor at end of text in TextView?

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

I tried android:cursorVisible="true" in a TextView, but did not go.

Even I tried text.setCursorVisible(true); Does not work.

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

Does anyone know any solution for this?

+6
source share
4 answers

First of all, you should use EditText instead of TextView to enter input. If the cursor still does not blink, set the android:cursorVisible="true" attribute to the xml file , it should make the cursor blink. If your cursor does not appear in a text editor, this is also the reason the cursor blinks. 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, also add this code.

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

I think you should go for EditText . You can set its background and make it look like a TextView using the code below.

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

+2
source

There is a solution for this.

I had to do this when I was making an application for the terminal, and I used a simple runnable to put the 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.

A class method has been created that runs runnable for 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 ran the variables and called runCursorThread() in onCreate()

  cursorOn = false; displayCursor = true; runCursorThread(); 
+1
source

Finally, fixed this with EditText as per @Chintan Rathod's 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. 
0
source

Source: https://habr.com/ru/post/952561/


All Articles