-1

I have the button "saveStats" that is seems to not accept clicking.

On the emulator there is no animation of clicking.

I have tried to do clean and restart Eclipse, didnt work.

I also have CheckBox widget that works fine, only the button dont work. here is my code:

Thank for helping :)

Button saveStats;

CheckBox ageCheck, heightCheck, weightCheck, fatCheck;


protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_personal_data);

        saveStats = (Button) findViewById(R.id.saveStats);

        ageCheck = (CheckBox) findViewById(R.id.checkBoxAge);
        heightCheck = (CheckBox) findViewById(R.id.checkBoxHeight);
        weightCheck = (CheckBox) findViewById(R.id.checkBoxWeight);     
        fatCheck = (CheckBox) findViewById(R.id.checkBoxFat);

        saveStats.setOnClickListener(this);
        ageCheck.setOnClickListener(this);
        heightCheck.setOnClickListener(this);
        weightCheck.setOnClickListener(this);
        fatCheck.setOnClickListener(this);

}

public void onClick(View v) {
    // TODO Auto-generated method stub


    switch(v.getId()){

    case R.id.saveStats:
    {
         Toast.makeText(this, "working", Toast.LENGTH_LONG).show();
          return;
        }
    case R.id.checkBoxAge:
    {
        if(ageCheck.isChecked())
            Toast.makeText(this, "ok", Toast.LENGTH_LONG).show()
            return;
    }
////
////
////
////....

Button XML code:

 <Button
        android:id="@+id/saveStats"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="save" />
tomer
  • 389
  • 2
  • 6
  • 16

2 Answers2

1

You have to implement OnClickListener() doing it this way

public class ActivityName extends Activity implements OnClickListener
{

then override your onClick()

@Override
public void onClick(View v)
{
    ...
}

Just a note. Another way to do this would be to declare the same click function in your xml for each Button then write that function in your java code. This prevents you from needing to implements OnClickListener and declaring your Buttons and setting onClickListener() inside your code. Which way you do it depends on what you like best but just another option. Here's a little example:

in xml for each Button declare a function

<Button
   android:id="@+id/btn1"
    ...
    android:onClick="functionName"/>
<Button
   android:id="@+id/btn2"
    ...
    android:onClick="functionName"/>

then in your java code:

public void functionName(View v)   
{
    ... switch on your id just like you would the other way
}

The method just needs to be public, accept a View as its only param, and be the same name as what you declared in your xml

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I did override and its not working. And why the button dont work and CheckBox do work? – tomer May 14 '13 at 15:34
  • You need to change your `return` to `break`. Return is used to exit a method and return some value which is `void` in `onClick()`. `break` is used to exit the `switch`. Even if this did work, if you decided to add behavior in the method no matter which `View` was clicked, the code would never run – codeMagic May 14 '13 at 15:36
  • return finishes the methos so the switch will end too. I dont get the code before the return. – tomer May 14 '13 at 15:39
  • Please just change it to see we are correct. It will take you 3 seconds – codeMagic May 14 '13 at 15:44
  • Its not correct. I fixed it by changing the position of the button in the linear layout, its some how worked. – tomer May 14 '13 at 16:41
1

You have not use break statement. Make sure your class implements OnClickListener

@Override
public void onClick(View v) {

    switch(v.getId())
    {
    case R.id.saveStats :
        Toast.makeText(ActivityName.this, "Button clicked", Toast.LENGTH_LONG).show();
        break;
    case R.id.checkBoxAge :
                         //dosomething
        break;
    }

}

For displaying toast use activity context. To know why check the answer by commonsware in the below link

When to call activity context OR application context?

Example

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_alignRight="@+id/textView1"
    android:layout_marginBottom="66dp"
    android:layout_marginRight="35dp"
    android:text="Button1" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/button1"
    android:text="Button2" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="18dp"
    android:text="CheckBox" />
 </RelativeLayout>

MainActivity.java

public class MainActivity extends Activity implements OnClickListener{

CheckBox cb ; 
Button b,b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b= (Button) findViewById(R.id.button1);
    b1= (Button) findViewById(R.id.button2);
    cb = (CheckBox) findViewById(R.id.checkBox1);
    cb.setOnClickListener(this);
    b.setOnClickListener(this);
    b1.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
     case R.id.button1 :
            Toast.makeText(MainActivity.this, "Button clicked1", Toast.LENGTH_LONG).show();
            break;
     case R.id.button2 :
          Toast.makeText(MainActivity.this, "Button clicked2", Toast.LENGTH_LONG).show();
            break;
     case R.id.checkBox1:
         if(cb.isChecked())
          Toast.makeText(MainActivity.this, "check box clicked", Toast.LENGTH_LONG).show();
            break;

    }       
}
}
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256