12

I am having Multiple Strings, that are coming dynamically, I want to set these strings as Id of the EditText Fields in my Form. How can I do that, can you please help me?

For Ex: If I am having id "title", I want to set this title as id of the EditText Field, so that when I want to access the value of this field, I can access it like findviewById(title).

Please help me here...

Thank you so much in advance.

Narendra Singh
  • 3,990
  • 5
  • 37
  • 78
user3256822
  • 135
  • 1
  • 7
  • Try to use setTag and getTag. – Amsheer May 15 '15 at 08:42
  • Hope you find informative from this [post](http://stackoverflow.com/questions/1714297/android-view-setidint-id-programmatically-how-to-avoid-id-conflicts/15442898#15442898) – NamNH May 15 '15 at 09:41

1 Answers1

0

You can get the id by reflection. For example, if you have a view in xml which has this id: @+id/select_time Then you can get the int value in Rclass by this way:

String idStr = "select_time";
    //com.example.appandroidtest is your app's package name
    Class<?> clz = com.example.appandroidtest.R.id.class;
    try {
        int viewId = (int) clz.getField(idStr).get(null);
    } catch (Exception e) {
        e.printStackTrace();
    }
hanswim
  • 1,182
  • 8
  • 20
  • Thank you for this, But I am not having the ids defined in xml file as these strings are dynamic, Is there any way in that I can add these strings as id in xml file dynamically, so that I can use the above method that you have suggested. Thank you so much for all your help, I really appreciate it. – user3256822 May 15 '15 at 07:59
  • Is there any way in that I can add these strings as id in xml file dynamically?--- I think this is impossible, since you cannot modify any resource file in runtime. – hanswim May 15 '15 at 08:06
  • Do you have any alternate for this problem that I can use for solving the issue. Your help is much appreciated... Thanks.... :) – user3256822 May 15 '15 at 09:08
  • Hii, I want to set Tag for the menuitems, I am creating the menuitems dynamically using this code PopupMenu popup = new PopupMenu(mContext, v); MenuItem menuItem = popup.getMenu().add("title"); When I am doing menuItem.getActionView().setTag("name"); It's giving me NullPointerException for this code. Can you please guide me for this problem, so that I can set the tags for menuitems. Thanks in advance.. – user3256822 May 21 '15 at 04:40