OnClick method not called on Android

I am making the simplest of onClick models and cannot run the onClick method. I know this 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 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> 
+8
android onclick
source share
6 answers

After severe anguish and gnashing of teeth, I realized this. I had to remove the device emulator and add a new one, and now it works like a champion. I appreciate everyone who is trying to help.

+7
source share

Instead of installing onClicklistener, try this approach:

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

You should try to Clear the project or try to restart Eclipse or any other editor that you use, because it is valid code and should work fine.

UPDATE:

In addition, you should check your Logcat, you get the output of Log.d("TEST", "TEST"); , because your Toast seems to be implemented incorrectly.

 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 that you are referencing the listener, which really shouldn't be. You must refer to the Activity itself, so it is better to use Activity_name.this .

+4
source share

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

Try it. Whenever you implement onclick in your activity, you need to install as described above to make it work for all controls, and onclick should look something like this:

 @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; } } } 
0
source share

Check out the three steps:

  • correctly find the button by identifier
  • associate the button with the listener. (add actionListener)
  • you specify the condition for this button when implementing the actionListener class.
-one
source share

try to press the button

 <?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" /> 

-2
source share

All Articles