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.
Related
I encountered an issue and couldn't resolve in Android Studio. The setOnClickListener remains red and doesn't work unless I get rid of my "loseStarter1" button name.
Note: Starter1 is a button, I'm trying to make it disappear when clicked by the user. My real code starts when I introduce the loseStarter1 button.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
}
Button loseStarter1;
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
})
}
Much appreciated.
You're missing a semicolon to end the new View.OnClickListener() { ... statement as well as that block not being inside of a method.
Not only move this code into the onCreate method, make sure you end it with a semicolon.
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); // Add the semicolon here
It should look like this:
public class game1 extends AppCompatActivity {
Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
}); //added semicolon
} // ends onCreate method
} // ends class
Your Button variable declaration and OnClickListener initialization is outside of the onCreate() method. Use the following code:
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
}
}
Move below code inside onCreate()
loseStarter1 = (Button) findViewById(R.id.Starter1);
loseStarter1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loseStarter1.setVisibility(View.GONE);
}
});
This is how i do it.
1) first make the class implement the interface View.OnClickListener, this let you handle the button clic event:
public class game1 extends AppCompatActivity implements View.OnClickListener;
2) second create the interface method to handle event.
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
3) OnCreate methods is the best way to find objects and set properties.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1= (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
all code :
package com.cutting_edge_tech.mentalenhancementapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class game1 extends AppCompatActivity implements View.OnClickListener {
private Button loseStarter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game1);
loseStarter1 = (Button) findViewById(R.id.Starter1);
if(loseStarter1!=null){
loseStarter1.setOnClickListener(this);
}
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.Starter1)
{
view.setVisibility(View.GONE);
}
}
}
Advice:
Rename class to Game1.
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);
}
});
}
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.
Two buttons, it mix between the links
I have a page with two buttons, when I had the first button, it took me to the link, when I had the second button, the first stopped and the second took me to the second link, help!
PageOne.jave
package com.d.di;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
public class PageOne extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abus);
setContentView(R.layout.weoff);
}
}
MainActivity.java
package com.d.di;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, PageOne.class);
startActivity(intent);
}
});
}
}
Here
button = (Button) findViewById(R.id.abus);
button = (Button) findViewById(R.id.weoff);
It replaces the old reference to R.id.weoff so you will add the listener to weoff, two times which replaces the old listener and you set it with a listener which points to PageOne.class.
You should use two variables
button1 = (Button) findViewById(R.id.abus);
button2 = (Button) findViewById(R.id.weoff);
and
button1.setOnClickListener(new OnClickListener() {
button2.setOnClickListener(new OnClickListener() {
Since the two listeners point to the same Intent, you could create the listener only one time and put the same?
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){
}