Onclick Listener Not Working Getting No Errors
Solution 1:
Do this instead
....
package com.example.testcode;
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;
// Test code: import android.widget.TextView;// Let your class implement the OnClickListener interface directly. This// will let you use the onClickListenerclassMainActivityextendsActivityimplementsOnClickListener{
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// "find" our views by their id's in our activity's layoutButtonbutton1= (Button) findViewById(R.id.button1);
Buttonbutton2= (Button) findViewById(R.id.button2);
//...... continue for button3 - button8// set our "click" listeners for each of our buttons
button1.setOnClickListener(this);
button2.setOnClickListener(this);
//...... continue for button3 - button 8
}
// Because our class implements the OnClickListener interface// it will be listening for "clicks". Because of this, we can// override the click listener's default onClick(View v) method.// View v is our view, or our button, that is "clicked".@OverridepublicvoidonClick(View v) {
// Test code: TextView text = (TextView) findViewById(R.id.text);// This is the statement that will allow each of your buttons// to perform different processes. For my test code, I have each// button reset the TextView I have displayed in the top of my// layout.switch(v.getId()){
case R.id.button1:
// Test code: text.setText("Button 1");break;
case R.id.button2:
// Test code: text.setText("Button 2");break;
//...... continue for button3 - button 8default:
Log.d(getApplication().getPackageName(), "Button click error!");
break;
}
}
}
Please take a look at my comments. The portion that is commented as
// Test Code:
is code that is pretty much useless for what you are doing. The Test Code from the switch statement is what you need to replace with what you want to do with each individual button. It is also important to note that where I say "//...... continue for button3 - button8" I simply mean to repeat the pattern that I have started with the first two buttons with the remaining six.
If you would like to test it with my given test code, here is my layout (just make sure you erase "// Test Code: " so that the test code lines are no longer commented out:
<LinearLayoutxmlns: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"android:orientation="vertical"tools:context=".MainActivity" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Press a button"android:id="@+id/text" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1"android:id="@+id/button1" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2"android:id="@+id/button2" /></LinearLayout>
NOTES:
It is important to understand that in "good" xml layouts, the "android:text=" portion will be set to 'android:text=@string/"string_id"' instead of some general string to help with localization.
Make sure you change the very top line that declares the package to whatever package you have the Activity placed in. The name of this package is found at "Application"/src/"package_name" and is usually something like com.example.applicationname
I also want to say that I have included the comments in the code so that it can be copied and pasted without having to revisit this link over and over again in case anyone wants to use the code as a guide, not because I wanted tons of code posted in the answer.
Solution 2:
Sometime onClick listener doesn't work because of some other transparent view is on top of your view.
Post a Comment for "Onclick Listener Not Working Getting No Errors"