So, I programmatically create new buttons and add them to LinearLayout, however I want to initialize these buttons with a predefined style. I spent some time finding a solution and tried out the answers, but I still can't get it to work.
When I add a new button to the layout, it should look like buttons (at the top) in this image .
I tried to create an xml file in res / values / and initialize the button with new Button(context, null, R.style.ChoiceButton), but this will not work, getting this to happen .
I also tried a workaround for creating a new xml layout for the button and with (Button)getLayoutInflater().inflate(R.layout.choice_buttton_layout, null), but this also did not work, as a result of this (two buttons to show lack of stock) .
RES / value / choice_button.xml
<resources>
<style name="ChoiceButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:layout_marginBottom">7dp</item>
<item name="android:minWidth">250dp</item>
<item name="android:background">#ff27ae60</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:enabled">true</item>
</style>
</resources>
Fragment from Main.java
public void btnAdd_click(View view) {
Button newBtn = new Button(getApplicationContext(), null, R.style.ChoiceButton);
newBtn.setText("new button");
newBtn.setId(Util.generateViewId());
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutTop);
layout.addView(newBtn);
}
activity_main.xml
A bit longer to insert here.
Is there something I am missing? Is it possible?
source
share