4this
4this

Reputation: 759

Android - How to change the color of all button when it being clicked

What I would like to know is, if there's a way to change any click color at the whole Activity.

I've set up a the maniest the next Activity -

                <activity android:name="com.example.test.MainActivity" 
                android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar" 
                 android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" >
            </action>

            <category android:name="android.intent.category.NORMAL" >
            </category>
        </intent-filter>
    </activity>

As you can see I am using the next theme for the Activity -

  android:theme="@style/Theme.Base.AppCompat.Light.DarkActionBar" 

This make any click color to be blue- meaning when a button or anything else is being pressed it turning into blue when it's clicked.

Now what i would like to know is there any way to change the blue color into a diffrent color, and now by changing the background for each view item that i'm gonna use.

Thanks for any kind of help

Upvotes: 0

Views: 618

Answers (1)

Ando Masahashi
Ando Masahashi

Reputation: 3122

file name: /res/drawable/button_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@color/color1"/>
<item android:state_focused="true" android:drawable="@color/color1"/>
<item android:state_pressed="true" android:drawable="@color/color1"/>
<item android:drawable="@color/color1"/> 

Take this file into your Button Style like this:

create new style in your resource value folder like below example and change your application attribute android:theme="@style/CustomActionBarTheme"

 <!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme" parent="@style/Theme.Base.AppCompat.Light.DarkActionBar>

    <item name="android:buttonStyle">@style/srp_button</item>

</style>
<style name="srp_button" parent="@android:style/Widget.Button">
<itemname="android:background">@drawable/button_color</item> <!-- Here isspecifiedtext color -->
</style>

Upvotes: 3

Related Questions