How to show effect on like button like facebook shows - android

I have a layout in which I have like button which is a simple textview and I have applied onclick event on this. Now I want to show the same effect which facebook shows when like button in facebook android app get clicked on the news feeds.
How can I do this in my android application. Please help me if you have any idea here.

Use selector if you want to give a highlight effect only when the button in touched.
Save this as an xml drawable and assign this drawable to the button :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_white_09_rectangle_unpressed" android:state_focused="true"
android:state_pressed="false" />
<item android:drawable="#drawable/btn_white_09_rectangle_pressed" android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_white_09_rectangle_pressed" android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/btn_white_09_rectangle_unpressed" />
</selector>
Create 2 other drawable xml files which is being used by the above xml.
btn_white_09_rectangle_unpressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item >
<shape
android:shape="rectangle"
>
<corners
android:radius="9dp"/>
<gradient
android:angle="45"
android:endColor="#27FFFFFF"
android:startColor="#18FFFFFF"
/>
</shape>
</item>
</layer-list>
btn_white_09_rectangle_pressed.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item >
<shape
android:shape="rectangle"
>
<corners
android:radius="11dp"/>
<gradient
android:angle="90"
android:endColor="#09FFFFFF"
android:startColor="#02FFFFFF"
/>
</shape>
</item>
</layer-list>
If you want to change the background of the button(and not revert) when user clicks on it then you have to do it through code.
Use the following code in your onClick()
your_button.setBackgroundResource(R.id.desired_drawable);

Related

Add custom color to touch feedback in android

To give users visual feedback when they touch a view, I do
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground" />
But ?attr/selectableItemBackground is a gray color. I want to use a different color. To do that I do
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="schemas.android.com/apk/res/android">;
<item android:drawable="#color/mine" android:state_selected="true"></item>
<item android:drawable="#color/mine" android:state_pressed="true"></item>
<item android:drawable="#android:color/transparent"></item>
</selector>
but it does not work, even after I set clickable="true" for the view in question.
Follow the code
button_normal.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_light_green"/>
<corners android:radius="5dp" />
</shape>
button_selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/button_light_green_selected"/>
<corners android:radius="5dp" />
</shape>
button_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="#drawable/button_selected"/>
<item android:state_focused="true" android:drawable="#drawable/button_selected"/>
<item android:drawable="#drawable/button_normal"/>
</selector>
assign button_background.xml as background for the button by changing the colors which u desire. Hope it works!!!

ButtonView with different background

I have designed a rounded corners button.
button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dp"/>
</shape>
In layout:
<Button
android:background="#drawable/button_selector"
.../>
on every click the color of the should button changes :
In activity, inside onClickListener():
btn1.setBackgroundColor(getAnswerColor(choiceArray[0]));
but if I do this, I am not getting the rounder corners.
How to get rounder corners? Please suggest.
You can make your button from this toolsite ==> Android Button Maker
You can make any kind of button with this tool with Code.
Here i edit this Answer as your requirement.
so,make two different button for with different background color and use selector for changing background onClick.For that use this code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/click_button" android:state_pressed="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/not_click_button" android:state_pressed="false" />
</selector>
try this way
i checked with below xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="13dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
on button click add this code
StateListDrawable drawable = (StateListDrawable) v.getBackground();
drawable.setColorFilter(Color.RED,PorterDuff.Mode.SRC_ATOP);
button1.setBackground(drawable);
btn1.setBackgroundResource(R.drawable.button_selector);
GradientDrawable drawableGradient = (GradientDrawable) btn1.getBackground();
drawableGradient .setColor(Color.RED);

Setting a Button with background Image and Round corners

I know there are already questions like this but i cannot find the answer for my uestion. Please Help?
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button"
android:state_pressed="true" />
<item android:drawable="#drawable/button"
android:state_focused="true" />
<item android:drawable="#drawable/button" />
</selector>
i already set the background but i dont know how to make the edges round.
Create your own custom button like this
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="3dp" />
<stroke android:width="5px" android:color="#1a1a1a" />
</shape>
Also check this on Developers Site

Custom tabs are not responding to pressed state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="#drawable/tab_profile_pressed_mdpi" />
<item android:drawable="#drawable/tab_profile_unselected_mdpi" />
</selector>
And how I set them:
((ImageView)tabHost.getTabWidget().getChildTabViewAt(i).findViewById(R.id.single_tab_img)).setImageResource(unselected_img[i]);
You need to use setBackgroundResource not setImageResource. State drawables work on the background image, not the foreground image.
I believe you need another xml file that lays out what will happen when the tab is pressed.
Example:
tab.xml
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#drawable/tab_bg_selected" />
tab_bg_selected
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#A8A8A8" android:centerColor="#7F7F7F"
android:endColor="#696969" android:angle="-90" />
</shape>

Selector color on LinearLayout

I'm trying to assing a color selector to an extended class of LinearLayout, so, i think its like if we speak about linearLayout.
i followed the instructions on this post, the answer talking about shapes.
Now i have 3 xml on drawables folders:
normal.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#ffffffff" />
</shape>
pressed.xml file
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#00000000" />
</shape>
and finally, bg.xml file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/pressed" />
<item android:state_focused="true" android:drawable="#drawable/pressed" />
<item android:state_selected="true" android:drawable="#drawable/pressed" />
<item android:drawable="#drawable/normal" />
</selector>
I am accessing this in the following way:
Drawable d = getResources().getDrawable(context.getResources().getIdentifier("mypackageuri.tProject:drawable/bg", null, null));
view.setBackgroundDrawable(d);
The "normal" state its fine, with the color set at "normal.xml", but no way with the other ones, I press my view and nothing happens, it's not changing color in any way...
I can't see what i'm doing wrong...
Thank you
Your view needs to be clickable in order to get the state pressed when you click on it.
Use :
view.setClickable(true);
or in the layout xml :
android:clickable="true"

Categories

Resources