Tinting the background of an outlined Material Components button - android

I'm trying to create an outlined Material Components button, however I need it to have a semi-transparent background in addition to the stroke.
This is my XML code so far:
<android.support.design.button.MaterialButton
android:id="#+id/foo"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:backgroundTint="#CFFF"
app:strokeColor="#color/colorAccent"
app:strokeWidth="2dp" />
And this is what this looks like:
The issue is that some of the background is visible outside the stroke around the button (the larger the stroke width, the more white pixels getting out).
For example, here's a 5dp stroke:
Is there a way to fix this, a better way to set the background color, or anything?

Just use the Widget.MaterialComponents.Button.OutlinedButton style:
<com.google.android.material.button.MaterialButton
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeWidth="2dp"
app:strokeColor="#color/colorAccent"
app:backgroundTint="#3ad64f"
.../>

If you create a style of that you have to remove the app: In my example this style is called SecondButton
<style name="SecondButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:theme">#style/SecondButtonTheme</item>
<item name="backgroundTint">#color/second_button_back</item>
<item name="strokeColor">#color/second_button_text</item>
<item name="strokeWidth">1dp</item>
<item name="android:textColor">#color/second_button_text</item>
</style>
Use it like that in you
<com.google.android.material.button.MaterialButton
style="#style/SecondButton"
.../>

You could try to create your button in drawables directly in xlm based on this :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="backgroundcolor"/>
<stroke android:color="strokecolor" android:width="2dp" />
<!--corners allow us to make the rounded corners button-->
<corners android:radius="15dp" />
</shape>
</item>
</selector>
founded here : https://android--code.blogspot.fr/2015/01/android-rounded-corners-button.html
and use it like this in your layout :
android:background="#drawable/nameofbutton.xml"

Related

Customise Android SwitchCompat

In my Android app, I have a list with a SwitchCompat to filter the list data. The default theme can't serve my purpose what I need is a iOS like switch. I have no idea how can I customize it to look exactly like the iOS switch. Thanks for reading.
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/switch_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#xml/custom_switch_background_main"
android:theme="#style/AppTheme.SwitchOverlay"/>
This is what I have archived so far
custom_switch_background_main.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#xml/custom_switch_background" />
<item android:state_checked="true" android:drawable="#xml/custom_switch_background" />
</selector>
custom_switch_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="50dp" />
<solid android:color="#color/transparent" />
<stroke
android:width="1dp"
android:color="#color/hello" />
</shape>
This is what I want
You can achieve something like:
with the standard MaterialButtonToggleGroup:
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:selectionRequired="true"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.rounded"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM1"/>
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.App.rounded"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ITEM2"/>
</com.google.android.material.button.MaterialButtonToggleGroup>
with:
<style name="ShapeAppearanceOverlay.App.rounded" parent="">
<item name="cornerSize">50%</item>
</style>
If you want the border you can wrap the MaterialButtonToggleGroup in a container (LinearLayout, CardView....) applying a stroke a small padding.
If you want the button with rounded corners on both side you can check something like this question.
I think you should use sliding tab layout instead of switchcompat to fulfill your requirement. I have found one third party library that's looks like same switch as you want.
Click Here to Use This Library
I achieved the design by using a custom vector (and the text inside was a mask) which was replacement for the switch’s state indicator. then I changed the switch compat background vector with a custom border design as well to achieve the border. I’ll update this answer again soon.

How to make buttons rounded with Material Design Theming?

I want my button to have rounded corners like:
// Image taken from google
To achieve this with material theming in android is to set the: shapeAppearanceSmallComponent to have a rounded corner.
But setting shapeAppearanceSmallComponent also affects all other components such as EditText so now they are rounded as well.
So instead of setting it to shapeAppearanceSmallComponent, I created a shapeMaterialOverlay. Set this overlay to a buttonStyle and set this button style in the theme as the default button style.
It works but only for default buttons. If I needed a TextButton as such:
<Button
...
style="#style/Widget.MaterialComponents.Button.TextButton"/>
The TextButton won't be rouned. So as a workaround, I created MyTextButton style which extends from TextButton and set the shapeOverlay there as well.
so Now if I need a TextButton, I'll do:
<Button
...
style="#style/Widget.MaterialComponents.Button.MyTextButton"/>
instead.
I will have to do this for all other button types. I was wondering whether this approach is correct and if not, can anyone guide me on how to properly do this?
Thank you very much.
Just use the app:shapeAppearanceOverlay attribute in the layout.
<com.google.android.material.button.MaterialButton
app:shapeAppearanceOverlay="#style/buttomShape"
.../>
with:
<style name="buttomShape">
<item name="cornerFamily">rounded</item>
<item name="cornerSize">50%</item>
</style>
The only way to apply it to all buttons is to define custom styles for all the button styles as you are just doing. Something like:
<style name="...." parent="Widget.MaterialComponents.Button">
<item name="shapeAppearance">#style/buttomShape</item>
</style>
<style name="..." parent="Widget.MaterialComponents.Button.TextButton">
<item name="shapeAppearance">#style/buttomShape</item>
</style>
You can set a background drawable on your button. The drawable could look like this:
<shape android:shape="rectangle">
<solid android:color="#android:color/blue" />
<corners android:radius="10dp" />
</shape>
This will give you rounded corners for your button background.
You know there is a site for creating custom layouts for buttons
here is the Link
it is a auto generated Code you don't have to code. Maybe it will be helpful for everyone
Create a drawable xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<stroke
android:color="#color/colorPrimary"
android:width="1dp" />
<corners
android:radius="30dp"
android:bottomLeftRadius="30dp"
/>
<solid
android:color="#4c95ec"
/>
</shape>

Xml: stroke, border, outline text inside button

I want to add a white stroke to black text inside a button. I can figure out to do this with regular textview but not when it comes to a button.
Thi is regarding Xml android studio btw.
activity_main.xml:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip"
android:background="#drawable/login_button_style"
android:text="Log Ind"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editTextPass"
android:layout_alignEnd="#+id/editTextPass"
android:clickable="true" /><![CDATA[
/>
login_button_style.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#d7d7d7"/>
<corners
android:bottomRightRadius="15dp"
android:bottomLeftRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp"/>
<stroke android:color="#d10f0f" android:width="3dp" />
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="shadoweffect">
<item name="android:paddingLeft">4px</item>
<item name="android:paddingBottom">4px</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">25sp</item>
<item name="android:shadowColor">#000000</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">0</item>
<item name="android:shadowRadius">3</item>
</style>
</resources>
can someone please enlighten me?
Look at this answer to a similar question on how to make a border. He creates a new file that is a shape of just that color then stores it as a drawable to be used as the background image
https://stackoverflow.com/questions/7690416/android-border-for-button
Just change the color to white and there you go
EDIT:
Actually seeing now. You want the letters to be outlined in white.
So my best guess would be to draw the entire image with the text and border up in Photoshop/Sketch. And then save that as a drawable. To set for your buttons background. Make sure to set the buttons text to just quotes since you will already have the text in the Photoshop mockup
android:text = ""

Material design button with border

I know how to make Material Design button with color fill:
style="#style/Widget.AppCompat.Button.Colored"
And no-bordered transparent button:
style="#style/Widget.AppCompat.Button.Borderless.Colored"
However, is there a way to make Material design bordered (transparent inside) button? Something like below?
You can also use the Material Components for Android.
Add the dependency to your build.gradle:
dependencies { implementation 'com.google.android.material:material:1.3.0' }
In this case you can use the MaterialButton in your layout file:
<com.google.android.material.button.MaterialButton
....
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:cornerRadius=".."
app:strokeColor="#color/colorPrimary"/>
Apply the style #style/Widget.MaterialComponents.Button.OutlinedButton.
In your case use the app:cornerRadius attribute to change the size of corner radius. This will round off the corners with specified dimensions.
Use te attribute app:strokeColor and app:strokeWidth to change the color and the width of the border.
You can also customize the corners using ShapeApperance (it requires version 1.1.0)
<style name="MyButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="shapeAppearanceOverlay">#style/MyShapeAppearance</item>
</style>
<style name="MyShapeAppearance" parent="">
<item name="cornerFamilyTopLeft">rounded</item>
<item name="cornerFamilyBottomLeft">rounded</item>
<item name="cornerFamilyTopRight">cut</item>
<item name="cornerFamilyBottomRight">cut</item>
<item name="cornerSize">8dp</item>
</style>
The official doc is here and all the android specs here.
With jetpack compose you can use the OutlinedButton and the border attribute:
OutlinedButton(
onClick = { },
border = BorderStroke(1.dp, Color.Blue),
shape = RoundedCornerShape(8.dp)
) {
Text(text = "Save")
}
OLD (support library)
With the new Support Library 28.0.0, the Design Library now contains the Material Button.
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="XXXX"
android:textSize="18sp"
app:icon="#drawable/ic_android_white_24dp" />
You can customize the button with these attributes:
app: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 stroke
app:strokeWidth: The width to be used for the button stroke
Also
Here's how to do it correctly.
What you need to do is
1 - Create shape drawable with stroke
2 - Create ripple drawable
3 - Create selector drawable for less than v21
4 - Create a new style for button with border
5 - Apply style on button
1 - Create shape with stroke
btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#color/colorAccent">
</stroke>
<solid android:color="#color/colorTransparent"/>
<corners
android:radius="5dp">
</corners>
</shape>
2 - Create ripple drawable
drawable-v21/bg_btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorOverlay">
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/colorAccent"/>
<corners android:radius="5dp"/>
</shape>
</item>
<item android:id="#android:id/mask">
<shape>
<stroke
android:width="2dp"
android:color="#color/colorAccent"/>
<solid android:color="#android:color/white"/>
<corners android:radius="5dp"/>
</shape>
</item>
</ripple>
android:id="#android:id/mask" is required to have ripple touch feedback on the button. The layer that is marked as mask is not visible on screen, its just for touch feedback.
3 - Create selector drawable for less than v21
drawable/bg_btn_outline.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_outline" android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorOverlay"/>
</shape>
</item>
<item android:drawable="#drawable/btn_outline" android:state_focused="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorOverlay"/>
</shape>
</item>
<item android:drawable="#drawable/btn_outline"/>
</selector>
4 - Create a new style for button with border
All resources that that are needed to create the style are given above, that's how your style should look like
<style name="ButtonBorder" parent="Widget.AppCompat.Button.Colored"/>
<style name="ButtonBorder.Accent">
<item name="android:background">#drawable/bg_btn_outline</item>
<item name="android:textColor">#color/colorAccent</item>
<item name="android:textAllCaps">false</item>
<item name="android:textSize">16sp</item>
<item name="android:singleLine">true</item>
</style>
4 - Apply style on button
<Button
style="#style/ButtonBorder.Accent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
That's pretty much it. Here's a sample of how the buttons looks now.
You could do this pretty easily by setting Material Design Button's style attribute to #style/Widget.MaterialComponents.Button.OutlinedButton and setting app:strokeColor attribute value to your preferred color.
example:
<com.google.android.material.button.MaterialButton
android:text="Rounded outlined button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/btnRound"
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
app:strokeColor="#color/colorPrimaryDark"/>
References:
https://material.io/develop/android/docs/getting-started/
https://material.io/develop/android/components/material-button/
Credits to #NomanRafique for the detailed answer! However, because of the custom background, we've lost a few important things:
Button's height is bigger than the default Widget.AppCompat.Button
Paddings
Enable/Disable states
In case you're wondering, here is how the default background looks like: https://android.googlesource.com/platform/frameworks/support/+/a7487e7/v7/appcompat/res/drawable-v21/abc_btn_colored_material.xml
By reusing original insets, paddings and color selectors, in a simple case we can come up with something like this (all the values are default and come from android support/material library) :
drawable-v21/bg_btn_outlined.xml
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="#dimen/abc_button_inset_horizontal_material"
android:insetTop="#dimen/abc_button_inset_vertical_material"
android:insetRight="#dimen/abc_button_inset_horizontal_material"
android:insetBottom="#dimen/abc_button_inset_vertical_material">
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/abc_btn_colored_borderless_text_material"/>
<corners android:radius="#dimen/abc_control_corner_material"/>
<padding
android:bottom="#dimen/abc_button_padding_vertical_material"
android:left="#dimen/abc_button_padding_horizontal_material"
android:right="#dimen/abc_button_padding_horizontal_material"
android:top="#dimen/abc_button_padding_vertical_material"/>
</shape>
</item>
<item android:id="#android:id/mask">
<shape>
<stroke
android:width="2dp"
android:color="#color/abc_btn_colored_borderless_text_material"/>
<solid android:color="#android:color/white"/>
<corners android:radius="#dimen/abc_control_corner_material"/>
</shape>
</item>
</ripple>
</inset>
styles.xml
<style name="Button.Outlined" parent="Widget.AppCompat.Button.Borderless.Colored">
<item name="android:background">#drawable/bg_btn_outlined</item>
</style>
At this point, we should have an outlined button that responds to touches, respects the enabled="false" state and of the same height as the default Widget.AppCompat.Button:
Now, from here you can start customizing colors by providing your own implementation of the #color/abc_btn_colored_borderless_text_material color selector.
In your XML use this,
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Its a button"
android:textColor="#android:color/white"
android:textSize="#dimen/_12ssp"
app:backgroundTint="#android:color/transparent"
app:strokeColor="#android:color/white"
app:strokeWidth="#dimen/_1sdp" />
where
app:backgroundTint is used for background color
app:strokeColor is border color
app:strokeWidth is border width
This is how I do buttons only with a border and the ripple effect on Lollipop and above. Just like the AppCompat buttons, these have a fallback pressed effect on lower APIs (if you need ripples on lower APIs, you need to use an external library). I use FrameLayout because it's cheap. The color of the text and the border is black, but you can change it with a custom one:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:background="#drawable/background_button_ghost">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:selectableItemBackground"
android:gravity="center"
android:padding="14dp"
android:textSize="16sp"
android:textAllCaps="true"
android:textStyle="bold"
android:textColor="#android:color/black"
android:text="Text"/>
</FrameLayout>
drawable/background_button_ghost.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="2dp"
android:color="#android:color/black"/>
<solid android:color="#color/transparent"/>
</shape>
If I have missed something, please leave a comment and I will update the answer.
You can do this with Jetpack Compose too. To do it, just create a composable function with a OutlinedButton and pass the as parameter the border you want:
#Composable
fun OutlineButton() {
OutlinedButton(
onClick = { //TODO - implement click here },
border = BorderStroke(1.dp, Color.Blue), // <-- border property
shape = RoundedCornerShape(corner = CornerSize(20.dp)),
colors = ButtonDefaults.outlinedButtonColors(contentColor = Color.Blue)
){
Text(text = "CONFIRM")
}
}
Just use MaterialButton with an outline style.
<com.google.android.material.button.MaterialButton
...
style="#style/Widget.MaterialComponents.Button.OutlinedButton"
android:text="Delete Mentor Manager"
app:strokeColor="#color/...."
app:strokeWidth="1dp" />
Simply you can use this code. Its looks soo Good.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#F48025"
android:text="login"
android:textColor="#color/colorWhite" />
</LinearLayout>
Here border color is:
android:background="#ffffff"
And background color is:
android:backgroundTint="#F48025"
<Button
android:id="#+id/btn_add_discussion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="15dp"
android:padding="8dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:backgroundTint="#80ffffff"
android:textColor="#color/colorPrimaryDark"
style="#style/btn_start_new_discussion"
android:text="Start new discussion" />
Put below code in Styles.xml file :
<style name="btn_start_new_discussion">
<item name="android:layout_marginTop">15dp</item>
<item name="strokeWidth">2dp</item>
<item name="strokeColor">#color/colorPrimary</item>
<item name="cornerRadius">10dp</item>
</style>

Android Material Design Button Styles

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.
I will add my answer since I don't use any of the other answers provided.
With the Support Library v7, all the styles are actually already defined and ready to use, for the standard buttons, all of these styles are available:
style="#style/Widget.AppCompat.Button"
style="#style/Widget.AppCompat.Button.Colored"
style="#style/Widget.AppCompat.Button.Borderless"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
Widget.AppCompat.Button:
Widget.AppCompat.Button.Colored:
Widget.AppCompat.Button.Borderless
Widget.AppCompat.Button.Borderless.Colored:
To answer the question, the style to use is therefore
<Button style="#style/Widget.AppCompat.Button.Colored"
.......
.......
.......
android:text="Button"/>
How to change the color
For the whole app:
The color of all the UI controls (not only buttons, but also floating action buttons, checkboxes etc.) is managed by the attribute colorAccent as explained here.
You can modify this style and apply your own color in your theme definition:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="colorAccent">#color/Orange</item>
</style>
For a specific button:
If you need to change the style of a specific button, you can define a new style, inheriting one of the parent styles described above. In the example below I just changed the background and font colors:
<style name="AppTheme.Button" parent="Widget.AppCompat.Button.Colored">
<item name="colorButtonNormal">#color/Red</item>
<item name="android:textColor">#color/White</item>
</style>
Then you just need to apply this new style on the button with:
android:theme="#style/AppTheme.Button"
To set a default button design in a layout, add this line to the styles.xml theme:
<item name="buttonStyle">#style/btn</item>
where #style/btn is your button theme. This sets the button style for all the buttons in a layout with a specific theme
Simplest Solution
Step 1: Use the latest support library
compile 'com.android.support:appcompat-v7:25.2.0'
Step 2: Use AppCompatActivity as your parent Activity class
public class MainActivity extends AppCompatActivity
Step 3: Use app namespace in your layout XML file
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
Step 4: Use AppCompatButton instead of Button
<android.support.v7.widget.AppCompatButton
android:id="#+id/buttonAwesome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Awesome Button"
android:textColor="#color/whatever_text_color_you_want"
app:backgroundTint="#color/whatever_background_color_you_want"/>
If I understand you correctly, you want to do something like this:
In such case, it should be just enough to use:
<item name="android:colorButtonNormal">#2196f3</item>
Or for API less than 21:
<item name="colorButtonNormal">#2196f3</item>
In addition to Using Material Theme Tutorial.
Animated variant is here.
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 effect
app: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 stroke
app:strokeWidth: The width to be used for the button stroke
app:cornerRadius: Used to define the radius used for the corners of the button
Here is how I got what I wanted.
First, made a button (in styles.xml):
<style name="Button">
<item name="android:textColor">#color/white</item>
<item name="android:padding">0dp</item>
<item name="android:minWidth">88dp</item>
<item name="android:minHeight">36dp</item>
<item name="android:layout_margin">3dp</item>
<item name="android:elevation">1dp</item>
<item name="android:translationZ">1dp</item>
<item name="android:background">#drawable/primary_round</item>
</style>
The ripple and background for the button, as a drawable primary_round.xml:
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/primary_600">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="1dp" />
<solid android:color="#color/primary" />
</shape>
</item>
</ripple>
This added the ripple effect I was looking for.
Beside android.support.design.button.MaterialButton (which mentioned by Gabriele Mariotti),
There is also another Button widget called com.google.android.material.button.MaterialButton which has different styles and extends from AppCompatButton:
style="#style/Widget.MaterialComponents.Button"
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
style="#style/Widget.MaterialComponents.Button.TextButton"
style="#style/Widget.MaterialComponents.Button.Icon"
style="#style/Widget.MaterialComponents.Button.TextButton.Icon"
Filled, elevated Button (default):
style="#style/Widget.MaterialComponents.Button"
Filled, unelevated Button:
style="#style/Widget.MaterialComponents.Button.UnelevatedButton"
Text Button:
style="#style/Widget.MaterialComponents.Button.TextButton"
Icon Button:
style="#style/Widget.MaterialComponents.Button.Icon"
app:icon="#drawable/icon_24px" // Icons can be added from this
A text Button with an icon::
Read: https://material.io/develop/android/components/material-button/
A convenience class for creating a new Material button.
This class supplies updated Material styles for the button in the
constructor. The widget will display the correct default Material
styles without the use of the style flag.
Here is a sample that will help in applying button style consistently across your app.
Here is a sample Theme I used with the specific styles..
<style name="MyTheme" parent="#style/Theme.AppCompat.Light">
<item name="colorPrimary">#color/primary</item>
<item name="colorPrimaryDark">#color/primary_dark</item>
<item name="colorAccent">#color/accent</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
</style>
<style name="ButtonAppTheme" parent="android:Widget.Material.Button">
<item name="android:background">#drawable/material_button</item>
</style>
This is how I defined the button shape & effects inside res/drawable-v21 folder...
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="2dp" />
<solid android:color="#color/primary" />
</shape>
</item>
</ripple>
2dp corners are to keep it consistent with Material theme.
I tried a lot of answer & third party libs, but none was keeping the border and raised effect on pre-lollipop while having the ripple effect on lollipop without drawback. Here is my final solution combining several answers (border/raised are not well rendered on gifs due to grayscale color depth) :
Lollipop
Pre-lollipop
build.gradle
compile 'com.android.support:cardview-v7:23.1.1'
layout.xml
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card"
card_view:cardElevation="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardMaxElevation="8dp"
android:layout_margin="6dp"
>
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:background="#drawable/btn_bg"
android:text="My button"/>
</android.support.v7.widget.CardView>
drawable-v21/btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:drawable="?attr/colorPrimary"/>
</ripple>
drawable/btn_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimaryDark" android:state_pressed="true"/>
<item android:drawable="#color/colorPrimaryDark" android:state_focused="true"/>
<item android:drawable="#color/colorPrimary"/>
</selector>
Activity's onCreate
final CardView cardView = (CardView) findViewById(R.id.card);
final Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new View.OnTouchListener() {
ObjectAnimator o1 = ObjectAnimator.ofFloat(cardView, "cardElevation", 2, 8)
.setDuration
(80);
ObjectAnimator o2 = ObjectAnimator.ofFloat(cardView, "cardElevation", 8, 2)
.setDuration
(80);
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
o1.start();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
o2.start();
break;
}
return false;
}
});
1) You can create rounded corner button by defining xml drawable and you can increase or decrease radius to increase or decrease roundness of button corner.
Set this xml drawable as background of button.
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="6dp"
android:insetRight="4dp"
android:insetBottom="6dp">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="#0091ea">
<corners android:radius="10dp" />
<solid android:color="#1a237e" />
<padding android:bottom="6dp" />
</shape>
</item>
</ripple>
</inset>
2) To change default shadow and shadow transition animation between button states, you need to define selector and apply it to button using android:stateListAnimator property. For complete button customization reference : http://www.zoftino.com/android-button
I've just created an android library, that allows you to easily modify the button color and the ripple color
https://github.com/xgc1986/RippleButton
<com.xgc1986.ripplebutton.widget.RippleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn"
android:text="Android button modified in layout"
android:textColor="#android:color/white"
app:buttonColor="#android:color/black"
app:rippleColor="#android:color/white"/>
You don't need to create an style for every button you want wit a different color, allowing you to customize the colors randomly
// here is the custom button style
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="45"
android:centerColor="#color/colorPrimary"
android:startColor="#color/colorPrimaryDark"
android:endColor="#color/colorAccent"
>
</gradient>
<corners
android:topLeftRadius="10dp"
android:topRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
>
</corners>
<stroke
android:width="2dp"
android:color="#color/colorWhite"
>
</stroke>
</shape>
</item>
</selector>
you can give aviation to the view by adding z axis to it and can have default shadow to it. this feature was provided in L preview and will be available after it release. For now you can simply add a image the gives this look for button background

Categories

Resources