Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android theme style for button not applying

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.

like image 666
DerpyNerd Avatar asked Oct 31 '13 13:10

DerpyNerd


People also ask

How to style a button in android studio?

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.

How to add button in android XML?

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.

How to add action to button in android studio?

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.


2 Answers

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

like image 113
IsidroGH Avatar answered Sep 19 '22 13:09

IsidroGH


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" />
like image 41
Dragan Avatar answered Sep 17 '22 13:09

Dragan