Ingo Guilherme Both Eyng Ingo Guilherme Both Eyng - 1 year ago 62
Android Question

Style not working entirely when applied on Button programmatically

Here is my style:

<style name="buttonQuestionStyle" parent="@style/Widget.AppCompat.Button.Colored">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">@color/white</item>
<item name="android:textSize">16sp</item>
<item name="android:padding">25dp</item>
<item name="android:layout_margin">10dp</item>
<item name="android:background">@color/questionButton</item>

And here my code:

Button btn = new Button(getActivity());
if (Build.VERSION.SDK_INT < 23) {
} else {

In the app:

Programmatically button appears like this:
programmatically button

And via layout it worked. Appears like this:
xml layout button

Here is my code in the XML Layout:

style="@style/buttonQuestionStyle" />

So... I dont know why it happens, and how fix it.

Answer Source

You cannot set a view's style programmatically yet, but you may find this thread useful.