Android Edittext - Stop Cursor Blinking, Want A Solid Cursor
I want to be able to see the cursor all the time. No blinking, and no hiding. I could extend editText and get into rendering a graphic and of-setting it as the text is written but
Solution 1:
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.
publicclassAutoCompleteEditTextStaticCursorextendsAutoCompleteTextView {
privateintmCursorColor= Color.BLACK; //Cursor defaults to blackprivateintmStroke=5; //Default stroke is 5publicAutoCompleteEditTextStaticCursor(Context context) {
super(context);
setCursorVisible(false);
}
publicAutoCompleteEditTextStaticCursor(Context context, AttributeSet attrs){
super(context, attrs);
setCursorVisible(false);
}
publicAutoCompleteEditTextStaticCursor(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
*/publicvoidsetCursorColor(int color){ //Cursor defaults to black
mCursorColor = color;
}
/**
* Set the cursor stroke width
*
* Default value is 5
* @param stroke
*/publicvoidsetCursorStroke(int stroke){
mStroke = stroke;
}
@OverrideprotectedvoidonDraw(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) {
intpos= getSelectionStart();
Layoutlayout= getLayout();
if (layout == null){
return canvas;
}
//Get where the cursor should befloatx= layout.getPrimaryHorizontal(pos);
//When there is no text, just off set it so that the whole cursor is drawnif (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 adjustmentfloatheight= canvas.getHeight();
//Get the text HeightfloattextHeight= getTextSize();
Paintp=newPaint();
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.
Post a Comment for "Android Edittext - Stop Cursor Blinking, Want A Solid Cursor"