Вопрос

Hello I have asked this question before but perhaps it wasn't clear or was deemed irrelevant but I need to figure out how to make a button OnClick create a button on another XML layout.

I am very new to java and coding in general and have looked for the past few days but can not find any information on how I would go about this.

The closest I could get was another users question on stackoverflow where the method of keeping the button I want to create invisible and having it switch visibility OnClick of original button, but this isn't feasible.

While this method would work for the single button I need to click to generate the other button, the place it would generate it will also generate a different variety of buttons from other sources. The only way this seems possible is to have the dozens of invisible buttons from each source already stored there and that doesn't seem like the most logical way to approach it code wise.

I would greatly appreciate any help in this area, I'm not expecting anyone to show me how it's done or write me sample code if you could simply nudge me in the direction of an online guide,tutorial or information source I'd be very grateful,

Thanks for your patience.

Это было полезно?

Решение

Creating Button programmatically and adding to the LinearLayout.

//First get the reference of LinearLayout where you need to add the buttons.

LinearLayout ll=(LinearLayout)findViewbyId(R.id.llayout);

// Layout Param
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

/* This line of code to add the button in layout (implement your own logic i.e how many time you need to add the button and at which condition you need to add).*/

Button button = new Button(Context);
button.setText("dynamic button 1");
ll.addView(button,param);

Hope this achieve your goal.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top