5

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:(

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
user1288621
  • 99
  • 1
  • 6

3 Answers3

4

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

Emran
  • 1,031
  • 11
  • 25
  • Eh, you meant button.setBackgroundResource(R.drawable.button); – AJcodez Mar 25 '12 at 00:13
  • Yes i did do this but when i run the app on the emulator the button are huge sizes! – user1288621 Mar 26 '12 at 16:58
  • Yes it the images anyone know what dimensions i should use? – user1288621 Mar 26 '12 at 17:00
  • You should use 9-patch images for buttons. Take a look at this tutorial: http://developer.android.com/guide/developing/tools/draw9patch.html – Emran Mar 26 '12 at 18:07
  • it has to be there. If you can't find then try to download SDK again from Android web site. – Emran Mar 26 '12 at 19:01
  • draw9patch.bat (if you are using Windows) is located inside Android SDK tools folder. Try to locate Android SDK folder. If you don't remember where that folder is you can find path in Eclipse by going to Window->Preferences->Android and SDK Location field should be path to your SDK folder. – Emran Mar 26 '12 at 19:37
  • Thank you alot Emran, by the way are you arab? I am :) – user1288621 Mar 26 '12 at 19:48
3
    alphaDown = new AlphaAnimation(1.0f, 0.3f);
    alphaUp = new AlphaAnimation(0.3f, 1.0f);
    alphaDown.setDuration(1000);
    alphaUp.setDuration(500);
    alphaDown.setFillAfter(true);
    alphaUp.setFillAfter(true);
    analyse.startAnimation(alphaUp);

try this code on your button click

Aashish Bhatnagar
  • 2,595
  • 2
  • 22
  • 37
  • Where do i put it where in button click – user1288621 Mar 24 '12 at 22:26
  • Sorry about this but im a noob could someone show a easier way? – user1288621 Mar 24 '12 at 22:32
  • just put these lines under onClick event of the button nothing else – Aashish Bhatnagar Mar 25 '12 at 06:03
  • `public void onClick(View arg) { AlphaAnimation alphaDown = new AlphaAnimation(1.0f, 0.3f); AlphaAnimation alphaUp = new AlphaAnimation(0.3f, 1.0f); alphaDown.setDuration(1000); alphaUp.setDuration(500); alphaDown.setFillAfter(true); alphaUp.setFillAfter(true); arg.startAnimation(alphaUp); }` this is what he means – Mr Heelis Jul 18 '19 at 12:40
0

Use android selectors. Create new drawable with its own states and set it as button's background drawable

pleerock
  • 18,322
  • 16
  • 103
  • 128