Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android default button color

When I open a new android studio project, the default color for button is purple. I want the default color to be the gray default button color(I assume you know what I mean). I tried to change the color via xml and java and nothing worked. I want that the default button color will be gray without I'd have to change it every time. enter image description here

enter image description here

themse.xml:

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HangmanGame" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_500</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/white</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

themes.xml(night)

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.HangmanGame" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_200</item>
    <item name="colorPrimaryVariant">@color/purple_700</item>
    <item name="colorOnPrimary">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_200</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>
like image 690
Tomer Avatar asked Oct 17 '20 08:10

Tomer


People also ask

What is the default button color android?

Show activity on this post. When I open a new android studio project, the default color for button is purple.

What is the default button color?

By default, a button has a white background and black text. Using the CSS background-color property, we can change a button's background color.

Why is my button pink on Android studio?

You have placed your map_button_text resource in your res/drawable folder, which tells Android to interpret it as a Drawable, not a color. If you move that file to res/color and refer to it via @color/map_button_text you should get what you want.

How to customize a button to set text and color in Android?

This example demonstrates how do I customize a button to set text and color in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. Step 3 − Add the following code to src/MainActivity.java

How to change the color of button programmatically?

To change the color of button programmatically Here it is : Button b1; //colorAccent is the resource made in the color.xml file , you can change it. b1.setBackgroundResource(R.color.colorAccent);

How do I change the color of my accent on Android?

Go back to the main Settings menu once again. Then, tap on the System selection. Then, tap on the Advanced option on the menu. Then, scroll down and tap on the Accent color part of the menu.

What is a button in Android Studio?

A Button is a user interface that are used to perform some action when clicked or tapped. In this article, we will try to change the shape and color of Button to various designs, like: Please refer to this article to see in detail about how to create a new Android Studio project.


2 Answers

Since you are using a Theme.MaterialComponents.* theme the default background color of the Button (which is replaced by a MaterialButton) is the colorPrimary defined in your app theme.
In your case:

<item name="colorPrimary">@color/purple_500</item>

You can change this value (but this will affect all widgets).

If you want to change globally the button style in your app you can also add the materialButtonStyle attribute in your app theme:

<style name="Theme.HangmanGame" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
   <item name="materialButtonStyle">@style/Widget.App.Button</item>
</style>

with:

<style name="Widget.App.Button" parent="Widget.MaterialComponents.Button">
    <item name="backgroundTint">@color/...</item>
</style>

If you want to change this color only in the button you can use also the app:backgroundTint attribute removing the android:background attribute:

<Button
    app:backgroundTint="@color/..."/>

If you want to use a custom background using the android:background attribute you have to add app:backgroundTint="@null" to avoid that the button is tinted.

like image 169
Gabriele Mariotti Avatar answered Oct 21 '22 01:10

Gabriele Mariotti


I think the easiest way is to go into three xml files: src\main\res\values\colors.xml, src\main\res\values\themes.xml, and src\main\res\values-night\themes.xml

In the colors.xml, add a color and call it "button", then set the color to what you want. For a simple list of color codes, see this answer: https://stackoverflow.com/a/7323234/5374362

The line would look something like this:

<color name="button">#808080</color>  //That code is the gray you want.

In the two themes files, change colorPrimary to "@color/button"

This will affect every button in the app.

like image 27
swordguy8 Avatar answered Oct 21 '22 00:10

swordguy8