Android: Using findViewById() with a string / in a loop
You should use getIdentifier()
for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = ((Button) findViewById(resID));
buttons[i][j].setOnClickListener(this);
}
}
Findviewbyid() in for loop
1.init TextView
、ImageView
array
2.add for loop
3.get id of the view
4.findViewById in the code
Try this .
TextView[] textViews1 = new TextView[7];
TextView[] textViews2 = new TextView[7];
ImageView[] imageViews = new ImageView[7];
for (int j = 0; j < 7; j++) {
String viewmt = "mt" + (i + 1);
String viewtid = "tid" + (i + 1);
String viewImage = "imageView" + (i + 1);
int resIDmt = getResources().getIdentifier(viewmt, "id", getPackageName());
int resIDtid = getResources().getIdentifier(viewtid, "id", getPackageName());
int resIDImage = getResources().getIdentifier(viewImage, "id", getPackageName());
textViews1[j] = ((TextView) v.findViewById(resIDmt));
textViews2[j] = ((TextView) v.findViewById(resIDtid));
imageViews[j] = ((ImageView) v.findViewById(resIDImage));
}
FindViewById where ID is dynamic string
You can get an identifier from a string by using:
notes = (TextView)findViewById(getResources().getIdentifier(VIEW_NAME, "id", getPackageName()));
Where VIEW_NAME
is whatever identifier string you're generating. After that, you can set the text of it like you currently are. This also works if you need to get strings and drawables as well, just change id
to the appropriate type.
findViewById with for loop
Do It like this. You can get drawable names from array stored in xml or code.
for (int i=1; i<=9; i++) {
int id = getResources().getIdentifier("drawableName", "drawable", context.getPackageName());
ImageView img = (ImageView) findViewById(id);
}
Here "drawableName" is the name of your image and leave "drawable" as is.
FindViewById Loop
Take One Integer array for R.id.imageView1......12 and pass its value
Like
private Integer[] Imgid = {R.id.imageview1,....,12 };
and use array
I want to use findViewById with strings as an Id
You can use this code to replace the findViewById(..)
. This should make it possible to use a string as identifier:
String BId = "button1"; // for example
int id = getResources().getIdentifier(BId, "id", getPackageName());
// finds R.id.button1
bdatum0 = (Button) findViewById(id);
Android: Using findViewById() with a string / in a loop
You should use getIdentifier()
for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i][j] = ((Button) findViewById(resID));
buttons[i][j].setOnClickListener(this);
}
}
Related Topics
Multi Flavor App Based on Multi Flavor Library in Android Gradle
Customize Android Facebook Login Button
Set Inputtype for an Edittext Programmatically
Actionbaractivity Is Deprecated
Transparent Actionbar: Custom Tabcolor
Getrunningtasks Doesn't Work in Android L
Lock Screen Orientation (Android)
Is a Relativelayout More Expensive Than a Linearlayout
How to Fix: "Hax Is Not Working and Emulator Runs in Emulation Mode"
Adding Table Rows Dynamically in Android
Android: Bitmapfactory.Decodestream() Out of Memory with a 400Kb File with 2Mb Free Heap
How to Show Icons in Overflow Menu in Actionbar
How to Use 3G Connection in Android Application Instead of Wi-Fi
How to Use a Custom Typeface in a Widget
Android Gradle Could Not Reserve Enough Space for Object Heap