When I programmatically create a button as shown below, none of the custom styles are considered and I lose all of the default stylings (such as default background color etc..)
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.activity_main);
Button button = new Button(this, null, R.style.LocButtonStyle);
layout.addView(button);
button.setText("Ok");
}
}
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="LocButtonStyle" parent="@android:style/Widget.Button">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:colorBackground">#ff0000</item>-->
<item name="android:textSize">160dp</item>
</style>
If I programatically create a Button without the styles the default styling is OK, but that's obviously not what I want.
Button button = new Button(this);
I have only seen workarounds for this, but I am yet to find the answer why this doesn't work.