Disable Edittext Blinking Cursor

Disable EditText blinking cursor

You can use either the xml attribute android:cursorVisible="false" or programatically:

  • java: view.setCursorVisible(false)
  • kotlin: view.isCursorVisible = false

remove cursor from editText

to remove the cursor from edittext you need to set

nameText.setFocusable(false);

and to visible cursor set

nameText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

nameText.setFocusableInTouchMode(true);

return false;
}
});

will show the cursor in edittext...

Android EditText - Stop cursor blinking, want a solid cursor

Okay, seems the API does not allow a static cursor. I created my own extension of the AutoCompleteTextView which renders a cursor which never blinks. (You can use the same code to extend a noemal EditText or other derivative)

Bewarned if you want to have a multiline edit text then the height parameters will need some extra work. I have assumed one line and centered the height on the text box.

    public class AutoCompleteEditTextStaticCursor extends AutoCompleteTextView {

private int mCursorColor = Color.BLACK; //Cursor defaults to black
private

int mStroke = 5; //Default stroke is 5

public AutoCompleteEditTextStaticCursor(Context context) {
super(context);
setCursorVisible(false);
}

public AutoCompleteEditTextStaticCursor(Context context, AttributeSet attrs){
super(context, attrs);
setCursorVisible(false);
}
public AutoCompleteEditTextStaticCursor(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
setCursorVisible(false);
}

/**
* Set the cursor color
* Must have a static int reference.
* If you wish to use a resource then use the following method
* int color = getResources().getColor(R.color.yourcolor);
*
* Default value Color.BLACK
* @param color
*/
public void setCursorColor(int color){ //Cursor defaults to black
mCursorColor = color;
}

/**
* Set the cursor stroke width
*
* Default value is 5
* @param stroke
*/
public void setCursorStroke(int stroke){
mStroke = stroke;
}

@Override
protected void onDraw(Canvas canvas) {

//Take this opportunity to draw our cursor
canvas = drawCursor(canvas);

super.onDraw(canvas);
}

/**
* Draw a cursor as a simple line,
* It would be possible to render a drawable if you wanted rounded corners
* or additional control over cursor
*
* @param canvas
* @return
*/
private Canvas drawCursor(Canvas canvas) {

int pos = getSelectionStart();
Layout layout = getLayout();

if (layout == null){
return canvas;

}

//Get where the cursor should be
float x = layout.getPrimaryHorizontal(pos);

//When there is no text, just off set it so that the whole cursor is drawn
if (x< mStroke/2){
x = mStroke/2

;
}

//Get the height of the edit text we will half this to center
//TODO this will only work for 1 line!!!!!!! Multi line edit text will need further adjustment
float height = canvas.getHeight();

//Get the text Height
float textHeight = getTextSize();

Paint p = new Paint();
p.setColor(mCursorColor);
p.setStrokeWidth(mStroke);

canvas.drawLine(x, height/2 - textHeight/2, x, height/2 +textHeight/2, p);

return canvas;
}
}

Usage is like this

 feedbackText = (AutoCompleteEditTextStaticCursor) findViewById(R.id.input_text);
feedbackText.setCursorColor(getResources().getColor(R.color.cursor_color));
feedbackText.setCursorStroke(9);

Hope this helps someone.

Android EditText Blinking Cursor

To stop the cursor from blinking in an EditText, simply use this line:

editText.setCursorVisible(false);

That's it.

How to prevent custom dialog from blinking cursor on first EditText inside of it

Use either XML attribute or Java function-

xml:

android:cursorVisible="false" 

Java function:

setCursorVisible(false)

How to hide the cursor when typing on android EditText?

Try this:

android:cursorVisible="false"

How to remove focus from EditText when user is done editing?

You could use a xml attribute,

android:cursorVisible 

or you can do it in code with this method.

 setCursorVisible(boolean).


Related Topics



Leave a reply



Submit