Custom button example not working - android

I am newbie in android application development.
I am using the Form Stuff example given on http://developer.android.com/resources/tutorials/views/hello-formstuff.html. In this i have used the custom button example.
My code is as below:
package com.example.helloformstuff;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class HelloFormStuff extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});
}
}
Its showing following error:
The type new DialogInterface.OnClickListener(){} must implement the inherited
abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int)
- The method setOnClickListener(View.OnClickListener) in the type View is not
applicable for the arguments (new DialogInterface.OnClickListener(){})
I am unable to figure out the reason for such error.
Please help me on this.
Thanks
Pankaj

You're using the wrong OnClickListener. Use this code instead:
button.setOnClickListener(new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Perform action on clicks
Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});

You are implementing the wrong OnClickListener. Try View.OnClickListener:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks
Toast.makeText(HelloFormStuff.this, "Beep Bop", Toast.LENGTH_SHORT).show();
}
});

Replace the import android.content.DialogInterface.OnClickListener with
import android.view.View.OnClickListener
Rest of the code remain unchanged will execute the code perfectly.

Related

Custom Dialog Window/Pop up window keeps rejecting button input?? Android Studio

So I created a pop-up dialog window to serve as the tutorial for the user, I decided to run tests on it since it was working well but saw that when you added an active button to it, it would not work. Each time I pressed the button that brings up the tutorial(which worked before the addition of the button code), the addition I made with the button taking the user to another part now renders that button inactive and instead takes the user back to the start screen when the tutorial button is pressed.
This is the code I used for it:
package com.example.rockpaperscissors;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import static com.example.rockpaperscissors.R.id.tutorialButton;
public class MainMenu extends AppCompatActivity {
Button tutorialButton, playGameButton, testbutton;
Dialog tutorial_popup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tutorial_popup = new Dialog(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getSupportActionBar().hide();
setContentView(R.layout.activity_main_menu);
playGameButton = findViewById(R.id.playGameButton);
tutorialButton = findViewById(R.id.tutorialButton);
testbutton = findViewById(R.id.testbutton);
tutorial_popup = new Dialog(this);
playGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
testbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
});
}
}
As you can see, there's another setOnClickListener within the tutorial which is basically supposed to take the user to another class/activity when pressed on. However, it does not do it and instead just ignores it, showing that there is an error.
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
testbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainMenu.this, MainGame.class));
}
});
}
});
Is there something I missed when doing this? Please let me know how I can fix this.
Try setting the onclick on the button before showing the pop up
testbutton.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) {startActivity(new Intent(MainMenu.this, MainGame.class));}}); tutorial_popup.show();
So I found a solution to this answer, being that there was actually no need to add a buttonListener to the pop-up dialog as not only was it unnecessary, but it was also breaking the code. With how pop-up dialogs work in Android Studio, I realised through watching videos that you are able to click off of it without needing an "X" button or whatsoever, so I removed the buttonListener and tried again. Safe to say it ran perfectly this time with no problems whatsoever.
tutorialButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tutorial_popup.setContentView(R.layout.tutorial_popup);
tutorial_popup.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
tutorial_popup.show();
}
});

Going to second activity by password

I am new to coding.I want to code
in order to go to second activity using a button,
an editText and a pasdword in case it maches.
But it does n`t work.Please help me.Following is my code:
package com.mycompany.myapp;
import android.app.*;
import android.os.*;
import android.content.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText eText= (EditText)findViewById(R.id.mainEditText1);
String myCode=eText.getText().toString();
Button nextPage= (Button)findViewById(R.id.mainButton1);
if(myCode.equals("Titan")){
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
startActivity(new Intent(MainActivity.
this,actor.class));
}
});
}
}
}
Move your condition and getText() call in listener
nextPage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan")){
startActivity(new Intent(MainActivity.
this,actor.class));
}
}
}
So, here's what's happening:
You have an EditText field in your app, where the user types their password.
You capture that EditText in your code using EditText eText = ...
You then check to see if eText.equals("Titan");
If it matches Titan, you see the onClick listener.
But, when this code is ran, the EditText is always empty, so you never set the onClick listener!
The fix is fairly straightforward:
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
String myCode=eText.getText().toString();
if(myCode.equals("Titan")) {
startActivity(new Intent(MainActivity.this,actor.class));
}
}
});
Now, when the button is clicked:
You capture the value of eText as myCode.
You check if myCode is equal to Titan
If true (ie it matches), you then run startActivity(new Intent(MainActivity.this,actor.class));
You have written class name actor.class starting with a smaller case. Make it starts with uppercase Actor.class. Bellow is the answer to your questions.
Button nextPage = (Button)findViewById(R.id.mainButton1);
nextPage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v){
String myCode=eText.getText().toString();
if(myCode.equals("Titan"))
{
startActivity(new Intent(MainActivity.
this,Actor.class));
}else{
//password not matching
}
});

Method does not override another method View.OnClickListener (Error in Override)

I m trying to create a new java file for button (sin) as this is about calculator. But it is showing Error in second override annotations. (Method does not override method from its SuperClass). There is one more Error associating with brackets in the bottom part of code. Being newbie this is the first time I'm having my hands on this part of java. I have searched many of the sites but didn't found any solution. Any help will be appreciated.
SinglesinActivity.java
package com.marsh.calculator.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class SinglesinActivity extends Activity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.btnsin);
b.setOnClickListener(this);
}
#Override
public void onclick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin:
break;
}
};
}
It's onClick not onclick.
Make the following changes:
#Override public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btnsin: break;
}
};
Its a typo. change onclick to onClick.

Android/Eclipse - saving button error

I have some problems with the "setOnClickListener(onSave)” and “View.OnClickListener”
Below is my code:
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class LunchList extends Activity {
Restaurant r = new Restaurant ();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//save button
//be notified when the button is clicked
Button save =(Button)findViewById(R.id.save);
save.setOnClickListener(onSave){
}
private View.OnClickListener onSave=new View.OnClickListener();
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAdress(address.getText().toString());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
for save.setOnClickListener(onSave) I get the error save.setOnClickListener(onSave) and for View.OnClickListener I get the error Cannot instantiate the type View.OnClickListener.
I looked on this website and googled for this problem put I didn’t find a solution. I hope you guys can help me out.
Greets.
Remove semicolon after
private View.OnClickListener onSave=new View.OnClickListener();
and write onClick(View v) inside anonomous constructor of onClickListener
or else change your code with below code.
private View.OnClickListener onSave=new View.OnClickListener(
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.name);
EditText address=(EditText)findViewById(R.id.addr);
r.setName(name.getText().toString());
r.setAdress(address.getText().toString());
});
Button save =(Button)findViewById(R.id.save);
save.setOnClickListener(onSave);
try the following code:
start.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
mainactivity();
}
});

onClickListener not working that was copied from another working activity

I have created a new activity, added it to my manifest file and copy and pasted code from another fully functioning activity yet my buttons do not work when I click on them. Here is my activity:
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
public class test extends Activity {
private Button btnChangeDate;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.salesticketoilui);
mainProgram();
}
public void mainProgram() {
btnChangeDate = (Button) findViewById(R.id.btnChangeDate);
btnChangeDate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
} // end onClick
}); // end setOnClickListener
Button buttonExit = (Button)findViewById(R.id.buttonExit);
buttonExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitActivity();
} // end onClick
}); // end OnClickListener
// setup button listener for saving data and exit to main
Button buttonSaveExit = (Button) findViewById(R.id.buttonSaveExit);
buttonSaveExit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
saveExit();
} // end onClick
}); // end OnClickListener
} // end MainProgram ()
public void saveExit() {
// does stuff
}
public void exitActivity () {
// does stuff
}
} // end class
any thoughts?
Based on the code you have shown, it doesn't appear that you ever call the method mainProgram so your click listeners will never actually get setup. Either call mainProgram from onCreate or just put that code directly into onCreate.
I believe the onClickListeners need to go inside of your onCreate method.
Listen to Scott
and looks like your missing the #Override
new View.OnClickListener() {
#Override
public void onClick(View view) {
exitActivity();
} // end onClick
}
Make sure your java settings are to 1.6 to avoid code completion missing this.
I had the same problem copying OnClickListener with ImageButtons from a class to another class, and renaming then with bulk copy/paste.
To make it work, I had to create new buttons in my layout and declare the events manually. Weird!

Categories

Resources