I am trying to make an application that generates buttons with random color but I don't even know how to generate these buttons help!!
I have tried to do it with the manual that our teacher has offered us but it is incomplete
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public void onClick (View v){
if (v.getClass().getSimpleName().equals("Button")) {
Button b = (Button) v;
}
}
public void Recorrer () {
View v;
GridLayout g = (GridLayout) findViewById(R.id.grLayout);
for (int i = 0; i < g.getChildCount(); i++) {
v = g.getChildAt(i);
Button b;
if (v.getClass().getSimpleName().contains("Button")) {
b = (Button) v;
b.setOnClickListener(this);
}
Log.e("Objetito: ", v.getClass().getSimpleName() + "<--->" +
v.toString());
}
}
public void añadeHijos (){
GridLayout g = (GridLayout) findViewById(R.id.grLayout);
Button b;
int iden;
for (int i = 0; i < 18; i++) {
b = new Button(this);
b.setLayoutParams(new
ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
iden = View.generateViewId();
b.setId(iden);
b.setText("botón" + i);
g.addView(b, i);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}