I am new to Android programming. I want to add ripple effect to cardview. Actually I found the solution, however, after touching cardview element, it seems that the effect doesn't cover entire cardview. I mean that the effect fades out suddenly and doesn't reach the edges of the cardview.
The result which I'm getting is as follows:
While I want something like this one posted in this blog post.
This is the approach I have tried:
cardview_item.xml
<android.support.v7.widget.CardView
android:layout_width="match_parent"
style="#style/MyCardViewStyle"
android:layout_height="280dp"
android:id="#+id/appletCard"
app:cardBackgroundColor="#color/cardDefaultBg"
android:clickable="true"
android:foreground="#drawable/ripple_effect"
>
//Some TextViews here ...
</android.support.v7.widget.CardView>
And this is the ripple_effect.xml in the drawable-v21 package:
<ripple
xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white"/>
</ripple>
If it is important, I use support library.
You should apply mask to your ripple. Try this:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?android:attr/colorControlHighlight">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#000000" />
<corners android:radius="#dimen/card_corner_radius" />
</shape>
</item>
</ripple>
You can use android default ripple effect with "?attr/selectableItemBackground"
<android.support.v7.widget.CardView
android:id="#+id/appletCard"
android:layout_width="match_parent"
android:layout_height="280dp"
style="#style/MyCardViewStyle"
android:clickable="true"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="#color/cardDefaultBg">
</android.support.v7.widget.CardView>
Or create custom
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape
android:shape="rectangle">
<solid android:color="#color/RippleColor" />
</shape>
</item>
<item android:state_pressed="true">
<shape
android:shape="rectangle">
<solid android:color="#color/RippleColor" />
</shape>
</item>
<item>
<shape
android:shape="rectangle">
<solid android:color="#color/backColor" />
</shape>
</item>
</selector>
If you want that effect reach the edges, use this library that provides you OnRippleCompleteListener, so you can wait until it finish effect and reach the edges
After checking the similar applications which are using ripple effect for their ui elements, I found out that the effect has changed over different versions of Android. The case I'm looking for is used in Lollipop Android devices. So as I ran the emulator on API 25 (Nogut), I didn't get the desired result.
Thank you all for ur responses.
Related
I have a custom button, and I need to create a Ripple effect. But after creating I do not know how I could combine the two effects.
I would like to have my original style and when I click add the ripple effect.
Original code before ripple:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EEEEEE"></solid>
<corners android:radius="5dp"></corners>
<stroke android:width="1dp" android:color="#99FFFFFF"></stroke>
</shape>
Code ripple:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f816a463"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" />
<corners android:radius="5dp" android:color="#f816a463"></corners>
<stroke android:width="1dp" android:color="#f816a463"></stroke>
</shape>
</item>
<item
android:id="#android:id/background"
android:drawable="#android:color/holo_blue_bright">
</item>
</ripple>
Code Button:
<Button
android:id="#+id/btns2teste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/btnTestes"
android:layout_below="#+id/btnTestes"
android:layout_marginTop="49dp"
android:background="#drawable/ripple_effect"
android:text="Botao2" />
Image Button before:
When I created the new style and called in the xml of the button, I can no longer call the effect I created earlier, and it gets a simple button with ripple colors.
I would like to apply the ripple effect but keep the button state in the way I'd like it to be already custom with the border.
I need to find a way to combine the styles.
I'm making a bottom navigation bar, of custom shape
Like this =>
So I tried using LinearLayout with a FAB on it, as I dont know much about custom shapes,
but my idea not working as, when I scroll my webView bottombar hides, but fab doesn't. Anyways this is a bad method.
So, What I want is to make a custom shaped bottombar,
please help me make it, or just simple guiding would be helpful,
Thank you so much :)
Create a drawable with the below fields
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="4dp"
android:left="20dp"
android:right="20dp"
android:top="15dp">
<shape android:shape="rectangle">
<solid android:color="#ED7D31" /> //Color of youe choice
</shape>
</item>
<item
android:width="50dp"
android:height="50dp"
android:gravity="top|center">
<shape android:shape="oval">
<solid android:color="#ED7D31" />
</shape>
</item>
</layer-list>
and in the XML file add it as a background like below
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:elevation="4dp"
android:background="#drawable/list" /> //Your drawable name here
When using the selectableItemBackground attribute for "touch ripple effects", it seems that normal touches are too fast for the "touch ripple" to appear. I have to touch a bit longer and everything works as it should. Is it possible to force this effect some how, even on short touches?
I should suggest to you that create a custom ripple effect.
Selector for Pre API 21
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
Ripple Effect for API 21+ Devices
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#color/colorAccent">
<item
android:id="#android:id/mask"
android:drawable="#android:color/white" />
</ripple>
For More: Click Here
How to create this type of button in android. I tried . May I know what is the correct way to achieve my objective?Maybe this question too basic, but i did't find any suitable solution.Please Help me out.
I want to create This
<Button
android:id="#+id/Btn1"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/green_circle"
android:textSize="20sp"
android:textColor="#android:color/white"
android:clickable="true"
/>
Here is green_circle.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1sp" android:color="#54d66a" />
</shape>
When i use this ,Button looks
How can i get my desire button. Any Help Appreciated
You defined 1sp (you should use "dp" btw.) circle and you got it.
I think that the easiest way to achieve what you want is using layer list xml element - just put your current drawing as the first layer and put second layer for the centered dot (just solid oval with some margins)
Here you have documentation: http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
Simple usage (it's not your's case but I think that it show you what you should to do):
<layer-list >
<item>
<shape android:shape="rectangle">
<corners android:radius="2dp"/>
<solid android:color="#color/grey"/>
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<corners android:radius="2dp"/>
<solid android:color="#color/green_light"/>
</shape>
</item>
</layer-list>
Key is android:bottom="2dp" that causes that the foreground layer is smaller than the background one.
One way to achieve your desired effect is using a Layer List Drawable. To get a Drawable similar to the image you've posted, try the following
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1sp" android:color="#54d66a" />
</shape>
</item>
<item android:top="5dp" android:left="5dp" android:right="5dp" android:bottom="5dp">
<shape android:shape="oval">
<solid android:color="#54d66a" />
</shape>
</item>
</layer-list>
1.use two different images of radio on and off actually what you want and make a xml in drawable :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="#drawable/check_off" />
<item android:state_checked="true" android:drawable="#drawable/checkbox_on" />
<item android:drawable="#drawable/checkbox_off" />
</selector>
2.and set this drawable as a background on your radio button.
Use an ImageButton:
<ImageButton android:src="#drawable/green_circle" />
The documentation also states how you can have different images depending on the state of the button.
put an image of green circle in drawable folder. then in xml just set background image for your button
<Button
android:id="#+id/Btn1"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/yourimage"
/>
this will work :)
What I'm trying to do is define a common background for use in a LinearLayout which frequents my application on many of its Activitys. This particular layout is a header that shows on the top of each activity.
What I'm trying to do is create a drawable that fills the linearlayout with a gradient and has a horizontal line below the gradient.
Does anyone know if this is possible or do I have to do this sort of thing only with nested layouts.
My attemptat the drawable xml is
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#AA000000" android:endColor="#AA333333"
android:angle="270" />
</shape>
</item>
<item>
<shape android:shape="line">
<stroke android:width="3dp" android:color="#FFFFFFFF"
android:dashWidth="1dp" android:dashGap="2dp" />
<size android:height="5dp" />
</shape>
</item>
</selector>
Drawable accepts multiple shapes, (defined always in others files) if i understand your question maybe you can do a Layer drawable (this draws multiple underlying drawables on top of each other)
i write this here, so, i dont tested, but try this and read this fantastic documentation.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/shape_7"/>
<item android:drawable="#drawable/shape_1"/>
</layer-list>
the android complete xml resources
cheers
Use gravity to set your items in layer-list.
for vertical orientation use:
android:gravity="top"
android:gravity="center"
android:gravity="bottom"
for horizontal orientation use:
android:gravity="left"
android:gravity="center"
android:gravity="right"
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
android:src="#drawable/top_img"
android:gravity="top"/>
</item>
<item>
<bitmap
android:src="#drawable/center_img"
android:gravity="center"/>
</item>
<item>
<bitmap
android:src="#drawable/bottom_img"
android:gravity="bottom"/>
</item>
</layer-list>
Be sure that the parent element has enough space to hold all the items!
Otherwise they will overlap each other