-1

Here is my XML code for the Edittext and i set id with this name ettMId

                <EditText
                android:id="@+id/ettMId"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/login_hint_email_id"
                android:imeOptions="actionNext"
                android:inputType="text|textEmailAddress"
                android:nextFocusDown="@id/etLPassword"
                android:singleLine="true"
                android:theme="@style/AppEditTextStyle" />

I want that ID programmatically in my activity class.

I tried this code for getting id

   int id=ettMId.getId();

But I got below result

 If2131230797

Actually I want same id name(ettMId) which I had set in XML

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arbaz.in
  • 1,478
  • 2
  • 19
  • 41
  • 1
    Why can't you check if it's equal to `R.id.ettMId` ? – Michael Dec 06 '18 at 09:14
  • 1
    Why do you want to do this? – Zun Dec 06 '18 at 09:16
  • @Michael Yes i do that with equal condition but i have more then 1k Edittext fields. – Arbaz.in Dec 06 '18 at 09:16
  • 1
    Ok, but even if you get the id as a string you still have 1000 EditText fields. I don't see how having a string would help. – Michael Dec 06 '18 at 09:18
  • yeah i knew that can you please suggest for same? – Arbaz.in Dec 06 '18 at 09:19
  • what you can do is use Tag and get Tag like android:tag="ettMId" and in coge ettMId.getTag(); – Intsab Haider Dec 06 '18 at 09:20
  • @IntsabHaider can explain in detail? – Arbaz.in Dec 06 '18 at 09:21
  • 1
    @Arbaz.in in every Editext set Tag also like id and then get tag in your activity as i told you et.getTag() it will return you string Tag – Intsab Haider Dec 06 '18 at 09:23
  • I also found the more solution from **Marked Tag by Sergey Glotov**, but accepted answer of that question is not my solution. The solution what i want is [Link-1](https://stackoverflow.com/a/51356286/5995648). [Link-2](https://stackoverflow.com/a/18067589/5995648). i can't delete this question because of @theanilpaudel was invest his time for the answer. _Thanks @ Sergey Glotov_ for suggestion next time i'll keep it in my mind. – Arbaz.in Dec 06 '18 at 10:50

1 Answers1

5

Try this

String stringID = ettMId.getResources().getResourceName(ettMId.getId());

And then you can do String manipulation to extract the exact value of id that will be after :id/

theanilpaudel
  • 3,348
  • 8
  • 39
  • 67