Issue
I'm confused on button styles for material design. I'd like to get colorful raised buttons like in the attached link., like the "force stop" and "uninstall" buttons seen under the usage section. Are there available styles or do I need to define them?
http://www.google.com/design/spec/components/buttons.html#buttons-usage
I couldn't find the default button styles.
Example:
<Button style="@style/PrimaryButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate"
android:id="@+id/button3"
android:layout_below="@+id/editText5"
android:layout_alignEnd="@+id/editText5"
android:enabled="true" />
If I try to change the background color of the button by adding
android:background="@color/primary"
all of the styles go away, such as the touch animation, shadow, rounded corner, etc.
Solution
You can use the Material Component library.
Add the dependency to your build.gradle
:
dependencies { implementation ‘com.google.android.material:material:1.3.0’ }
Then add the MaterialButton
to your layout:
<com.google.android.material.button.MaterialButton
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/app_name"
app:strokeColor="@color/colorAccent"
app:strokeWidth="6dp"
app:layout_constraintStart_toStartOf="parent"
app:shapeAppearance="@style/MyShapeAppearance"
/>
You can check the full documentation here and API here.
To change the background color you have 2 options.
- Using the
backgroundTint
attribute.
Something like:
<style name="MyButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name="backgroundTint">@color/button_selector</item>
//..
</style>
- It will be the best option in my opinion. If you want to override some theme attributes from a default style then you can use new
materialThemeOverlay
attribute.
Something like:
<style name="MyButtonStyle"
parent="Widget.MaterialComponents.Button">
<item name=“materialThemeOverlay”>@style/GreenButtonThemeOverlay</item>
</style>
<style name="GreenButtonThemeOverlay">
<!-- For filled buttons, your theme's colorPrimary provides the default background color of the component -->
<item name="colorPrimary">@color/green</item>
</style>
The option#2 requires at least the version 1.1.0
.
You can use one of these styles:
- Filled Button (default):
style="@style/Widget.MaterialComponents.Button
- Text Button:
style="@style/Widget.MaterialComponents.Button.TextButton"
- OutlinedButton:
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
OLD Support Library:
With the new Support Library 28.0.0, the Design Library now contains the MaterialButton
.
You can add this button to our layout file with:
<android.support.design.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="YOUR TEXT"
android:textSize="18sp"
app:icon="@drawable/ic_android_white_24dp" />
By default this class will use the accent colour of your theme for the buttons filled background colour along with white for the buttons text colour.
You can customize the button with these attributes:
app:rippleColor
: The colour to be used for the button ripple effectapp:backgroundTint
: Used to apply a tint to the background of the button. If you wish to change the background color of the button, use this attribute instead of background.app:strokeColor
: The color to be used for the button strokeapp:strokeWidth
: The width to be used for the button strokeapp:cornerRadius
: Used to define the radius used for the corners of the button
Answered By - Gabriele Mariotti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.