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

6

6

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.

Botheration answered 30/9, 2014 at 5:4 Comment(1)
dont use requestFocus() and clearFocus() , and check my answer, it worksEure
H
10

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;
    }
});
Hemipterous answered 30/9, 2014 at 5:27 Comment(1)
When a View clears focus the framework is trying to give focus to the first focusable View from the top. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after which the framework will give focus to this view.Popedom
F
1

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

Programatically:

edittext.requestFocus();

Through xml:

<EditText...>
    <requestFocus />
</EditText>
Fingerboard answered 30/9, 2014 at 5:5 Comment(2)
Still nothing happensBotheration
what do you mean by nothing happens ?Fingerboard
E
1

try this :

editText.setFocusable(true);// to set focus on EditText

    editText.setFocusable(false);// to remove focus from EditText
Eure answered 30/9, 2014 at 5:8 Comment(0)
S
0

use setFocus for EditText

editText1.setFocusable(true);
Summertime answered 30/9, 2014 at 5:28 Comment(0)
I
0

you can add this to onCreate and it will hide the keyboard everytime the activty starts You could also programatically change the focus to another item.

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Iseabal answered 30/9, 2014 at 5:47 Comment(0)
D
0
CodeScannerView scannerView = findViewById(R.id.scanner_view);
        mCodeScanner = new CodeScanner(this, scannerView);
        mCodeScanner.setDecodeCallback(result -> runOnUiThread(() -> Toast.makeText(scanner.this, result.getText(), Toast.LENGTH_SHORT).show()));
        scannerView.setOnClickListener(view -> mCodeScanner.startPreview());
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCodeScanner.startPreview();
    }

    @Override
    protected void onPause() {
        mCodeScanner.releaseResources();
        super.onPause();
    }
}
Dippy answered 12/2 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.