Home » Android » onClick method not called on Android

onClick method not called on Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am doing the simplest of onClick models and cannot get the onClick method to fire. I know it is something simple, and I am new to Android. Any help is appreciated.

package com.bordeloniphone.timeentry;

import android.app.Activity;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class TimeEntryActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */

    Button okButton;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        okButton = (Button) findViewById(R.id.btnOK);
        okButton.setText(":)");
        okButton.setOnClickListener(this);
        //setContentView(okButton);

    }

    public void onClick(View v) {
        Log.d("TEST", "TEST");
        Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();

    }

}

Here is the main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnOK"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:text="OK" />

</LinearLayout>
How to&Answers:

After much angst and gnashing of teeth, I figured it out. I had to delete the emulator device and add an new one and now it works like a champ. I appreciate everyone trying to help.

Answer:

Instead of setting the onClicklistener to this, try this approach:

okButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Log.d("TEST", "TEST");
            Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
        }
    });

Answer:

You should try Cleaning your project or try to restart your Eclipse or any other Editor you are using as it is a valid code and should work fine.

UPDATE:

Also, you should check your Logcat, are you getting the output of Log.d("TEST", "TEST"); because your Toast seems to be implemented in a wrong manner.

Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show(); // wrong
Toast.makeText(Activity_name.this, "TEST", Toast.LENGTH_SHORT).show(); // correct

Using this in Toast inside the Listener means you are Referencing the Listener, which indeed should not be the case. You have to reference to the Activity itself so better use Activity_name.this.

Answer:

Button iv_StyleInspiration_Back = (Button) findViewById(R.id.iv_StyleInspiration_Back);
iv_StyleInspiration_Back.setOnClickListener(this);

Try this Whenever you implement onclick in your activity you need to set as above to make it work for all controls and onclick should look something as below

@Override
    public void onClick(View pView) {
        if (null != pView) {
            switch (pView.getId()) {
                case  R.id.iv_StyleInspiration_Back:
                    //do what you want
                                    break;
                default:
                    break;
            }
        }
    }

Answer:

Check for Three Steps:

  1. find the button by id correctly
  2. link the button to a listener. (adding the actionListener)
  3. you specify a condition for this button in case of implementing an actionListener class.

Answer:

try making the button clickable

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/btnOK"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:text="OK"
    android:clickable="true" />