13

I am developing an application in which i am creating an Edittext programmatically as :

EditText edText = new EditText(this);
edText.setId(1);
edText .setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,0f));
edText .setInputType(InputType.TYPE_CLASS_NUMBER);
edText.setHint("dsgsdgsgs");
tableLayout.addView(edText);

Here I am setting the id of the Edit text as "1" by the line edText.setId(1); in integer.

But what i need is - I want to set the ID in character as for example:

edText.setId("edittext_hello");

So that i can access it via that id. How can i achieve this task please help.

Optim India
  • 486
  • 1
  • 5
  • 11

5 Answers5

25

As the others have said you can't do this. Why do you want to / what's your requirement?

You could create an id in an xml file and use that - if you want it to be descriptive. This is also a better approach than using literal ints as you may get an id clash with other views in the layout hierarchy (unlikely but possible). This to me seems like the best / cleanest solution to your problem.

See http://developer.android.com/guide/topics/resources/more-resources.html#Id

e.g. in res/values/id.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item
        type="id"
        name="edittext_hello" />
</resources>

and then set with

edText.setId(R.id.edittext_hello);
Dori
  • 18,283
  • 17
  • 74
  • 116
  • What if you have created elements dynamically according to a properties file? How can you set the IDs that are within the properties file dynamically to each one of the elements created? – FLBKernel May 09 '18 at 08:58
  • Ok, I have figured out how. Just need to have the parent reference of the element you have set a tag, and then, you can find it with the tag. `LinearLayout linearLayout= (LinearLayout) findViewById(R.id.sale_aditionals_layout);` and then `MaterialEditText materialEditText = (MaterialEditText) linearLayout.findViewWithTag(entry.getKey());` for example.. – FLBKernel May 10 '18 at 06:52
4

You can't set id with char, String or anything else except int...because, id is maintained by R.java file which contains only int.

You can use setTag() instead of setId().

Use setTag() as below...

edText.setTag("edittext_hello");
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
4

No, you can't set id as a String. You can only assign integer as Id. You can use setTag() of View with String. But for id it will only Integer. As android resources are maintained in R.java file for integer type.

Update:

Why do you not set String or other data types (without integer) id to any android resource?

Because:

An Android Resource id is a 32-bit integer. It comprises

an 8-bit Package id [bits 24-31]
an 8-bit Type id [bits 16-23]
a 16-bit Entry index [bits 0-15]

The Package id identifies the Package chunk which contains the Resource.

The Type id identifies the type of the Resource and hence the corresponding Typespec chunk and Type chunk or chunks which contain its value or value(s)

The Entry index identifies the individual Resource within the Typespec chunk and Type chunk(s).

user370305
  • 108,599
  • 23
  • 164
  • 151
2

You can also achieve your problem without creating any extra resource like ids.xml and without having any id set to any of the EditText created dynamically.

Just need to have the parent reference of the element you have set a tag, and then, you can find it with the tag.

Parent

LinearLayout linearLayout= (LinearLayout) findViewById(R.id.layout_id);

and then

Childs

EditText editText = (EditText) linearLayout.findViewWithTag("yourStringTag");

for example..

Note: Obviously, if you have created EditTexts dynamically, you would do something like this:

for (Map.Entry<String, String> entry : yourHashMap.entrySet()) {

LinearLayout linearLayout= (LinearLayout) findViewById(R.id.parent_layout_id);

EditText editText = (EditText) linearLayout.findViewWithTag(entry.getKey());

// Then do whatever you want with your editText referenced.
// ...

}
FLBKernel
  • 860
  • 13
  • 21
-3

You can use setText() instead of setId().

edText_view.setText("Your Text");

Lakshay Sharma
  • 1,347
  • 12
  • 13