Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 895
Android Avatar asked Sep 30 '14 05:09

Android


People also ask

How do I remove focus in EditText?

Remove focus but remain focusable: editText. setFocusableInTouchMode(false); editText. setFocusable(false); editText.

What is focusable in touch mode?

Focusable in touch mode allows view to gain focus when user is touching the view, good example of this kind of component is EditText . With Button or any clickable component pressed state is usually what you are interested in.


2 Answers

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;
    }
});
like image 107
mmlooloo Avatar answered Sep 20 '22 01:09

mmlooloo


Try this way,hope this will help you to solve your problem.

Programatically:

edittext.requestFocus();

Through xml:

<EditText...>
    <requestFocus />
</EditText>
like image 28
Haresh Chhelana Avatar answered Sep 22 '22 01:09

Haresh Chhelana