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 t
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());
I think you should go for EditText
. You can set its background and make it appears like TextView
with below code.
<EditText
android:id="@+id/edtText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent" >
</EditText>
EditText edt = (EditText) findViewById(R.id.edtText);
edt.setSelection(edt.getText().length());
Output
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.
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();