Issue
I have layout with several EditText elements:
<EditText android:id="@+id/etName"/>
<EditText android:id="@+id/etModel"/>
<EditText android:id="@+id/etYear"/>
And this array stores ID's of all these elements:
int[] fields = {
R.id.etName,
R.id.etModel,
R.id.etYear
};
Can I store and load these ID's in "res/values/fields.xml" instead int array in java file like this:
<resources>
<array name="fields">
<item name="etName" type="id" />
<item name="etModel" type="id" />
<item name="etYear" type="id" />
</array>
</resources>
TypedArray fields = getResources().obtainTypedArray(R.array.fields);
Solution
Your ids already exist in your xml layout, you can simply refer to them to define an array:
<array name="fields">
<item>@id/etName</item>
<item>@id/etModel</item>
<item>@id/etYear</item>
</array>
After then you can use this array in your code:
TypedArray fields = getResources().obtainTypedArray(R.array.fields);
for (int i = 0; i < fields.length(); i++) {
EditText editText = findViewById(fields.getResourceId(i, 0));
editText.setText("Yo!"); // or something else
}
fields.recycle();
Answered By - Vadik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.