Button not responding to onClick Event in android

import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
	
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	
	private ImageView img1;
	private ImageView img2;
	private ImageView img3;
	private ImageView img4;
	
	private boolean btn1On;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        if(btn1On == false){
        	btn1On= false;
        }
        
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);
        btn4 = (Button) findViewById(R.id.button4);
        
        img1 = (ImageView) findViewById(R.id.imageView1);
        img2 = (ImageView) findViewById(R.id.imageView2);
        img3 = (ImageView) findViewById(R.id.imageView3);
        img4 = (ImageView) findViewById(R.id.imageView4);
        
        btn1On = false;
        
        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(btn1On = false){
					btn1On = true;
					btn1.setText("End");
//					AnimatorSet as = new AnimatorSet();
//					ObjectAnimator oa1 = ObjectAnimator.ofFloat(img1, "translationY", 0f, 100f);
//					ObjectAnimator oa2 = ObjectAnimator.ofFloat(img1, "translationY", 100f, 0f);
//					as.setDuration(1000);
//					as.play(oa1).before(oa2);
//
//					as.start();
					
				} else {
					btn1On = false;
					btn1.setText("Start");
				}


				
			}
		});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

Open in new window

Hi,  I'm mostly just looking for ideas here.  I'm not sure if it's the Android Virtual Device Manager that is causing this issue or if there is something going on in my code that I'm not seeing.  I create a button and set it's onClickListener and the On Click event never seems to get called.

In debug mode I have added meaningless code to the onCreate event to see that it is called.  I have commented out any labor intensive code in the onClickListener but still it never gets called.  I'm not sure what I am doing wrong.

Here is my activity_main.xml

My MainActivity.java

I think those are the only relevant files.  Any help or suggestions would be great

Thanks in advance!
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/beachball" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/beachball" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip" >

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/beachball" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/beachball" />
    </TableRow>

</TableLayout>

Open in new window

kpbaremAsked:
Who is Participating?
 
Tamil ArasiConnect With a Mentor Android DeveloperCommented:
Hi try this,
This code is working to me when i am change the code instead of if(btn1On = false ) into if(btn1On == false )

I hope this will be help to you...
0
 
girionisCommented:
Your code is correct. What happens if you put a System.out line on your button 1 listener?

btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				System.out.println("Button one is pressed");
				
			}
		});

Open in new window


If the above does not display anything try to add an android:onClick event on the button itself as explained here.
0
 
kpbaremAuthor Commented:
I discovered this issue last night and felt so ridiculous about it.  Thank you for your help, though!
0
 
Tamil ArasiAndroid DeveloperCommented:
Welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.