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 <Button> 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 <Button> 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,
Example 1
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:text="Button"
android:background="#FFFFCC" />
Example 2
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#88FFFFCC" />
Example 3
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="@drawable/image_name" />
Change Button color using xml attribute
button.setBackgroundResource(R.drawable.image_name);
button.setBackgroundResource(Color.RED);
button.setBackgroundResource(Color.rgb(100, 100, 100));
More Posts related to Android,
- Android Error Unexpected cast to Button: layout tag was FrameLayout
- ADT quit unexpectedly error on Mac OSX Android Eclipse SDK
- Parsing Data for android-21 failed unsupported major.minor version 51.0
- Android Studio Ctrl Shift o auto import not working
- java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
- Android : How to make TextView Scrollable
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Integrating Android Facebook SDK 3.17.2 Tutorial
- Android R Cannot Be Resolved To A Variable
- Android : Exception raised during rendering: action_bar API 22
- How to take screenshot on Android
- Read Text file from SD Card : Android Programming
- How to make Android EditText not editable
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later.
- The declared package does not match the expected package Eclipse
- Can't Run SDK Manager find_java.bat issue
- What is Android Toast.LENGTH_SHORT and Toast. LENGTH_LONG durations
- Android Emulator Soft Back button action using Computer keyboard
- Multiline EditText in Android Example
- Use 5G Network on Android Emulator
- Make Android TextView Clickable like Buttons
- How to empty trash in Android Device
- Android : Execute some code after back button is pressed
- Disable Fading Edges Scroll Effect Android Views
- How To Disable Landscape Mode in Android Application
More Posts:
- SharePoint 2016 error - Could not find file ManageUserProfileServiceApplicationTenantSimplified.xml - SharePoint
- Base64 Encoding Decoding in Python Programming - Python
- Fix Microsoft Office 365 error code 135011 - Your organization has disabled this device - Microsoft
- Outlook - The mailbox isn't available. This may have occurred because the license for the mailbox has expired. - Microsoft
- Chrome : When Adobe flash player has finished updating, reload this page to active it - Chrome
- [Fix] zsh: command not found: python on Mac - Python
- [Fix] Docker Run unknown shorthand flag: 'r' in -rm - Docker
- Install SonarLint on Visual Studio Code - HowTos