Android: Using findViewById() with a string / in a loop - android

I'm making an android application, where there is a view composed of hundreds of buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code (for each one of the buttons).
My question is: How can I use findViewById without statically having to type in each button id?
Here is what I would like to do:
for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
buttons[i][j] = ((Button) findViewById(R.id.buttonID));
buttons[i][j].setOnClickListener(this);
}
}
Thanks in advance!

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);
}
}

You can try making an int[] that holds all of your button IDs, and then iterate over that:
int[] buttonIDs = new int[] {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }
for(int i=0; i<buttonIDs.length; i++) {
Button b = (Button) findViewById(buttonIDs[i]);
b.setOnClickListener(this);
}

Take a look at these answers:
Android and getting a view with id cast as a string
Array of ImageButtons, assign R.view.id from a variable

you can Use tag if you want to access.
in onClick
int i=Integer.parseInt(v.getTag);
But you cant access that button like this.
simply create button programatically
by Button b=new Button(this);

create Custom Button in java code rather in Xml as i shown below
Button bs_text[]= new Button[some_value];
for(int z=0;z<some_value;z++)
{
try
{
bs_text[z] = (Button) new Button(this);
}
catch(ArrayIndexOutOfBoundsException e)
{
Log.d("ArrayIndexOutOfBoundsException",e.toString());
}
}

If your top level view only has those button views as children, you could do
for (int i = 0 ; i < yourView.getChildCount(); i++) {
Button b = (Button) yourView.getChildAt(i);
b.setOnClickListener(xxxx);
}
If there are more views present you'd need to check if the selected one is one of your buttons.

If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.
int id = 0;
switch(name) {
case "x":
id = R.id.x;
break;
etc.etc.
}
String value = findViewById(id);

To put it simply, here's a function for it
public View findViewByArrayName (String name, int i) {
buttonID = name + Integer.toString(i);
resID = getResources().getIdentifier(buttonID, "id", getPackageName());
return findViewById(resID);
}
Also unlike Python, Java is a compiled language, so it probably makes sense that there aren't any chances for dynamic variable names. Unless achieved through a certain approach like this one.

Related

Is it possible to iterate over an id string in the following manner?

for(int i=1;i<=3;i++)
{
String my_id="Ezequiel_1_"+i;
final TextView modelTextview = (TextView) findViewById(R.id.my_id);
modelTextview.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
the_controller.buttonController(v);
}
});
}
I arranged my ids in the previous manner and I don't want to set the method one by one. Is it possible to iterate like that?
Not directly, because strings aren't ids. There's two ways to do what you want though:
1)Data based
int textViewIds[] = [R.id.Ezequiel_1_1, R.id.Ezequiel_1_2, R.id.Ezequiel_1_3,...]
for(int id: textViewIds) {
TextView tv = (TextView) findViewById(id);
...
}
2)Name based
for(int i=0; i<numView, i++) {
int resourceId = resources.getIdentifier("Ezequiel_1_"+i, "id",
context.getPackageName());
TextView tv = (TextView) findViewById(id);
...
}
I prefer method 1 as it gives you clearer code and protection against off by 1 errors (they won't compile).

Looping through numerous Button variables

I have 50 Buttons coded like this:
button_list.set(0,(Button) findViewById(R.id.button0));
button_list.set(1,(Button) findViewById(R.id.button1));
button_list.set(2,(Button) findViewById(R.id.button2));
button_list.set(3,(Button) findViewById(R.id.button3));
button_list.set(4,(Button) findViewById(R.id.button4));
button_list.set(5,(Button) findViewById(R.id.button5));
button_list.set(6,(Button) findViewById(R.id.button6));
button_list.set(7,(Button) findViewById(R.id.button7));
button_list.set(8,(Button) findViewById(R.id.button8));
button_list.set(9,(Button) findViewById(R.id.button9));
button_list.set(10,(Button) findViewById(R.id.button10));
button_list.set(11,(Button) findViewById(R.id.button11));
button_list.set(12,(Button) findViewById(R.id.button12));
button_list.set(13,(Button) findViewById(R.id.button13));
button_list.set(14,(Button) findViewById(R.id.button14));
button_list.set(15,(Button) findViewById(R.id.button15));
.
.
.
How can I put this all in a loop?
When I run the below code I get NullPointerExceptions, I guess meaning the Buttons are not recognized when I attempt to use findViewById. Does anyone know what is wrong with the following code and how I can fix it?
Button[] bttn = new Button[50];
String ids[] = new String[50];
for(int i=0; i<50; i++)
{
ids[i] = "button" + Integer.toString(i);
}
for(int i=0; i<50; i++)
{
int resID = getResources().getIdentifier(ids[i], "id", "your.package.name");
bttn[i] = (Button) findViewById(resID);
}
for(int i=0; i<50; i++)
{
button_list.set(i, bttn[i]);
}
You can do it in a single loop
Button[] buttons = new Button[50];
for(int i = 0; i < buttons.length; i++){
button[i] = (Button) findViewById(getResources().getIdentifier("button" + i, "id", getPackageName());
buttonList.set(i, button[i]); // if your list is already built
//buttonList.add(button[1]); // if you are building your list
}
I use getPackageName() as I assume you are in an Activity, if that is not the case you need to get a reference to the context and use context.getPackageName(); This goes the same for the getResources() call as it also needs a reference to your context.

Properly implementing/initializing lots of new variables, taken from RadioGroups and Spinners

I am developing a simple questionnaire-like app which includes lots of radio buttons joined into groups and spinners. I have multiple activities (6); some of them having RBs and some Spinners to let the user answer the questions.
The following step, which I have trouble with, is how to fetch lots of selections (of all the radio buttons/choices) and possibly do that in a for loop (so I don't have to initialize each new variable 30+ times in a row for just one activity). I've already assigned IDs to all of the views, but am having a hard time how to actually fetch the selection, initialize a new var corresponding to the selection (let's say radio button 1 in radio group 1 gives me a new variable with a value of 1) and then make the variables available to all of the activities (should I use global when initializing?).
My failed attempt on generating 10 variables for the first "page"
public void goTo2(View v) {
checkRB();
Intent intent1 = new Intent(Vprasalnik1.this, Vprasalnik2.class);
startActivity(intent1);
finish();
}
public void checkRB()
{
for (int i=0;i<9;i++)
{
RadioButton "vRB" + i; //I'd like to loop and initialize vars by adding a number to them (vRB1, vRB2, ...)
}
}
Put variables into array like a
int size = 9;
RadioButton[] views = new RadioButton[size];
public static checkRB()
{
for(int i=0;i<size;i++)
{
views[i] = (RadioButton)findViewByID(...);//For example
}
}
Or make a structure :
public class Choise
{
int mRadioButtonChoise;
int mSpinnerChoise;
}
And use something like this:
...
Choise c = new Choise();
c.mRadioButtonChoise = yourRadioButtonID;
c.mSpinnerChoise = youtSpinnerChoiseID;
...
Using a variable to identify a resource:
RadioButton[] rb = new RadioButton[size];
public static checkRB()
{
for(int i=0;i<size;i++)
{
int id = context.getResources().getIdentifier("vRB" + i, "id", context.getPackageName())
rb[i] = (RadioButton)findViewByID(id);
}
}
If you have an array of RadioButtons then you can get all the values at the same time, however initializing them will have to be manual.
RadioButton rb[];
boolean rbc[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rbc=new boolean[200];
rb=new RadioButton[200]();
rb[0]=(RadioButton)findViewById(R.id.rb1);
rb[1]=(RadioButton)findViewById(R.id.rb2);
rb[2]=(RadioButton)findViewById(R.id.rb3);
rb[3]=(RadioButton)findViewById(R.id.rb4);
// many more.
}
public void checkRB()
{
for (int i=0;i<9;i++)
{
rbc[i]=rb.isChecked(); //I'd like to loop and initialize vars by adding a number to them (vRB1, vRB2, ...)
}
}
Then before starting your intent add all relevant data to it.
So I've managed to cramp up the radio buttons activity, so that it finally works. If anyone is interested - I've used tags in xml code to properly assign values (1, 2 and 3 for each group of buttons) and managed to get an output in my testToast. At least I didn't have to initialize all of the variables manually - I've been saving the values into an ArrayList and then appended to them via StringBuilder.
Thanks to everyone who tried to help - it turned out I've needed a bit more research, testing and teasing my half-awake brain.
btn = (Button) findViewById(R.id.v3_btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for(int i = 1; i <= 36; i++)
{
tmpRGid = "radioGroup_v3q" + i;
tmp2RGid = getResources().getIdentifier(tmpRGid, "id", getPackageName());
RGid = (RadioGroup) findViewById(tmp2RGid);
selectedOption = RGid.getCheckedRadioButtonId();
RBid = (RadioButton) findViewById(selectedOption);
addToIDList.add((String)RBid.getTag());
}
String testToast = "";
StringBuilder builder = new StringBuilder();
builder.append("Vaša izbira (");
for (int z=0; z < addToIDList.size(); z++) {
testToast = addToIDList.get(z);
builder.append(testToast + ", ");
}
builder.setLength(builder.length() - 2);
builder.append(") je bila shranjena.");
Toast.makeText(Vprasalnik3.this, builder, Toast.LENGTH_LONG).show();

Edit text of several buttons using a for loop

I have 16 buttons, whose names are "button1", "button2", and so on. Is there a way I can iterate through them using a for loop, by somehow appending the number value upon each iteration? Something like this:
for(int i = 1; i<17; i++ ){
Button b = (Button)findViewById(R.id.buttoni);
I know I can simply initialize each button in my onCreate() method, but I was just curious if I could do it in a way similar to my example code.
Thank you.
You can use getIdentifier :
for(int i = 1; i<17; i++ ){
int buttonId = getResources().getIdentifier("button"+i, "id", getPackageName());
Button b = (Button)findViewById(buttonId);
//Your stuff with the button
}
You can create an array of Button's and use getIdentifier method that allows you to get an identifier by its name.
final int number = 17;
final Button[] buttons = new Button[number];
final Resources resources = getResources();
for (int i = 0; i < number; i++) {
final String name = "btn" + (i + 1);
final int id = resources.getIdentifier(name, "id", getPackageName());
buttons[i] = (Button) findViewById(id);
}
In case someone is interested how to achive the same result using Java only
The solution above uses Android specific methods (such as getResources, getIdentifier) and can not be used in usual Java, but we can use a reflection and write a method that works like a getIdentifier:
public static int getIdByName(final String name) {
try {
final Field field = R.id.class.getDeclaredField(name);
field.setAccessible(true);
return field.getInt(null);
} catch (Exception ignore) {
return -1;
}
}
And then:
final Button[] buttons = new Button[17];
for (int i = 0; i < buttons.length; i++) {
buttons[i] = (Button) findViewById(getIdByName("btn" + (i + 1)));
}
NOTE:
Instead of optimizing this kind of code you should rethink your layout. If you have 17 buttons on the screen, a ListView is probably the better solution. You can access the items via index and handle onClick events just like with the buttons.

Dynamic Button Onclick Listener

I am trying to create dynamic buttons. When clicking a button it should go to the specified url assigned to the text of the button.
For testing, first I tried to get that ID, if it is equal it prints the value of i. But whenever I clicked any one button, instead of telling that particular i value, it enters into whole loop, and prints all the values of i starting from 1 to 19 (the number of buttons that are dynamically created)
And after printing all values from 1 to 19, the program is getting force closed saying Null pointer exception.
I even tried by placing the handler code outside onCreate(), but I'm still getting the same error.
for ( i = 0; i <itemList.getTitle().size()-1; i++) {
title[i] = new TextView(this);
title[i].setTextColor( -16711936 );
title[i].setTextSize(18);
title[i].setText("Title = "+itemList.getTitle().get(i));
description[i] = new TextView(this);
description[i].setTextColor(-16776961);
description[i].setText("Description = "+itemList.getDescription().get(i)+"......");
more[i]=new Button(this);
more[i].setText(itemList.getLink().get(i));
layout.addView(title[i]);
System.out.println("Title view is set");
layout.addView(description[i]);
//System.out.println("Description view is set");
layout.addView(more[i]);
more[i].setOnClickListener(listener);
}
private OnClickListener listener=new OnClickListener(){
public void onClick(View arg) {
int index = 0;
for (i = 0; i < more.length; i++)
{
if (more[i].getId() == arg.getId())
{
index = i;
System.out.println("Value of i onclick is"+i);
}
}
//System.out.println("Vlaue of I in onclick"+i);
//Uri uri=Uri.parse(itemList.getLink().get(i));
//startActivity(new Intent(Intent.ACTION_VIEW,uri));
//Toast.makeText(getApplicationContext(), "This button is clicked"+i+more[i].getText()+itemList.getLink().get(i),Toast.LENGTH_LONG).show();
}
}
You can use setTag() and getTag() method of View to identify different button.
for (i = 0; i < itemList.getTitle().size()-1; i++) {
...
more[i].setTag(i); // Use index of itemList as the tag
}
In onClick:
int index = (Integer)arg.getTag();
you can also set the id of button
more[i].setid(i);
int index = 0;
for (i = 0; i < more.length; i++)
{
if (more[i].getId() == arg.getId())
{
index = i;
System.out.println("Value of i onclick is"+i);
}
}
As you can see here, i is still in your for loop.
Put the System.out.println("Value of i onclick is"+i); outside of your for loop and it should work
PS: format your code, it's easier to read that way and you'll notice small mistakes like these more easily
I think this will help you..
set button tag also dynamic like
more[i].setId(i);
and also changed condition like
if (more[i].getId() == i) {
index = i;
}
hope this will help you...

Categories

Resources