How to change Android Button Color using xml attribute and programatically using java
By default we have a grayish looking Button view for Android if you want to change the color you can do it either using XML attribute for tag or programmatically using java code, let's see both of them,
>Change Button color using XML attribute
All we need to do is add android:background attribute to the view xml tag, we can set colors as hex codes with opacity. We can also set image as background for a button using android:background:"@drawable/imageName attribute, lets see some examples,
Change Button color using xml attribute
button.setBackgroundResource(R.drawable.image_name);
button.setBackgroundResource(Color.RED);
button.setBackgroundResource(Color.rgb(100, 100, 100));
- appcompat_v7 errors after updates to API level 21 Material Theme
- adb: The Android Debug Bridge and Commands
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- How to make TextView Text Transparent [Android]
- Android is starting optimizing. app 1 of 1
- How to make Text in TextView bold and italic in Android
- Android Development - How to switch between two Activities
- Android : IOException: Unable to open sync connection!
- Android Toolbar example with appcompat_v7 21
- How to Change Android Title Bar Color?
- How to Add Padding to Android TextView
- JavaScript : redirect page to other url
- Programmatically Send an Email from Android App using Intent
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- [Soluiton] You already have the latest version of Android Studio installed
- Detect swipes on Android Activity
- Add Buttons at the bottom of Android Layout xml file
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Hide Navigation Bar from Android Screen Activity
- Android Disable EditText from Auto Focus on Activity load
- Toast not getting displayed Android App
- How to Enable Developers Option in Android Phones Settings
- Android : Duplicate registration for activity com.example.abc
- Android ListView turns Black or Flickers while Scrolling
- Android Studio Ctrl Shift o auto import not working
- Validate email address in Python using regular expression (regex) - Python
- How to Setup AWS Credentials using Visual Studio Code - AWS
- Which Python version is bundled with macOS Sonoma 14 by default - MacOS
- Nano Show Line Numbers - Linux
- How to Know the Line Number on Windows Notepad App - Windows-11
- [Fix] Springboot: Web application could not be started as there was no ServletWebServerFactory bean defined in the context. - Java
- macOS 13 Ventura - The New About this Mac Window - MacOS
- Java 8: Find the Max value in a List - Java
Popular Tag List
Author Info:
Rakesh (He/Him) has a Masters Degree in Computer Science with over 15+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
You can also reach out to him via e-mail: rakesh@code2care.org