0

I have two Text fields. I have focus on the first text field. If I click on a button I should be able to find which text field has focus and want to know its id.

<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/editText2"
  android:layout_below="@+id/editText"
  android:layout_alignParentRight="true"
  android:layout_alignParentEnd="true"
  android:layout_marginTop="33dp"
  android:hint= "@string/rfid_plh"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true" />

<EditText
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/editText2"
  android:layout_below="@+id/editText"
  android:layout_alignParentRight="true"
  android:layout_alignParentEnd="true"
  android:layout_marginTop="33dp"
  android:hint= "@string/rfid1_plh"
  android:layout_alignParentLeft="true"
  android:layout_alignParentStart="true" />

How to achieve this?

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
user3383301
  • 1,891
  • 3
  • 21
  • 49
  • You check this out: http://stackoverflow.com/a/17342104/3931910 – YakuZa Nov 09 '16 at 04:59
  • 1
    Possible duplicate of [How can I detect focused EditText in android?](http://stackoverflow.com/questions/17341946/how-can-i-detect-focused-edittext-in-android) – Nabin Nov 09 '16 at 04:59

1 Answers1

-1

This will do it for you:

    final EditText et1,et2;
    Button checkButton;

    //initilize both edit text and button here

    checkButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(et1.hasFocus()){
                //TODO code here for edit text 1
                int id = et1.getId();
            }else if(et2.hasFocus()){
                //TODO code here for edit text 2
                int id = et2.getId();

            }
        }
    });
Ashwani Kumar
  • 1,402
  • 1
  • 19
  • 26
  • Can you please write a little bit about the change you did here. Or something about the code? – Nabin Nov 27 '16 at 03:07
  • OnClick of the button I've just checked which EditText has focus, by using the method .hasFocus(). Then to get the id of that EditText I've used the method .getId(). Its very straight forward. What is there that you are not getting? Please specify. – Ashwani Kumar Nov 28 '16 at 10:57