Custom Buttons onClick() not working

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

Custom Buttons onClick() not working

Postby mdownie » Wed Dec 10, 2008 7:15 pm

I am attempting to make set of buttons that all share the same onClick() method. Here is my PokerButton class:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.pokereuphoria.odds;
  2.  
  3.  
  4.  
  5. import android.content.Context;
  6.  
  7. import android.util.AttributeSet;
  8.  
  9. import android.widget.Button;
  10.  
  11. import android.view.View;
  12.  
  13.  
  14.  
  15. public class PokerButton extends Button implements View.OnClickListener {
  16.  
  17.         public PokerButton(Context context, AttributeSet attrs, int defStyle){
  18.  
  19.                 super(context, attrs, defStyle);
  20.  
  21.         }
  22.  
  23.         public PokerButton(Context context, AttributeSet attrs){
  24.  
  25.                 super(context, attrs);
  26.  
  27.         }
  28.  
  29.         public PokerButton(Context context){
  30.  
  31.                 super(context);
  32.  
  33.         }
  34.  
  35.         public void onClick(View v){
  36.  
  37.                 this.setBackgroundDrawable(null);
  38.  
  39.         }
  40.  
  41. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

Here is my main activity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.pokereuphoria.odds;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.os.Bundle;
  8.  
  9. import android.view.*;
  10.  
  11. import android.widget.TextView;
  12.  
  13.  
  14.  
  15. public class PokerOdds extends Activity {
  16.  
  17.     private static final int HOLDEM_ID = Menu.FIRST;
  18.  
  19.     private static final int OMAHA_ID = Menu.FIRST + 1;
  20.  
  21.     private static final int O8_ID = Menu.FIRST + 2;
  22.  
  23.     private static final int STUD_ID = Menu.FIRST + 3;
  24.  
  25.     private static final int STUD8_ID = Menu.FIRST + 4;
  26.  
  27.     /** Called when the activity is first created. */
  28.  
  29.     @Override
  30.  
  31.     public void onCreate(Bundle savedInstanceState) {
  32.  
  33.         super.onCreate(savedInstanceState);
  34.  
  35.         setContentView(R.layout.main);
  36.  
  37.     }
  38.  
  39. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4

For some reason, when I click on the PokerButton object, it does nothing. Of course, I have it setBackgroundDrawable(null) just for testing purposes. I originally tried it as v.setBackgroundDrawable(null) and that had no effect either. The main reason I am trying to do it like this is because I do not want to set separate onClickListeners for all 72+ buttons. Any help will be greatly appreciated.
mdownie
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 6:54 pm
Location: Las Vegas

Top

Postby MrSnowflake » Wed Dec 10, 2008 7:53 pm

You might try actually registering the onClickListener! Put in all the constructors:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setOnClickListener(this);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
It could also be addOnClickListener(this), not sure.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby mdownie » Wed Dec 10, 2008 7:57 pm

Thank you very much MrSnowFlake. That works perfectly. For future reference it was the setOnClickListener(this).
mdownie
Junior Developer
Junior Developer
 
Posts: 20
Joined: Tue Nov 25, 2008 6:54 pm
Location: Las Vegas

Postby MrSnowflake » Wed Dec 10, 2008 8:04 pm

Sometimes it's set, other times it's add.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 20 guests