Ingo Guilherme Both Eyng Ingo Guilherme Both Eyng - 17 days ago 10
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>
</style>


And here my code:

Button btn = new Button(getActivity());
btn.setText(ojb.getText());
if (Build.VERSION.SDK_INT < 23) {
btn.setTextAppearance(getActivity(), R.style.buttonQuestionStyle);
} else {
btn.setTextAppearance(R.style.buttonQuestionStyle);
}


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:

<Button
android:text="Question"
style="@style/buttonQuestionStyle" />


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

Answer

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