EditText cursor visible even if the EditText is not editable

Viewed 1338

I need to introduce data in an EditText but i want to use an virtual keyboard, not the android keyboard. If I use setKeyListener(null) the cursor is invisible even after using setCursorVisible(true).

Is it possible to make an EditText where even if it isn't editable the cursor is visible ?

EDIT 2 : I found an partial method to do that, but it's not working when i'm double taping the EditText.

I made an setOnClickListner() and an setOnLongClickListner() method for the EditText. In this methods I hide the Soft Input from the Window, also i use setTextIsSelectable(false). My only problem is that when I double tap the EditText the soft input keyboard shows and I dont know how to hide it, I tried to use android:windowSoftInputMode="stateAlwaysHidden" in manifest, but it doesn't work either.

EDIT :

Here is the code that I'm using at this moment for my base converter calculator.

public class MainActivity extends AppCompatActivity {

EditText number;
EditText base;
boolean baseB = false;
String numberS = "0";
String baseS = "10";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(activity_main);


    //make the EditText for number and base not editable
    number = (EditText) findViewById(R.id.number);
    number.setKeyListener(null);
    base = (EditText) findViewById(R.id.base);
    base.setKeyListener(null);

    //... more code here (changing fonts for each EditText and changing status bar color

}

// I have a function for each button all are the same

public void onClickBaseChange(View v) {
    if (baseB) {
        baseB = false;
        // i use toasts at this moment to know when i'm on number or base field
        Toast.makeText(this, "Number", Toast.LENGTH_SHORT).show();
    } else {
        baseB = true;
        Toast.makeText(this, "Base", Toast.LENGTH_SHORT).show();
    }
}

public void onClickB0(View v) {
    if (numberS.length() > 0 && !numberS.equals("0") && !baseB) {
        numberS += "0";
        number = (EditText) findViewById(R.id.number);
        number.setText(numberS, TextView.BufferType.EDITABLE);
        number.setSelection(numberS.length());
    } else {
        if (Integer.valueOf(baseS) >= 1) {
            baseS += "0";
            base = (EditText) findViewById(R.id.base);
            base.setText(baseS, TextView.BufferType.EDITABLE);
        }
    }
}

public void onClickB1(View v) {
    if (numberS.equals("0")) {
        numberS = "1";
    } else {
        numberS += "1";
    }
    number = (EditText) findViewById(R.id.number);
    number.setText(numberS, TextView.BufferType.EDITABLE);
    number.requestFocus();
    number.setSelection(numberS.length());
}

And the xml looks like this :

<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorBackground"

tools:context="manastur.calculator.MainActivity">


<EditText
    android:id="@+id/base"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    android:layout_marginTop="120dp"
    android:background="@android:color/transparent"
    android:cursorVisible="true"
    android:text=""
    android:textColor="@color/text"
    android:textSize="30dp" />

<EditText
    android:id="@+id/number"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="50dp"
    android:background="@android:color/transparent"
    android:cursorVisible="true"
    android:text=""
    android:textColor="@color/text"
    android:textSize="50dp" />


<LinearLayout
    android:id="@+id/secondRow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/firstRow"
    android:layout_centerHorizontal="true">

    <Button
        android:id="@+id/b1"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:background="@drawable/b1"
        android:onClick="onClickB1" />

    <Button
        android:id="@+id/b2"
        android:layout_width="85dp"
        android:layout_height="85dp"
        android:background="@drawable/b2"
        android:onClick="onClickB2" />

   <!-- from this point on is the same, there are 5 LinearLayouts which
   represents the 5 rows of button of the num pad -->       
3 Answers

I wanted the same behavior which I achieved as follows -

  1. Make a custom class that will override 2 methods of AppCompatEditText.

     class CustomEditText(context: Context?, attrs: AttributeSet) : AppCompatEditText(context, attrs) {
     override fun onCheckIsTextEditor(): Boolean {
         return true
     }
    
     override fun isTextSelectable(): Boolean {
         return true
     }
    }
    
  2. In the XML file, create EditText using this custom view.

     <com.ui.custom.CustomEditText
         android:id="@+id/et_email"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:inputType="none"
         android:focusable="true"
         android:gravity="center"
         android:focusableInTouchMode="true"/>
    
  3. Now, just add onFocusChangeListener and set editText.setKeyListener = null.

      binding.etEmail.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
         if (hasFocus) {
             binding.etEmail.keyListener = null
         }
     }
    

You can add the same on onTouch if that is the requirement.

The main issue here is that onCheckIsTextEditor() of View class always returns false, which leads to cursor never blinking or being visible even if setCursorVisible(true) was called in code.

I hope it helps.

Related