Reputation: 2641
I was wondering how to give a TextField an 'id' in java. So I am pretty much wanting to know how to give the TextField android:id="@id/NAMEHERE" but do it in java. Here is the code so far:
TextView taskname = new TextView(this);
taskname.setText("Task " + tasknumber);
tasklayout.addView(taskname);
What do I need to add to give it an id?
Upvotes: 1
Views: 825
Reputation: 4569
Try this
static final int TEXT_ID = 80001;
TextView taskname = new TextView(this);
taskname.setId(TEXT_ID);
and later in your code you can refer to this textview by
TextView t = (TextView)findViewById(TEXT_ID);
or
TextView t = taskname;
Upvotes: 0
Reputation: 29199
If your question is to how to set id into activity, then understand purpose of id. Id is specially required when you want to fetch view reference into activity class, where view has been declared into xml file. But in Activity, if you are creating any view, by
TextView taskname = new TextView(this);
here, you already have view reference, but if you still want to set id, then you can use method
taskname.setId(10002);
Upvotes: 2