Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Theming Material Design buttons involves altering various attributes, including background color, text color, shape, and elevation. Here's a guide on how to theme Material Design buttons in Android:
Ensure you have the Material Components library in your build.gradle
:
implementation 'com.google.android.material:material:<version>'
Replace <version>
with the appropriate library version.
Also, make sure your app's theme inherits from a Material Components theme, such as Theme.MaterialComponents.Light.DarkActionBar
.
XML Layout:
Declare your Material Button:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" app:backgroundTint="@color/yourButtonColor" android:textColor="@color/yourTextColor" app:cornerRadius="8dp" app:strokeWidth="2dp" app:strokeColor="@color/yourStrokeColor"/>
Replace yourButtonColor
, yourTextColor
, and yourStrokeColor
with your desired colors.
In your activity or fragment, you can set the theme attributes programmatically:
import android.graphics.Color; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.google.android.material.button.MaterialButton; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); MaterialButton materialButton = findViewById(R.id.materialButton); materialButton.setBackgroundColor(Color.BLUE); materialButton.setTextColor(Color.WHITE); materialButton.setStrokeWidth(2); materialButton.setStrokeColorResource(R.color.yourStrokeColor); } }
You can define custom styles for buttons to reuse them across your app:
res/values/styles.xml:
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar"> ... </style> <style name="CustomMaterialButton" parent="Widget.MaterialComponents.Button"> <item name="backgroundTint">@color/yourButtonColor</item> <item name="android:textColor">@color/yourTextColor</item> <item name="cornerRadius">8dp</item> <item name="strokeWidth">2dp</item> <item name="strokeColor">@color/yourStrokeColor</item> </style> </resources>
Apply the custom style in XML:
<com.google.android.material.button.MaterialButton style="@style/CustomMaterialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me"/>
These are basic theming techniques for the Material Button. Remember, there are many other attributes you can customize, including ripple color, elevation, icon, and icon gravity. Check the official Material Components documentation for more detailed theming guidance.
Customizing Material Design Button appearance in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" app:cornerRadius="8dp" app:strokeColor="@color/colorAccent" app:strokeWidth="2dp" />
Changing color and style of Material Design Button in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" app:backgroundTint="@color/customButtonColor" app:strokeColor="@color/customButtonStrokeColor" app:cornerRadius="12dp" app:strokeWidth="3dp" />
Material Design theming for Buttons in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" style="@style/Widget.MaterialComponents.Button" ... />
Using styles and themes with Material Design Button Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" style="@style/MyMaterialButton" ... />
<!-- styles.xml --> <style name="MyMaterialButton" parent="Widget.MaterialComponents.Button"> <item name="backgroundTint">@color/customButtonColor</item> <item name="strokeColor">@color/customButtonStrokeColor</item> <!-- Add other style attributes as needed --> </style>
Adding icon and text to Material Design Button with theming:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" app:icon="@drawable/ic_add" app:iconGravity="textStart" app:iconPadding="8dp" app:backgroundTint="@color/customButtonColor" app:strokeColor="@color/customButtonStrokeColor" ... />
Material Design Button animations and theming in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" style="@style/Widget.MaterialComponents.Button.OutlinedButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" android:layout_margin="16dp" android:translationZ="2dp" android:stateListAnimator="@anim/button_state_animator" />
Themed Material Design Button with XML layout in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" style="@style/Widget.MaterialComponents.Button" ... />
Applying custom colors to Material Design Button Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click me" app:backgroundTint="@color/customButtonColor" app:strokeColor="@color/customButtonStrokeColor" ... />
Material Design Button theming in XML layout in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" style="@style/Widget.MaterialComponents.Button" ... />
Changing size and elevation of Material Design Button with theming:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="200dp" android:layout_height="60dp" android:text="Click me" android:elevation="8dp" ... />
Material Design Button states and theming in Android:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" style="@style/Widget.MaterialComponents.Button" app:backgroundTint="@color/customButtonColorStateList" ... />
Custom drawable for Material Design Button in Android theming:
<com.google.android.material.button.MaterialButton android:id="@+id/materialButton" android:layout_width="wrap_content" android:layout_height="wrap_content" app:icon="@drawable/custom_button_icon" ... />
Creating themed Material Design Button with Kotlin in Android:
val materialButton: MaterialButton = findViewById(R.id.materialButton) materialButton.backgroundTintList = ColorStateList.valueOf(ContextCompat.getColor(this, R.color.customButtonColor)) materialButton.text = "Click me"