When user set one item with "Consumido", he needs to set one rating to this item. I inflate the view with one rating bar, and when user clicks OK button, i try this.
builder.setNeutralButton(labelEditar, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if(regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais().get(position).isConsumido()){
regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais().get(position).setConsumido(false);
}
else{
regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais().get(position).setConsumido(true);
AlertDialog.Builder avaliacaoDialog = new AlertDialog.Builder(TelaCadastrados.this);
avaliacaoDialog.setTitle("Avalie este item!");
avaliacaoDialog.setView(R.layout.layout_alert_dialog_avaliacao);
avaliacaoDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
regraDeNegocioSingleton.getListaDeItensSingleton().getListaDeItensCulturais().get(position).setAvaliacao(reAvalia.getRating());
}
});
avaliacaoDialog.show();
}
Toast.makeText(TelaCadastrados.this, "Status de consumido alterado para: " + labelToast, Toast.LENGTH_SHORT).show();
}
});
On "Ok" click, the app crashes.
Thank you!
You set the view for your dialog but you are calling reAvalia.getRating() without ever setting reAvalia to anything. That's why you're getting a NullPointerExeption.
Related
I am trying to show a confirm AlertDialog before user press on back button or Activity going Pause.
I tried this code:
#Override
public void onBackPressed() {
super.onBackPressed();
AlertDialog.Builder confirmBuilder=new AlertDialog.Builder(DoExam.this);
confirmBuilder.setTitle("Confirm Exit");
confirmBuilder.setMessage("are you sure to exit form this activity");
confirmBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
confirmBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog confirmDialog=confirmBuilder.create();
confirmDialog.show();
}
But it disappeared immediately at the same time the app back to previous activity.
I tried also to put the code at onPause method but I got the same problem.
Any help about show alert dialog and back to previous activity if clicked yes and keep user in the Activity if clicked no?
Because you are calling super.onBackPressed(); remove that and see what happens. the super.onBackPressed(); is meant to trigger the default action of closing the activity. you need to defer that call when the user dismisses the dialog.
Remove super.onBackPressed(); and add it in onClick(...) of your posetive Yes button of your Alertdialog..
like this
confirmBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { #Override public void onClick(DialogInterface dialog, int which) { super.onBackPressed();
} });
use this code
#Override
public void onBackPressed() {
AlertDialog.Builder confirmBuilder=new AlertDialog.Builder(DoExam.this);
confirmBuilder.setTitle("Confirm Exit");
confirmBuilder.setMessage("are you sure to exit form this activity");
confirmBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// do any action you require on click
DoExam.super.onBackPressed();
}
});
confirmBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog confirmDialog=confirmBuilder.create();
confirmDialog.show();
}
I am using the technique brought up in previous StackOverflow questions, which suggest using TextViews and setting properties in them as the parameter for setTitle in an AlertDialog. I need this so I can style the font of the title.
The problem is that Android Studio says that it
cannot resolve method android.widget.TextView
Below is my code:
TextView settingsTitle = new TextView(this);
settingsTitle.setText("Settings");
settingsTitle.setTypeface(Typeface.createFromFile("monospace"));
new AlertDialog.Builder(this)
.setTitle(settingsTitle)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
Replace your code as
new AlertDialog.Builder(this)
.setTitle(settingsTitle.getText().toString())
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
as there is no such method called setTitle() which has an argument of type TextView.
Thus use, settingsTitle.getText().toString() which gets the String in the TextView and use that to set the title like
setTitle(settingsTitle.getText().toString())
Read more about it in the docs
It is possible to modified the checkbox align left in alert dialog?
this is activity.java file
AlertDialog dialog;
final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
// arraylist to keep the selected items
final ArrayList seletedItems=new ArrayList();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {
// indexSelected contains the index of item (of which checkbox checked)
#Override
public void onClick(DialogInterface dialog, int indexSelected,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
// write your code when user checked the checkbox
seletedItems.add(indexSelected);
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
// write your code when user Uchecked the checkbox
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
})
// Set the action buttons
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on OK
// You can write the code to save the selected item here
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on Cancel
}
});
dialog = builder.create();//AlertDialog dialog; create like this outside onClick
dialog.show();
}
.........................................................................
http://i.stack.imgur.com/rTpYb.jpg">
but... I want the checkbox displayed at the left side...anyone help me.
You can put custom view in your alertdialog. In that view you can easily align your checkbox where you want. For more read this tutorial. http://www.pcsalt.com/android/create-alertdialog-with-custom-layout/#sthash.yN5edpAX.FKWQjxBj.dpbs
Try this way,hope this will help you to solve your problem.
dialog = builder.create();
dialog.getWindow().setGravity(Gravity.LEFT);
dialog.show();
The code is not working. Please help me. It print the replace all string, but further code is not running.
when I debug this, there is no error in the code. It will show the code of alert box.
if(count>0)
{
System.out.println("replace all string name ");
// final Intent intent_ul=new Intent(this, UploadExcel.class);
AlertDialog.Builder alertDialogBuilder_ue = new AlertDialog.Builder(this);
alertDialogBuilder_ue.setTitle("Alert!!");
alertDialogBuilder_ue
.setMessage("Are you sure you want to Replace all the data related to this style ? ")
.setCancelable(false)
.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
mySQLiteAdapter.openToWrite();
mySQLiteAdapter.delete_style_measurement(style_no);
Log.d("","yes click");
count=0;
mySQLiteAdapter.close();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Log.d("","No click");
count++;
dialog.cancel();
// startActivity(intent_ul);
//finish();
}
});
}
Add these lines before the end of if condition
AlertDialog alertDialog = alertDialogBuilder_ue.create();
alertDialog.show();
You need to add
alertDialogBuilder_ue.show();
in your code
Check with this code. This code working for me
Context context = CurrentActivity.this;
AlertDialog.Builder ad = new AlertDialog.Builder(context);
ad.setTitle("Application");
ad.setMessage("Do you want to proceed?");
ad.setPositiveButton("Yes", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setNegativeButton("Cancel", new OnClickListener()
{
public void onClick(DialogInterface dialog, int arg1)
{
}
});
ad.setCancelable(false);
ad.show();
In your code adding alertDialogBuilder_ue.show(); should make the dialog appear.
By some people it has been suggested that you have to use alertDialogBuilder_ue.create(); to get a handle to the AlertDialog that you can then use the .show() method on.
Both are possibilities but you don't have to use the .create() option if you don't need a handle to the AlertDialog
I am validating an AlertDialog, and I would like to raise a Toast on top of the AlertDialog display.
I have this code, but the Toast is displayed on the activity
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
.setView(addView).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
if (wrapper.getTitle().length()>0)
{
processAdd(wrapper);
} else {
Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
}).show();
Instead of using AdvertDialog.Builder, you can create a custom dialog which will behave like a dialog, but is in fact a normal activity. Your toasts should be drawn normally on top of this.
Had this problem myself as well, when I wanted to show a validation message within a dialog.
The answer that seanhodges gave is probably the cleaner and better way. But a seperate activity wasnt practical for me, so i came up with this solution.
Anyway, you can use the AlerDialog.Builder, and show a toast.
If you override the OnClickListener of the button the you want to trigger the toast, you can show a toast on top of a dialog.
An example;
public void showToastOnDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing, you will be overriding this anyway
}
});
builder.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// You can implement code here, because you wont be
// overriding this
}
});
final AlertDialog dialog = builder.create();
// Make sure you show the dialog first before overriding the
// OnClickListener
dialog.show();
// Notice that I`m not using DialogInterface.OnClicklistener but the
// View.OnClickListener
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(context,
"I`m a toast on top of a dialog.",
Toast.LENGTH_LONG);
toast.show();
// Because you are overriding the OnClicklistener, the
// dialog will not auto dismiss
// after clicking
dialog.dismiss();
}
});
}
Try this:
AlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);