New to android development, but not entirely new to Java. Right now my code inside the onCreate() method is quite basic:
Button b1 = (Button) findViewById(R.
Just a fun trick for you, since all your buttons are using the same listener, you could do something like this with less code (though it's less efficient, not likely to be noticeable though):
ViewGroup group = (ViewGroup)findViewById(R.id.myrootlayout);
View v;
for(int i = 0; i < group.getChildCount(); i++) {
v = group.getChildAt(i);
if(v instanceof Button) v.setOnClickListener(this)
}
Create an array of buttons and do it in a loop:
int[] ids = { R.id.button1, R.id.button2 , ........... };
Button[] buttons = new Buttons[ids.length];
for (int i = 0; i < ids.length; ++i) {
buttons[i] = (Button)findViewByIf(ids[i]);
buttons[i].setOnClickListener(this);
}
in onCreate
call get the root layout
ViewGroup rootLayout=(ViewGroup) findViewById(R.id.root_layout);
then pass it to this method, (using recursive for deep search for buttons)
public void setAllButtonListener(ViewGroup viewGroup) {
View v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
v = viewGroup.getChildAt(i);
if (v instanceof ViewGroup) {
setAllButtonListener((ViewGroup) v);
} else if (v instanceof Button) {
((Button) v).setOnClickListener(myButtonsListener);
}
}
}
good luck
Something a little less repetitive could be:
int[] ids = {R.id.button1, R.id.button2, ... };
for (int id:ids) {
Button b = (Button) findViewById(id);
b.setOnClickListener(this);
}