OnClick listener implementation not working - java

I'm using onClick() method by implementing OnClickListener class. Using below code but it does not work! There are no errors but it doesn't work. When I click on the button nothing happens. What is wrong?
package com.behnam.PhoneCh.main;
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.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id. Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
}

You never set your listener on your Buttons
mainBtn.setOnClickListener(this);
You will need to add this for all of the Buttons that you want to use the onClick().
See this answer for different ways of handling onClick()

#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2.setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id.button1;
Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
Change your onCreate and onClick with the above code.

You need to change your code to :
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2 .setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
// And then you can use
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Dome some stuff on click of button 1
break;
case R.id.button2:
// Dome some stuff on click of button 2
break;
}
}
}

Try this instead:
Button btMain = (Button)findViewById(R.id.button1);
btMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doStuff();
}
});
This is the best way to start understanding how the listeners work.

Related

onClick view is already defined

I am trying to work with two buttons on one screen and I want to have two different actions occur when each button is clicked. This is my code and I am getting the error message onClick view is already defined on the method titles of both onClick methods. Any help is greatly appreciated.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private Button button3;
private Button button2;
private EditText editText;
private EditText editText9;
private EditText editText10;
private EditText editText11;
private EditText editText12;
private EditText editText13;
private EditText editText14;
private TextView textView;
private TextView textView22;
private View view;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent intent = getIntent();
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
editText = (EditText) findViewById(R.id.editText);
editText9 = (EditText) findViewById(R.id.editText9);
editText10 = (EditText) findViewById(R.id.editText10);
editText11 = (EditText) findViewById(R.id.editText11);
editText12 = (EditText) findViewById(R.id.editText12);
editText13 = (EditText) findViewById(R.id.editText13);
editText14 = (EditText) findViewById(R.id.editText14);
textView = (TextView) findViewById(R.id.textView);
textView22 = (TextView) findViewById(R.id.textView22);
}
public void onClick (View view) {
this.view = view;
if (view.getId() == R.id.button3) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
MainActivity2.this.startActivity(intent);
}
}
public void onClick (View view){
if (view.getId() == R.id.button2) {
String value8 = editText.getText().toString();
String value9 = editText9.getText().toString();
String value10 = editText10.getText().toString();
String value11 = editText11.getText().toString();
String value12 = editText12.getText().toString();
String value13 = editText13.getText().toString();
String value14 = editText14.getText().toString();
}
}
}
You have two onClick methods with the same parameters so it doesn't know which to use. Combine the two and it should work.

setOnclickListener(this) ERROR

When I try to call setOnClickListener(this); 'this' gets an error. I have tried to declare it as other things but that didn't work. I am only trying to make multiple onClick events.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.View.OnClickListener;
import android.view.View;
public class MainActivity extends Activity {
ImageButton button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton one = (ImageButton) findViewById(R.id.img1);
one.setOnClickListener(this);<-error
ImageButton two = (ImageButton) findViewById(R.id.img1);
one.setOnClickListener(this);<-error
ImageButton three = (ImageButton) findViewById(R.id.img2);
two.setOnClickListener(this);<-error
}
public OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.img1:
button.setBackgroundResource(R.mipmap.checkmark);
break;
case R.id.img2:
button.setBackgroundResource(R.mipmap.checkmark);
break;
case R.id.img3:
button.setBackgroundResource(R.mipmap.checkmark);
break;
}
}
};
}
Instead of this, reference your already created onClickListener class member. Also you have a bug in your 2nd and 3rd imagebutton where it is not getting any clicklistener or targeting the wrong resource id, here is all the fixes
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton one = (ImageButton) findViewById(R.id.img1);
one.setOnClickListener(onClickListener);
ImageButton two = (ImageButton) findViewById(R.id.img2);
two.setOnClickListener(onClickListener);
ImageButton three = (ImageButton) findViewById(R.id.img3);
three.setOnClickListener(onClickListener);
}
Use this:
ImageButton one = (ImageButton) findViewById(R.id.img1);
one.setOnClickListener(onClickListener );
change your class declaration to :
public class MainActivity extends Activity implements View.OnClickListener {
ImageButton button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton one = (ImageButton) findViewById(R.id.img1);
one.setOnClickListener(this);<-error
ImageButton two = (ImageButton) findViewById(R.id.img1);
one.setOnClickListener(this);<-error
ImageButton three = (ImageButton) findViewById(R.id.img2);
two.setOnClickListener(this);<-error
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.img1:
button.setBackgroundResource(R.mipmap.checkmark);
break;
case R.id.img2:
button.setBackgroundResource(R.mipmap.checkmark);
break;
case R.id.img3:
button.setBackgroundResource(R.mipmap.checkmark);
break;
}
}
}
try this :
public class MainActivity extends Activity implements View.OnClickListener
just implement this on first line otherwise all thing ok.

1 of 2 Button Working in an android app home page

I made a basic homepage for my app.
The page consists of a sign in and sign up button.
Only the signup button works. I'm fairly new to android development and I guess I just can see why this is happening.
I checked other questions on stack overflow and decided to make a private handler and then cases however I still have the same problem.
Here is the code. Any help and insight as to why this is happening is much appreciated.
package net.citrusdynamics.citrus;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
signin.setOnClickListener(onclicklistener);
signup.setOnClickListener(onclicklistener);
}
private View.OnClickListener onclicklistener = new View.OnClickListener() {
#Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.signin:
setContentView(R.layout.activity_signin);
Intent signin_intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(signin_intent);
break;
case R.id.signup:
setContentView(R.layout.activity_signup);
Intent signup_intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signup_intent);
break;
}
}
};
}
Try to add the click listener like in this code below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button signin = (Button) findViewById(R.id.signin);
Button signup = (Button) findViewById(R.id.signup);
signin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent signin_intent = new Intent(MainActivity.this, SigninActivity.class);
startActivity(signin_intent);
}
});
signup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent signup_intent = new Intent(MainActivity.this, SignupActivity.class);
startActivity(signup_intent);
}
});
}

button can not be resolved

This is my starting activity. I'm trying to get this button to work but it's been giving me this error.
Line 15 button can not be resolved.
package com.synamegames.giveaway;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class GiveawayActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button register = (Button) findViewById(R.id.register);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
setContentView(R.layout.main);
}
}
Please try this..
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
}
});
}
The problem is You have defined the Button instances as register in the line final Button register = (Button) findViewById(R.id.register); But you are setting onclick listener to the button instance which is not defined. You should have
register.setOnClickListener(new OnClickListener() {
instead of
button.setOnClickListener(new OnClickListener() {
U can use android:onClick from xml and pass a the view into the .java file
eg:
android:onClick="bactToList"
in java:
public void bactToList(View view){
}

Android: Why isn't my button playing its sound effect?

The first button works fine, but when I click to the second, nothing happens. I'm not getting any errors, I can't see what's wrong.
Sounds.java
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
}
public void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
}
You can't randomly create a method like you're doing with onCreate1, it's never executed by you or by your Activity.
This is all you need:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(this);
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}
You aren't assigning the onclick listener inside the actual OnCreate. This should work:
package com.andrew.finnandjake;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Sounds extends Activity {
private SoundManager mSoundManager;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.finn_whatthejugisthat);
mSoundManager.addSound(2, R.raw.jake_dancingwithbabes);
Button b1 = (Button)findViewById(R.id.Button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
Button b2 = (Button)findViewById(R.id.Button2);
b2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(2);
}
});
}

Categories