Android : Getting id's by using for loop?

on Tuesday, August 12, 2014


I simply want to know . is there a way to create an id's using for loop


I have 10 buttons in xml . there id's are button1,button2,button3... button10 Now i create an Array of Button in java class and do it like this



public class Menu extends Activity
{
Button[] arrayButton=new Button[10];

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);

init();
}

private void init()
{
for(int i=1 ; i<9 ; i++)
{
String abc = "but"+String.valueof(i);
int x = Integer.parseInt(abc);


Log.d("abc", abc);

Log.d("x", String.valueOf(x) );

//arrayButton[i] = (Button) findViewById(R.id.x); // giving error

//arrayButton[i].setText("Hello:");
}
}

}


I want to know how can i do this kind of work . Getting all button using for loop to make my work faster and some time when i want to set the text of all the buttons .


0 comments:

Post a Comment