Android EditText - Stop cursor blinking, want a solid cursor
Asked Answered
G

1

2

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 this is just a pain and will need redundant work to recognise user taps / cursor moves.

To be clear

editText.setCursorVisible(false);

Is not the answer I am looking for.

Possibly it could be set in the XMl drawable file?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
    <size android:width="1dp" />
    <stroke android:color="@android:color/black"/>
    <solid android:color="@android:color/black"/>
</shape>

but this seems unlikely.

Here is the editText

 <AutoCompleteTextView
        android:id="@+id/input_text"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:textSize="7mm"
        android:drawableRight="@drawable/clear_tran"
        android:drawableEnd="@drawable/clear_tran"
        android:background="@android:color/transparent"

        android:cursorVisible="true"
        android:textCursorDrawable="@drawable/cursor"

        android:text=""
        android:autoLink="none"
       android:textAppearance="@android:style/TextAppearance.Holo.Medium"
        android:clickable="true"
        android:inputType="text|textNoSuggestions"
        android:layout_weight="1"
        android:textStyle="normal"
        android:singleLine="true"
        android:textIsSelectable="false"
        android:layout_marginLeft="2mm"
        android:layout_marginRight="2mm"
        android:layout_marginBottom="1mm"/>

Any suggestions from the meta-mind? All my googles are just returning how to hide the cursor.

I will keep investigating and return with my results.

Gunfight answered 13/7, 2015 at 16:37 Comment(0)
G
0

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.

Gunfight answered 14/7, 2015 at 10:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.