how can I only show the cursor of an EditText when the keyboard is displayed. At the moment the cursor is blinking even if the EditText is not active and the keyboard is hidden, which is really annoying.

This is how my layout looks:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white" >

    <ImageButton
        android:id="@+id/activity_main_add_button"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:background="@drawable/button"
        android:contentDescription="@string/desc_add"
        android:onClick="addGame"
        android:src="@android:drawable/ic_input_add"
        android:tint="@color/gray" />

    <View
        android:id="@+id/stub1"
        android:layout_width="2dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/activity_main_add_button"
        android:background="@color/light_gray" />

    <ImageButton
        android:id="@+id/activity_main_menu_button"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/stub1"
        android:background="@drawable/button"
        android:contentDescription="@string/desc_add"
        android:onClick="showMenu"
        android:scaleType="fitXY"
        android:src="@drawable/square_button"
        android:tint="@color/gray" />

    <View
        android:id="@+id/stub2"
        android:layout_width="2dp"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/menu_button"
        android:background="@color/light_gray" />

    <EditText
        android:id="@+id/activity_main_search"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_toRightOf="@id/stub2"
        android:background="@drawable/default_background"
        android:gravity="center"
        android:hint="@string/search_game"
        android:inputType="text"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:textStyle="bold" />

    <ListView
        android:id="@+id/activity_main_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/activity_main_add_button"
        android:layout_margin="10dp"
        android:divider="@color/white"
        android:dividerHeight="10dp" >
    </ListView>

</RelativeLayout>

Thanks, Solidus

I tried multiple options and only one is working correctly. May be there is a cleaner way to do it but this worked for me:

Code

public class MainActivity extends Activity {
    EditText edit = null;

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

        setContentView(R.layout.activity_main);
        edit = (EditText) findViewById(R.id.edit);

        final View activityRootView = findViewById(R.id.activityRoot);
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                activityRootView.getWindowVisibleDisplayFrame(r);
                int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
                if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
                    if (edit != null) {
                        edit.setCursorVisible(true);
                    }
                } else {
                    if (edit != null) {
                        edit.setCursorVisible(false);
                    }
                }
             }
        });
    }
}

Details

The idea is to use a ViewTreeObserver in order to detect when the activityRoot (id of the root layout of your activity) is hidden by something (comparing the actual height with the initial height of the layout), probably the keyboard.

I found the solution using this question.

I hope this code will help somebody.

1) Set in your xml under your EditText:

android:cursorVisible="false"

2) Set onClickListener:

iEditText.setOnClickListener(editTextClickListener);

OnClickListener editTextClickListener = new OnClickListener() 

{

    public void onClick(View v) 
    {
         if (v.getId() == iEditText.getId()) 
        {
            iEditText.setCursorVisible(true);
        }

    }
};
  • doesnt work: after a keyboard close a cursor still active – tadvas May 11 at 12:11

add

android:cursorVisible="false" 

in Edittext

  • please add comment why you find unhelpful – Rahul Mar 31 '15 at 6:09
  • 1
    setting cursorVisible to false will always hide the cursor, even if the EditText has focus. instead, we want to the cursor to show when there's focus but hide when there's no focus – Pietro Rea Apr 23 '15 at 22:58

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.