I'm trying to add a style to a button in Xamarin Android from code, because the buttons will be generated dynamically and need to change colors based on other system events.
Using the code shown here, I expect that the two rows, one rendered from XML and the other from code, will look exactly alike. Instead, I see that the style ButtonDefaultTheme is only applied to the first row.
I know setting the style from code is possible based on this answer and this one. I think I could accomplish what I need using a layout inflater as in this answer, but I'm very curious why the code I have won't work.
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Main);
LinearLayout roomButtonLayout = FindViewById<LinearLayout>(Resource.Id.roomButtonLayout);
LinearLayout roomInfo = new LinearLayout(this)
{
Orientation = Orientation.Horizontal,
LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent, 1f)
};
roomInfo.SetGravity(GravityFlags.CenterVertical);
roomInfo.SetPadding(20, 10, 0, 10);
TextView roomNameDisplay = new TextView(this)
{
Gravity = GravityFlags.Left,
TextSize = 20, // defaults to scaled pixels
Text = "Room 100",
LayoutParameters = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WrapContent, LinearLayout.LayoutParams.WrapContent, 1f)
};
roomInfo.AddView(roomNameDisplay);
Button button2 = new Button(new ContextThemeWrapper(this, Resource.Style.ButtonDefaultTheme), null, 0)
{
Text = "View2"
};
roomInfo.AddView(button2);
roomButtonLayout.AddView(roomInfo);
}
}
In Main.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_height="wrap_content"
android:id="@+id/roomButtonLayout">
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:paddingLeft="20dp"
android:paddingRight="0dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:gravity="left"
android:textSize="20sp"
android:text="Room 100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:text="View1"
style="@style/ButtonDefaultTheme" />
</LinearLayout>
</LinearLayout>
In styles.xml
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<style name="ButtonDefaultTheme" parent="android:style/Widget.Holo.Button">
<item name="android:paddingBottom">0dp</item>
<item name="android:paddingTop">0dp</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:paddingRight">10dp</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:minWidth">100dp</item>
</style>
</resources>
Result:
Minimum API level needed: 19
Compiling using: 21 in actual app, 23 in test app. Same visual results in both.