Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To make a small flash when a button is tapped?

So I'm making an android app that has more than 100 buttons,but you know when you tap a button normally when you don't changed the background or anything it flashes orangish color. However since I've added a background color to my buttons when they're tapped it just goes to the next screen and you can't tell that you've tapped a Button!

Could someone help me please? Sorry if I don't know what those are called:(

like image 906
user1288621 Avatar asked Mar 24 '12 20:03

user1288621


1 Answers

Declare in drawables this selector and name it for example: button.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/btnPressed"/>
    <item  android:drawable="@drawable/btnNormal"></item>

</selector>

android:drawable can be color, image, another drawable... And then you can declare your button as:

<Button 
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:background="@drawable/button"
     />

If you create your buttons in code you can call method: setBackgroundResource() and pass resource id. Example:

Button button = new Button(this);
button.setBackgroundResource(R.drawable.button);

http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

like image 139
Emran Avatar answered Oct 03 '22 19:10

Emran