I have three simple buttons in my main activity view. What I am trying to do is apply a buttonstyle to all my buttons but I fail to do so.
Here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/minecraft_portrait"
android:alpha="0.75"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/txtAppName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="@string/app_name_readable"
android:textSize="32sp"
android:textColor="@android:color/white" />
<Button
android:id="@+id/btnCrafting"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/button_selector"
android:onClick="craftingButtonClicked"
android:text="@string/crafting" />
<Button
android:id="@+id/btnServerCommands"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/btnCrafting"
android:layout_below="@id/btnCrafting"
android:layout_marginTop="20dp"
android:background="@drawable/button_selector"
android:onClick="commandsButtonClicked"
android:textColor="@android:color/white"
android:text="@string/servercommands" />
<Button
android:id="@+id/btnVideos"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/btnServerCommands"
android:layout_below="@id/btnServerCommands"
android:layout_marginTop="20dp"
android:background="@drawable/button_selector"
android:onClick="videosButtonClicked"
android:text="@string/videos" />
</RelativeLayout>
And this is my style.xml from the res/values folder:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppBaseTheme" parent="android:Theme.Light">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:buttonStyle">@style/ButtonTheme</item>
</style>
<style name="ButtonTheme" parent="android:Widget.Button">
<item name="android:textColor">@android:color/white</item>
<item name="android:background">@android:color/darker_gray</item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">32sp</item>
</style>
</resources>
And my theme definition from the manifest file:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
....
Any ideas why the style isn't applying to my buttons? I have set the textcolor of one button to white just to see any difference.
To add a button, that has an Android style all you need to do is to drag and drop a button from the Palette to your layout. For most versions that would mean a grey button with all corners at 2dp roundness. Check our blog, if you need to learn more about using Android Studio Layout Editor.
According to official documentation of Buttons provided by Android. You can first create Button in your . xml file. And then cast your button with Button Class and set ClickListener.
Add Action Buttons To add actions to the action bar, create a new XML file in your project's res/menu/ directory. The app:showAsAction attribute specifies whether the action should be shown as a button on the app bar.
When using the support library the button style must be applied as:
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:buttonStyle">@style/ButtonTheme</item>
<item name="buttonStyle">@style/ButtonTheme</item>
</style>
Note the duplication but with no namespace
The code that you post is working. Please add style on button in xml, to see if that is working for you.
<Button
style="@style/ButtonTheme"
android:id="@+id/btnCrafting"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/button_selector"
android:onClick="craftingButtonClicked"
android:text="@string/crafting" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With