A HoldButton for your greasy fingers

Quickly share your Android Code Snippets here...

A HoldButton for your greasy fingers

Postby MrSnowflake » Tue Dec 16, 2008 10:19 pm

Hi Guys,

I've made a HoldButton for android. It allows you to press and hold the button and then release it, this will trigger 2 listeners, instead of only just one.

It has 2 listeners you can register: OnButtonDownListener and OnButtonUpListener. The OnButtonDownListener will get called when the user presses and holds the HoldButton down, so for example you can start a MediaRecorder. And when the users lets loose of the HoldButton OnButtonUpListener will be called, so the just started MediaRecorder will stop.

Usage example:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. protected void onCreate(Bundle savedInstanceState) {
  3.  
  4.         super.onCreate(savedInstanceState);
  5.  
  6.         setContentView(R.layout.holdbutton_example);
  7.  
  8.         holdbutton = (HoldButton)findViewById(R.id.holdbutton);
  9.  
  10.         holdbutton.setOnButtonDownListener(new OnButtonDownListener() {
  11.  
  12.                 public void onDown(HoldButton view) {
  13.  
  14.                         // Start recording here
  15.  
  16.                         mediaRecorder.start();
  17.  
  18.                 }
  19.  
  20.         });
  21.  
  22.         holdbutton.setOnButtonUpListener(new OnButtonUpListener() {
  23.  
  24.                 public void onUp(HoldButton view) {
  25.  
  26.                         // Stop recording
  27.  
  28.                         mRecorder.stop();
  29.  
  30.                 }
  31.  
  32.         });
  33.  
  34. }
  35.  
  36.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <com.DeepFrozen.android.view.HoldButton
  2.  
  3.         android:id="@+id/holdbutton"
  4.  
  5.         android:text="Holdme down"
  6.  
  7.         android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="wrap_content" />
  10.  
  11.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4
Hmm, while writing this post, I saw an improvement I need to make: Have the up text and down text being specified in the xml.

It's released under a BSD style licence, so you can do with it what ever you want as long as you hold to the licence, please read it, it's very short :). Oh and if you want it, would you mind putting my name in the credits please :).
Attachments
HoldButton.java
HoldButton by Maarten 'MrSnowflake' Krijn
(5.9 KiB) Downloaded 143 times
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby linkmaster_6 » Tue Jan 13, 2009 9:35 pm

i have tried your code and it doesn't seem to work. sorry i know that is very vague. what extecly did you od to make the button tell that its being held
linkmaster_6
Experienced Developer
Experienced Developer
 
Posts: 51
Joined: Thu Oct 16, 2008 8:52 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 7 guests