Search code examples
androidandroid-edittextfocus

How to programmatically set and remove Focus on edit text on click of a button


I have one edit text and i want to set focus on it once a button is clicked and after editing my status when enter or done is pressed for soft keyboard i want to remove focus again and send a request to server.

here is my edit text in XML

<EditText
                android:id="@+id/time_statusTV"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/selectIBT"
                android:layout_alignLeft="@+id/profile_name"
                android:layout_toLeftOf="@+id/add_timeline_status_IBTN"
                android:layout_toRightOf="@+id/image_profile"
                android:background="@android:color/transparent"
                android:focusable="false"
                android:maxLines="2"
                android:text="short description of \nyourself that can go\nover 2 lines."
                android:textColor="@color/text_color_gray"
                android:textSize="18sp" />

here what i am doing on button click

case R.id.add_timeline_status_IBTN:
        time_statusTV.setFocusable(true);
        time_statusTV.requestFocus();

        break;

here is my Edit text key event

time_statusTV = (EditText) rootView.findViewById(R.id.time_statusTV);
    time_statusTV.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {

                // My code

                time_statusTV.clearFocus();
            }
            return false;
        }
    });

But when I click on button nothing happens.


Solution

  • I think you confused setting focus and showing and hiding keyboard so try my answer:

     case R.id.add_timeline_status_IBTN:
    
        time_statusTV.setFocusable(true);
        time_statusTV.requestFocus();
        InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);
        imm.showSoftInput( time_statusTV, 0);
    

    and for clearing it:

    imm.hideSoftInputFromWindow(your edittext.getWindowToken(), 0);

    so your code must be something like:

      time_statusTV = (EditText) rootView.findViewById(R.id.time_statusTV);
      time_statusTV.setOnKeyListener(new OnKeyListener() {
    
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    && (keyCode == KeyEvent.KEYCODE_ENTER)) {
    
    
                imm.hideSoftInputFromWindow( time_statusTV.getWindowToken(), 0); 
                time_statusTV.clearFocus();
            }
            return false;
        }
    });