Error opening AlertDialog with a list - java

I need some help to run an AlertDialog from my application. It won't compile while there are some errors. Especially at the line alertDialog.setItems(items, new DialogInterface.OnClickListener() with the setItems().
Please take a look.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nastavenie_casu);
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
final CharSequence[] items = {"30 minút", "45 minút", "60 minút", "75 minút", "90 minút"};
Button tlacidlo = (Button) findViewById(R.id.spusti);
EditText pripomienka = (EditText) findViewById(R.id.upozornit_za);
pripomienka.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Intent myIntent = new Intent(view.getContext(), agones.class);
// startActivityForResult(myIntent, 0);
AlertDialog alertDialog = new AlertDialog.Builder(NastavenieCasu.this).create(); //Read Update
alertDialog.setTitle("hi");
alertDialog.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do something
}
alertDialog.show();
});
}
}
}

setItems() is part of the AlertDialog.Builder class, not AlertDialog. Try:
AlertDialog.Builder builder = new AlertDialog.Builder(NastavenieCasu.this);
builder.setTitle("hi");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//
// do something
}
AlertDialog alertDialog = builder.create();
alertDialog.show();

Related

The button that I have created does not work. Is there something wrong with my code?

I am building an app in Android studio and basically I want a window to popup when a user clicks the add button. I used the setOnClickListener but when I run the app, nothing happens. Could there possibly something wrong with my code?
Here's my MainActivity code
public class MainActivity extends AppCompatActivity {
Button addBtn;
ListView itemListView;
DatePickerDialog.OnDateSetListener dateSetListener;
String dateString = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addBtn = (Button)findViewById(R.id.addBtn);
itemListView = (ListView)findViewById(R.id.itemListView);
addBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.activity_popup_window, null);
EditText itemName = (EditText)view.findViewById(R.id.itemName);
Button expirationDateBtn = (Button)view.findViewById(R.id.expirationDateBtn);
builder.setView(view)
.setTitle("Add Item")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
})
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (itemName.getText().toString().isEmpty() || dateString == null) {
Toast.makeText(MainActivity.this,
"Item name or expiration date is missing",
Toast.LENGTH_LONG).show();
}
else{
//do action
}
}
});
//when clicked on Expiration Date Btn
//display date on button
AlertDialogBuilder doesn't create and show a new AlertDialog implicitly. It only prepares the dialog before explicitly calling create() (or you can directly call show() if you need to display your dialog in the moment it gets built).
Your code misses the following lines at the end of onClick():
AlertDialog dialog = builder.create();
dialog.show();
or just:
builder.show();

Android: What is the best practice to organize methods to show alert dialog

I am beginner in Android development. Suppose I have some methods to show AlertDialog within an Activity. But each AlertDialog behavior is slightly different. What is the best practice to organize the methods to show AlertDiaolog?
code is like this.
private void showNumberPickerDialog() {
LayoutInflater inflater = this.getLayoutInflater();
View numberPickerDialogView = inflater.inflate(R.layout.number_picker, null);
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title for number picker here");
alertDialog.setCancelable(false);
alertDialog.setView(numberPickerDialogView);
final NumberPicker numberPicker = roomSizeNumberDialogView.findViewById(R.id.number_picker);
numberPicker.setMaxValue(10);
numberPicker.setMinValue(0);
numberPicker.setWrapSelectorWheel(false);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Something here
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
private void showMessageDialog(final boolean isA) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Title here");
alertDialog.setCancelable(false);
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (isA) {
doA();
} else {
doB();
}
}
});
alertDialog.show();
}
private void showAlertDialogC() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final EditText inputEditText = new EditText(this);
inputEditText.setInputType(InputType.TYPE_CLASS_TEXT);
innputEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
alertDialog.setTitle("Title here");
alertDialog.setCancelable(false);
alertDialog.setView(nameEditText);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do something here
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
}
Is there a good way to organize the parts like this?
You can also use a customizable dialog if that's what you are looking for.
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_options);
dialog.show();
TextView tvDelete = dialog.findViewById(R.id.tvDelete);
tvDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Dialog deleteDialog = new Dialog(context);
deleteDialog.setContentView(R.layout.dialog_delete);
deleteDialog.show();
}
});
Treat it just like you would treat an activity. Set onClickListeners on the views for which you want some particular actions. I believe this custom dialog is much more flexible than the AlertDialog
I like to handle my dialog in a separate class, that way you have more control over everything - clickListners, layout design, etc... and you don't have tons of code lines in your activity.
For example, create dialogClass:
public class ProgressDialog extends Dialog {
public ProgressDialog(#NonNull Context context) {
super(context);
setContentView(R.layout.progress_dialog); //this is your layout for the dialog
}
}
And all you need to do is to create dialog instant and call it like this:
ProgressDialog progressDialog = new ProgressDialog(getContext());
progressDialog.show(); // this line shows your dialog

How to wait for an okay button before starting a new activity

Good day.I have a problem, I am displaying a message box when a button is clicked.The message box simple shows a confirmation of registration.After that I open a new activity.
The problem is that it shows the messagebox and then starts the new activity without waiting for the ok button to be clicked.How can I only display the new activity upon clicking the ok button.
Below is the code I used.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Button btn = (Button)findViewById(R.id.registerButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);
dlgAlert.setMessage("You have successfully Registered.Please Press okay to continue");
dlgAlert.setTitle("Registration");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(false);
dlgAlert.create().show();
startActivity(intent);
finish();
}
});
Changed the code to
#Override
public void onClick(View view) {
// Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);
dlgAlert.setMessage("You have successfully Registered.Please Press okay to continue");
dlgAlert.setTitle("Registration");
dlgAlert.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
startActivity(intent);
}
});
dlgAlert.setCancelable(false);
dlgAlert.show();
Don't start activity there. Remove the line startActivity(intent) and finish(). You need to do this
builder.setPositiveButton(R.string.label_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(your arguments here)
startActivity(intent);
}
});
builder.show();
So all you need to do is change the line to setPositiveButton and use as given above.
As per your style, you are not setting action to the dialog but to the button that displays the dialog.
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
Button btn = (Button)findViewById(R.id.registerButton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);
dlgAlert.setMessage("You have successfully Registered.Please Press okay to continue");
dlgAlert.setTitle("Registration");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(false);
dlgAlert.create().show();
dlgAlert.setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(getApplicationContext(), BookingActivity.class);
startActivity(intent);
}
});
}
});

How do I code for user input in an alert dialog for an android app?

I have an android app that has two buttons. So basically what I am trying to do is when a user clicks the 'Share Button' on the page, an alert dialog box will appear that will prompt the user to enter their email address. Here is what I have so far. I've tried an EditView but it hasn't been working well for me.
Thanks in advance!
package com.colors;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
private Button shareButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
shareButton = (Button) findViewById(R.id.shareButton);
// add button listener for Welcome Message.
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set the title of the Alert Dialog
alertDialogBuilder.setTitle("Welcome!");
// set dialog message
alertDialogBuilder
.setMessage("Program Description here...")
.setCancelable(false)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if no is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
// Button listener for Share Button
shareButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set the title of the Alert Dialog
alertDialogBuilder.setTitle("Share");
// set dialog message
alertDialogBuilder
.setMessage("Would like to a user input here.")
.setCancelable(false)
.setPositiveButton("Send!",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if no is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
// if no is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
});
}
}
This may helpful to you.
shareButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder al1 = new Builder(
AgriListView.this);
al1.setMessage("Share Something");
al1.setPositiveButton("Share",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
final EditText input = new EditText(
AgriListView.this);
input.setSingleLine();
AlertDialog.Builder al = new Builder(
AgriListView.this);
al.setTitle("Enter New Value");
al.setView(input);
al.setCancelable(true);
al.setIcon(R.drawable.bt);
al.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
int len = input
.length();
if (!(len == 0)) {
Toast.makeText(
AgriListView.this,
"Entered text is: "+input.getText()
,
Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(
getApplicationContext(),
"Enter Value Properly",
Toast.LENGTH_LONG)
.show();
}
}
});
al.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert = al.create();
alert.show();
}
});
al1.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
dialog.cancel();
}
});
AlertDialog alert1 = al1.create();
alert1.show();
}
});
This code create dialog with Edittext value.
You could do something like this.
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();

How to use input dialog box

I want to add dialog box in my app to let the user put his/her desire Ip address.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please put Ip address")
------> here the user can type on the dialog in String
.setNeutralButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
printer = new PrinterObject("134.188.204.155");--->the result text from dialog
....
}
});
AlertDialog alert = builder.create();
alert.show();
Anybody knows how to add it?
Try this...
AlertDialog.Builder builder = new Builder(this);
builder.setTitle("Please put Ip address");
builder.setMessage("");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.alert, null);
final EditText ipfield = (EditText) view.findViewById(R.id.ipfield);
builder.setView(view);
builder.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
// do what you need
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int arg1) {
dialog.dismiss();
}
});
AlertDialog dialog = builder.create();
dialog.show();
You can use this snippet:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Please put Ip address");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();

Categories