Android: View.setID(int id) programmatically - how to avoid ID conflicts?
According to View
documentation
The identifier does not have to be unique in this view's hierarchy. The identifier should be a positive number.
So you can use any positive integer you like, but in this case there can be some views with equivalent id's. If you want to search for some view in hierarchy calling to setTag
with some key objects may be handy.
How can I assign an ID to a view programmatically?
Android id
overview
An Android id
is an integer commonly used to identify views; this id
can be assigned via XML (when possible) and via code (programmatically.) The id
is most useful for getting references for XML-defined View
s generated by an Inflater
(such as by using setContentView
.)
Assign id
via XML
- Add an attribute of
android:id="@+id/
somename"
to your view. - When your application is built, the
android:id
will be assigned a uniqueint
for use in code. - Reference your
android:id
'sint
value in code using "R.id.
somename" (effectively a constant.) - this
int
can change from build to build so never copy an id fromgen/
package.name/R.java
, just use "R.id.
somename". - (Also, an
id
assigned to aPreference
in XML is not used when thePreference
generates itsView
.)
Assign id
via code (programmatically)
- Manually set
id
s usingsomeView.setId(
int);
- The
int
must be positive, but is otherwise arbitrary- it can be whatever you want (keep reading if this is frightful.) - For example, if creating and numbering several views representing items, you could use their item number.
Uniqueness of id
s
XML
-assignedid
s will be unique.- Code-assigned
id
s do not have to be unique - Code-assigned
id
s can (theoretically) conflict withXML
-assignedid
s. - These conflicting
id
s won't matter if queried correctly (keep reading).
When (and why) conflicting id
s don't matter
findViewById(int)
will iterate depth-first recursively through the view hierarchy from the View you specify and return the firstView
it finds with a matchingid
.- As long as there are no code-assigned
id
s assigned before an XML-definedid
in the hierarchy,findViewById(R.id.somename)
will always return the XML-defined View soid
'd.
Dynamically Creating Views and Assigning ID
s
- In layout XML, define an empty
ViewGroup
withid
. - Such as a
LinearLayout
withandroid:id="@+id/placeholder"
. - Use code to populate the placeholder
ViewGroup
withView
s. - If you need or want, assign any
id
s that are convenient to each view. Query these child views using placeholder.findViewById(convenientInt);
API 17 introduced
View.generateViewId()
which allows you to generate a unique ID.
If you choose to keep references to your views around, be sure to instantiate them with getApplicationContext()
and be sure to set each reference to null in onDestroy
. Apparently leaking the Activity
(hanging onto it after is is destroyed) is wasteful.. :)
Reserve an XML android:id
for use in code
API 17 introduced View.generateViewId()
which generates a unique ID. (Thanks to take-chances-make-changes for pointing this out.)*
If your ViewGroup
cannot be defined via XML (or you don't want it to be) you can reserve the id via XML to ensure it remains unique:
Here, values/ids.xml defines a custom id
:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="reservedNamedId" type="id"/>
</resources>
Then once the ViewGroup or View has been created, you can attach the custom id
myViewGroup.setId(R.id.reservedNamedId);
Conflicting id
example
For clarity by way of obfuscating example, lets examine what happens when there is an id
conflict behind the scenes.
layout/mylayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
To simulate a conflict, lets say our latest build assigned R.id.placeholder
(@+id/placeholder
) an int
value of 12
..
Next, MyActivity.java defines some adds views programmatically (via code):
int placeholderId = R.id.placeholder; // placeholderId==12
// returns *placeholder* which has id==12:
ViewGroup placeholder = (ViewGroup)this.findViewById(placeholderId);
for (int i=0; i<20; i++){
TextView tv = new TextView(this.getApplicationContext());
// One new TextView will also be assigned an id==12:
tv.setId(i);
placeholder.addView(tv);
}
So placeholder
and one of our new TextView
s both have an id
of 12! But this isn't really a problem if we query placeholder's child views:
// Will return a generated TextView:
placeholder.findViewById(12);
// Whereas this will return the ViewGroup *placeholder*;
// as long as its R.id remains 12:
Activity.this.findViewById(12);
*Not so bad
How to set id for each item in a view dynamically?
As @Rasoul Miri said, you can use View.generateViewId()
to set id for newly inserted views, and you should use a list to record them. And also you should set the id for those three items.
like these:
for the inserted view
<EditText
android:id="@+id/one_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="@+id/two_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
<EditText
android:id="@+id/three_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"/>
for the MainActivity
public ArrayList<InsertedView> list;
public void addView() {
View new_view = layoutInflater.inflate(R.layout.new_layout,insertPoint, false);
insertPoint.addView(new_view);
index_num++;
InsertedView insertedView = new InsertedView(View.generateViewId(), View.generateViewId(), View.generateViewId());
new_view.findViewById(R.id.one_view).setId(insertedView.firstId);
new_view.findViewById(R.id.two_view).setId(insertedView.secondId);
new_view.findViewById(R.id.three_view).setId(insertedView.thirdId);
list.add(insertedView);
}
class InsertedView {
public int firstId;
public int secondId;
public int thirdId;
public InsertedView(int one, int two, int three) {
firstId = one;
secondId = two;
thirdId = three;
}
}
How can I define an ID for my EditText field programmatically?
You can set it with TextView.setId(int id).
Try to see this question for more details.
Android: View.setID(int id) programmatically - how to avoid ID conflicts?
Programmatic Views how to set unique id's?
Just an addition to the answer of @phantomlimb,
while View.generateViewId()
require API Level >= 17,
this tool is compatibe with all API.
according to current API Level,
it decide weather using system API or not.
so you can use ViewIdGenerator.generateViewId()
and View.generateViewId()
in the
same time and don't worry about getting same id
import java.util.concurrent.atomic.AtomicInteger;
import android.annotation.SuppressLint;
import android.os.Build;
import android.view.View;
/**
* {@link View#generateViewId()}要求API Level >= 17,而本工具类可兼容所有API Level
* <p>
* 自动判断当前API Level,并优先调用{@link View#generateViewId()},即使本工具类与{@link View#generateViewId()}
* 混用,也能保证生成的Id唯一
* <p>
* =============
* <p>
* while {@link View#generateViewId()} require API Level >= 17, this tool is compatibe with all API.
* <p>
* according to current API Level, it decide weather using system API or not.<br>
* so you can use {@link ViewIdGenerator#generateViewId()} and {@link View#generateViewId()} in the
* same time and don't worry about getting same id
*
* @author fantouchx@gmail.com
*/
public class ViewIdGenerator {
private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
@SuppressLint("NewApi")
public static int generateViewId() {
if (Build.VERSION.SDK_INT < 17) {
for (;;) {
final int result = sNextGeneratedId.get();
// aapt-generated IDs have the high byte nonzero; clamp to the range under that.
int newValue = result + 1;
if (newValue > 0x00FFFFFF)
newValue = 1; // Roll over to 1, not 0.
if (sNextGeneratedId.compareAndSet(result, newValue)) {
return result;
}
}
} else {
return View.generateViewId();
}
}
}
Related Topics
How to Get the Source Code from an Apk File
How to Change an Application Icon Programmatically in Android
Android Check Internet Connection
Android Studio Suddenly Cannot Resolve Symbols
How to Adjust Text Font Size to Fit Textview
Android Fatal Signal 11 (Sigsegv) At 0X636F7D89 (Code=1). How Can It Be Tracked Down
How to Change Progress Bar'S Progress Color in Android
Detect If Android Device Has Internet Connection
How to Dynamically Add Suggestions to Autocompletetextview With Preserving Character Status
Firebase Offline Capabilities and Addlistenerforsinglevalueevent
How to Define Dimens.Xml For Every Different Screen Size in Android
Firebase Onmessagereceived Not Called When App in Background
How to Send a Json Object Over Request With Android
How to Send Image Via Mms in Android
Sharedpreferences.Onsharedpreferencechangelistener Not Being Called Consistently