on button released

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

on button released

Postby Jenselense » Mon Mar 09, 2009 12:25 pm

Hi,
I have a custom button with a bitmap as background.
I want this background to be changed, when the button is pressed, and again when it is released...
Changing it "onPress" (or OnClick) ist no Problem, but is there a way to notice, when the button is released???

Or is there a possibility to set something like a "onTouch Background" which handles this issue itself??? I couldnt find anything...?????shouldn't be tooo difficult...?

Thanks in advance
JJ


Edit: OK, now I am using an ImageButto. This button changes its appereance on default, when its clicked... but only its background... So now I have my image on the button with a small white frame which changes when its clicked. Which is better than nothing, but still...actually i want the hole button to change, when its clicked...???
Jenselense
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 06, 2009 3:41 pm

Top

Postby powerdeng » Tue Mar 10, 2009 7:54 pm

try OnTouchListener
here is an example.
more detail at docs :)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. yourButton.setOnTouchListener(new OnTouchListener() {
  2.  
  3.             @Override
  4.  
  5.             public boolean onTouch(View arg0, MotionEvent arg1) {
  6.  
  7.                
  8.  
  9.                 if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
  10.  
  11.                     /*change the background to a pressed one*/
  12.  
  13.                     arg0.setBackgroundResource(R.drawable.pressed);
  14.  
  15.                 } else if (arg1.getAction() == MotionEvent.ACTION_UP) {
  16.  
  17.                     /*change the background to a released one*/
  18.  
  19.                     arg0.setBackgroundResource(R.drawable.released);
  20.  
  21.                 }
  22.  
  23.                 return false;
  24.  
  25.             }
  26.  
  27.            
  28.  
  29. });
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
:) e2ecloud.com
powerdeng
Freshman
Freshman
 
Posts: 9
Joined: Mon Feb 16, 2009 10:20 am
Location: Chengdu,China

Postby MrSnowflake » Tue Mar 10, 2009 8:28 pm

How about my HoldButton? It's a regular button, but also has 2 listeners for when the button is held down and released.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Jenselense » Wed Mar 11, 2009 9:22 pm

Nice...
thanks for your replies, I already used the first solution.
Next time I'll check your holdbutton :-)

thanks,
JJ
Jenselense
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 06, 2009 3:41 pm

Postby MrSnowflake » Wed Mar 11, 2009 10:13 pm

Oh, I didn't read you post, I just saw you wanted to do something when the button was pressed. For making a button with your own bitmap you should use a selector.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Jenselense » Thu Mar 12, 2009 12:10 am

I used a "normal" button with an image as background and switched this backgroundimage in the buttons onTouchListener if the action was DOWN and again back to the original when the action was UP...works fine...

I just need to return false, because I want to the onClickListener to handle the rest...
Jenselense
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 06, 2009 3:41 pm

Top

Postby MrSnowflake » Thu Mar 12, 2009 8:25 pm

Using the selector drawable in the button does all this for you, so you don't have to write code for this.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby powerdeng » Sat Mar 21, 2009 6:44 pm

MrSnowflake wrote:Using the selector drawable in the button does all this for you, so you don't have to write code for this.


Thx~MrSnowflake
got a new trick~ :)
:) e2ecloud.com
powerdeng
Freshman
Freshman
 
Posts: 9
Joined: Mon Feb 16, 2009 10:20 am
Location: Chengdu,China

Postby pskink » Sat Mar 21, 2009 8:53 pm

MrSnowflake wrote:How about my HoldButton? It's a regular button, but also has 2 listeners for when the button is held down and released.


How about SmoothButton which is included among my misc-widgets http://code.google.com/p/android-misc-widgets ? it smoothly changes (morphs) while pressing/releasing
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 17 guests