So I have a button with an on click listener. It's supposed to open a dialog window with options... but it doesn't.
Anyone know what the problem is?
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
}
});
EDIT (UPDATED CODE):
seeTexts.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
You just don't call the show method of your AlertDialog object
Related
I have a created a app with List view when i click back button from home page it will toast a message and ask to exit or not. after i have implement a Navigation drawer and set open and close. but when i click back button from navigation go back and popup the toast which i set to hoe page.
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
super.onBackPressed();
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
how to stop the toast message when i click back from navigation bar and set toast from home page.
hi You can change your code like this
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
finish();
AdminHome.super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
Put the AlertDialog code in else block.
Put your alertdialog inside else of your condition
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
}else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to Exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
super.onBackPressed();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
I have two dialog.i open dialog one and click one button in adapter listview in dialog, and i want open another dialog to ask question for are you sure delet this item or no.
But just i can open one of them at a time.
What can i do?
viewHolder.btnDelet.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DialogDelet(context).setOnListener(new ListenerDelet() {
#Override
public void onSuccess() {
listenerAdapterUserAndRole.unSelect(item);
}
#Override
public void onCancel() {
}
}).Show();
}
});
You can cancel first dialog when user is confirm or you want to open next dialog
like:
public void open(){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setMessage("Are you sure,
You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(MainActivity.this,"You clicked yes
button",Toast.LENGTH_LONG).show();
//cancel first dialog here
firstDialogObj.cancel;
// do your stuff contnious what you want
}
});
alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
case R.id.Delete_Contact:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
return true;
default:
return super.onOptionsItemSelected(item);
How can i use this in an button click event ? This works when clicking menu button
//Initialize your button
//Set Click Listener to that like below
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Perform Some Action
//common method to be called
deleteConfirmation();
});
public void deleteConfirmation(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.deleteContact)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mydb.deleteContact(id_To_Update);
Toast.makeText(getApplicationContext(), "Deleted Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog d = builder.create();
d.setTitle("Are you sure");
d.show();
}
});
}
You should use common method as you want same code to be executed multiple times. This is just a suggestion
I have an AlertDialog having the text "For more information, Please Call 1-800-200-1000".
Here is my Code for the Alert Dialog Display while clicking on the ListView Item :
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
final SpannableString s = new SpannableString("1-800-200-1000");
Linkify.addLinks(s, Linkify.ALL);
ListView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
builder.setMessage("For more information, Please Call "+s)
.setCancelable(true)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}});
Here i wish to hyperlink the "1-800-200-1000" and while clicking on this, another dialog for call function should be implemented.
Here is my AlertDialog for Call Function:
final Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to Call?");
builder.setCancelable(false);
builder.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");dial.setData(Uri.parse("tel:"+ Phone));
startActivity(dial);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Toast.makeText(getApplicationContext(),"Activity will continue",Toast.LENGTH_LONG).show();
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Please help me in two problems:
1.How to hyperlink the Phone number in the First Dialog?
2.how to embed the Call AlertDialog while clicking the Hyperlinked Phone Number?
Thanks in advance.
try this one:
Spannable spans = (Spannable) text;
ClickableSpan clickSpan = new ClickableSpan() {
#Override
public void onClick(View widget) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"+ url));
}
public void updateDrawState(TextPaint ds) {
//override link-centric text appearance
}
};
int index = (text.toString()).indexOf(url);
spans.setSpan(clickSpan, index, url.length() + index,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
I created a Button,While i Click that Button a new Dialog Box is Displayed.In that dialog Box i have set icon and text.text is Displayed but icon(image) is not displayed in dialog Box.
Here my Coding
Button btnteam_westernbulldogs=new Button(this);
btnteam_westernbulldogs.setId(team_westernbulldogsid);
btnteam_westernbulldogs.setBackgroundResource(R.drawable.team_westernbulldogs);
public void onClick(View v){
createbtnteam_westernbulldogs();
}
});
public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setMessage("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
createText();
}
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
} });
Try this..
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setIcon(R.drawable.icon)
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alert = builder.create();
public void createbtnteam_westernbulldogs()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_westernbulldogs);
alertDialog.setTitle("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
createText();
}
alertDialog.setIcon(R.drawable.icon);
alertDialog.show();
} });
You must call setTitle with non-empty string!
Icon is displayed only if there's also title.