I’m trying to create buttons after i press a button ( this one is premade in XML ).
The thing is, i can do this in LinearLayout however when i switched to RelativeLayout everytime my buttoncreator method runs it deletes the previously created button and creates a new one.
to be more spesific;
My buttoncreator method
public void buttoncreator(String name,RelativeLayout.LayoutParams position,RelativeLayout layout){
positionrandomer(position);
final Button dummybutton = new Button(this);
dummybutton.setText(name);
//these are here for test,it works but still i have the same problem
position.addRule(RelativeLayout.BELOW,R.id.button1);
position.addRule(RelativeLayout.BELOW,R.id.button2);
dummybutton.setLayoutParams(position);
layout.addView(dummybutton);
return;
}
(position randomer is a method where it sets random margins ).
The place where i call creator method
Button luckbutton = (Button) findViewById(R.id.button1);
luckbutton.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
name="Blue";
buttoncreator(name,position,layout);
name="Blu4e";
buttoncreator(name,position,layout);
}
});
so, my intent is to create 2 buttons on 1 buttonclick, however it creates only 1. ( actually, it creates the first one then deletes it and creates the second one ).
Appereantly there is something i don’t understand with RelativeLayouts,
What i am doing wrong?
Thx in advance
I can suggest you a workaround to do the thing you want. Why don’t you create your buttons in your xml file and set their visibility in your
onCreate()toView.GONEand in button’sonClickyou set their visibility toView.VISIBLE. I think this will do the thing that you want and I don’t think you will have problem withRelativeLayout.