1. Check out our companion app, Forums for Android! Download from Google Play

My buttons won't work...

Discussion in 'Application Development' started by DragonRider71, Oct 6, 2012.

  1. DragonRider71

    DragonRider71 New Member
    Thread Starter
    5

    Oct 6, 2012
    1
    0
    5
    I have been trying for the last 3-4 days to get my buttons to work and I can't figure out what my problem is. I feel like it is so simple, yet I just don't see it.

    Current task:
    Getting the buttons to display a Toast message so that I can ensure the buttons are working.

    Next task:
    Create an increase and decrease counter......first things first though

    I appreciate the help.

    Code (Text):
    1.  
    2.  
    3. package com.sports.discgolfou;
    4.  
    5. import android.os.Bundle;
    6. import android.app.Activity;
    7. import android.view.View;
    8. import android.view.View.OnClickListener;
    9. import android.widget.Button;
    10. import android.widget.TextView;
    11. import android.widget.Toast;
    12.  
    13. public class hole_1 extends Activity implements View.OnClickListener{
    14.  
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState){
    17.         super.onCreate(savedInstanceState);
    18.         setContentView(R.layout.activity_page2);
    19.        
    20.         //TextView SC = (TextView) findViewById(R.id.labelShotCounter);
    21.         Button ButtonAdd = (Button) findViewById(R.id.buttonAdd);
    22.         ButtonAdd.setOnClickListener(this);
    23.         Button ButtonMinus = (Button) findViewById(R.id.buttonMinus);
    24.         ButtonMinus.setOnClickListener(this);
    25.        
    26.     }//End onCreate
    27.  
    28.  
    29.     @Override
    30.     public void onClick(View v) {
    31.         // TODO Auto-generated method stub
    32.         if(v.getId() == R.id.buttonAdd)
    33.         {
    34.             Toast.makeText(this, "Hello World", Toast.LENGTH_LONG).show();
    35.         }
    36.         else if(v.getId() == R.id.buttonMinus)
    37.         {
    38.             Toast.makeText(this, "GoodBye World", Toast.LENGTH_LONG).show();
    39.         }
    40.            
    41.        
    42.     }//End onClick
    43.    
    44.    
    45.    
    46. }//End Activity
    47.  
    48.  
     

    Advertisement

  2. olijf

    olijf Member
    15

    Sep 23, 2012
    19
    0
    15
    Male
    school
    Rotterdam, Holland
    did you set the button property's right?
    onClick : onClick
    and clickable checked.

    if that isnt it, post your layout.xml.
    we can't really see what's going on now.

    EDIT: make sure you named everything EXACTLY right. uppercase lowercase etc.
     
  3. jonbonazza

    jonbonazza Well-Known Member
    163

    Jul 13, 2010
    1,934
    458
    163
    Male
    Your code is correct and should work. The only thing I can guess that might be the problem is that your ids that you are referencing are not the right ids. Can you post your layout xml?
     

Share This Page

Loading...