1

I'm trying to display the cursor in an EditText by adding

 android:cursorVisible="true"

in the xml. The issue is that the app version written for a full touch device that use softKeyboard display the cursor but i've developed the app also for devices with physical keyboard and when i'm hidding the softkeyboard the cursor is not even visible.

So the question is how can i make the cursor visible?

For example here is my xml file of an custom Alert and even here the cursor is not showed.

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    android:background="#ffffff"
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="2dp"
        android:layout_marginEnd="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:text="Site:"
        tools:ignore="HardcodedText" />

    <EditText
        android:cursorVisible="true"
        android:layout_margin="10dp"
        android:id="@+id/site"
        android:layout_width="match_parent"
        android:background="#2323"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:ems="10"
        android:textSize="24sp"
        android:textColor="#232323"
        android:imeOptions="actionDone"
        android:inputType="textUri"
        tools:ignore="LabelFor" />

    <TextView
        android:id="@+id/textUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/site"
        android:layout_marginTop="5dp"
        android:text="User:"
        tools:ignore="HardcodedText" />

    <EditText
        android:cursorVisible="true"
        android:id="@+id/editUser"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textUser"
        android:layout_margin="10dp"
        android:background="#2323"
        android:ems="10"
        android:imeOptions="actionDone"
        android:inputType="textNoSuggestions"
        android:textColor="#232323"
        android:textSize="24sp"
        tools:ignore="LabelFor" />

    <TextView
        android:id="@+id/pswFtp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView"
        android:layout_below="@+id/editUser"
        android:layout_marginTop="5dp"
        android:text="Password:"
        tools:ignore="HardcodedText" />

    <EditText
        android:cursorVisible="true"
        android:layout_margin="10dp"
        android:id="@+id/editPswFtp"
        android:layout_width="match_parent"
        android:background="#2323"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/pswFtp"
        android:ems="10"
        android:textSize="24sp"
        android:textColor="#232323"
        android:imeOptions="actionDone"
        android:inputType="textNoSuggestions"
        tools:ignore="LabelFor" />

    <Button
        android:id="@+id/save"
        android:layout_below="@+id/editPswFtp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#232323"
        android:text="SALVA"
        tools:ignore="HardcodedText" />
</RelativeLayout>
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100

2 Answers2

3

Solved by using

editText.setShowSoftInputOnFocus(false);

Instead of

editText.setInputType(InputType.TYPE_NULL);
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100
0

You could just create your custom one that would be visible.

In the res/drawable make your custom_cursor.xml

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

and then add it to your EditText

 android:textCursorDrawable="@drawable/custom_cursor"

You can select your own colors and size of the cursor.

Beasteca
  • 83
  • 9
  • It doesn't work, as i'm using in my java editText.setInputType(InputType.TYPE_NULL); editText.setTextIsSelectable(true); so the cursor is not viewed anyway – NiceToMytyuk Oct 23 '18 at 13:30
  • @JohnKarry Why you are using `editText.setInputType(InputType.TYPE_NULL);` ? because you set Input Type **NULL** in your java code due to that cursor not showing up. If you remove/ comment that line then cursor must be start showing. – Ankit Kumar Singh Oct 23 '18 at 14:04
  • @AnkitKumarSingh _i've developed the app also for devices with physical keyboard and when i'm hidding the softkeyboard the cursor is not even visible._ – NiceToMytyuk Oct 23 '18 at 14:06