Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: only show cursor in edittext when keyboard is displayed

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

like image 934
Solidus0815 Avatar asked Jul 17 '13 22:07

Solidus0815


Video Answer


2 Answers

  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);
            }
    
        }
    };
    
like image 59
Shahab Rauf Avatar answered Oct 17 '22 07:10

Shahab Rauf


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.

like image 3
smarroufin Avatar answered Oct 17 '22 06:10

smarroufin