I am a beginner, so if anyone would help me out. I created a list in the dialogue box , now how do i use those options? Like click one and it does something , click another and it does something else.
CharSequence features[] = new CharSequence[] {"Save", "Send", "Something", "Something"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
alertDialog.setTitle("Options");
alertDialog.setItems(features, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,"Eta chu ma aile",
Toast.LENGTH_LONG).show();
}
});
alertDialog.show();
return true;
}
If you know exact position of every item, just compare it with which param.
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
// handle "Save" option
} else if (which == 1) {
// handle "Send" option
} ...
}
You can use following code:
Somewhere in another function:
String title = "My Alert Box";
String msg = "Choose Option";
alertfunc(title,msg);
The main alert function:
private void alertfunc(String title, String msg) {
if (title.equals(TASK_VIEW_PROFILE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle(title)
.setMessage(msg)
.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
//Do something
}
})
.setNegativeButton("Send",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
.setNegativeButton("Something",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which)
{
//Do something
}
}).create().show();
//...and so on
}
}
Related
guys I've been trying to solve this problem but I couldn't
I want when the user click on the btn_delete he will get a message to insure the delete (Yes or No), I've tried a lot of methods but I don't know exactly what's the problem, I'm new in Android programing so forgive me for my stupid questions, here is my Java code :
public void onDeleteClick(View v) {
int i = Integer.parseInt((String)v.getTag());
Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
_dbAdapter.deleteAddress(address.Id);
_GetAddresses();
}
Replace the onDeleteClick method with the following method:
public void onDeleteClick(View v) {
int i = Integer.parseInt((String)v.getTag());
AlertDialog.Builder alert = new AlertDialog.Builder(AddressListActivity.this);
alert.setTitle("Delete");
alert.setMessage("Are you sure you want to delete?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Address address = _list.get(_currentPage*PANELS_PER_PAGE + i);
_dbAdapter.deleteAddress(address.Id);
_GetAddresses();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
}
I am trying to get a number of cards to pop up using alertdialog.builder. Even though I did .create().show(); the dialog does not show on my screen. I'm not sure what is causing this problem.
I have marked the place where I am getting nothing in the comments.
Java Code:
ImageView image_questionmark= new ImageView(this);
final ImageView image_pass = new ImageView(this);
final ImageView image_youpay = new ImageView(this);
image_questionmark.setImageResource(R.drawable.card_questionmark);
image_pass.setImageResource(R.drawable.card_pass);
image_youpay.setImageResource(R.drawable.card_youpay);
AlertDialog.Builder dialog = new AlertDialog.Builder(PayActivity.this);
for(int i=0; i<people; i++) {
/*PASS*/
if(array[i] == 0) {
dialog.setView(image_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(image_pass)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
}).create().show(); /*HERE: Nothing showed on my screen when running debugger...*/
}
});
dialog.create().show();
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}/*YOU PAY*/
else {
dialog.setView(image_questionmark);
dialog.setPositiveButton("FLIP", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(PayActivity.this)
.setView(image_youpay)
.setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish(); /*move on to next value in array*/
}
}).create().show();
}
});
dialog.create().show();
/*If not the first card, show previous card*/
if(i!=0) {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
i--; /*return to previous value in array*/
} /*First card*/
else {
dialog.setNegativeButton("PREVIOUS", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "No previous card", Toast.LENGTH_LONG).show();
}
});
}
}
}
Thank you for your time :)
I did not try your code.But you can try it like -
First initilise your image object then assign to another final variable.
I think it will work.
ImageView image_pas1 = new ImageView(this);
final ImageView image_youpay = new ImageView(this);
image_questionmark.setImageResource(R.drawable.card_questionmark);
image_pass1.setImageResource(R.drawable.card_pass);
image_youpay.setImageResource(R.drawable.card_youpay);
final ImageView image_pass=image_pass;
You need to set LayoutParams for your ImageView in order to know how to draw itself. Maybe something like this:
image_questionmark.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // width
LayoutParams.WRAP_CONTENT)); // height
And do this as well for your other 2 ImageViews
First of all, Dialogs need
buildervariablename.create();
only.
Second, every single Dialog has to be in a seperate class, like this:
public class TestDialog extends DialogFragment
{
// Put your dialog code in here
}
And then you just call your dialog there, where it is needed. Inside another method, or even another dialog, by using the following:
DialogFragment fragment = new TestDialog();
fragment.show(getFragmentManager(),"testdialog");
Hope this helped you, if you have further questions, just comment this answer.
im trying to get text from edit text and check if it is True or False and if not display an error message
correcttxt=correctans.getText().toString();
if ((!correcttxt.equals("True")) || (!correcttxt.equals("False"))){
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminAdd4.this);
alertBuilder.setTitle("Invalid");
alertBuilder.setMessage("The correct answer should be True or False.");
alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertBuilder.create().show();
}
else {
//confirmation to add exercise
AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminAdd4.this);
alertBuilder.setTitle("Add Exercise");
alertBuilder.setMessage("Are you sure you want to add this exercise?");
alertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
new CreateNewexercise().execute();
}
});
alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertBuilder.create().show();
}
but this will always display the error message even when the text is True or False i don't know why ?
can someone help please ? thank you :)
I'm trying to make a SingleChoiceItems dialog activated from a listview onItemLongCLick which has 3 options via radiobuttons 1.View Profile 2.Send Message 3.Delete Friends and from the way I have it set up,only the first option toast works on all 3 options.
How would I make it that options 2 and 3 does their actions instead of doing option 1's actions?
Here is the code to my dialog,
#Override
public Dialog onCreateDialog(int id) {
String[] items = { "View Profile", "Send Message", "Remove Friend" };
final DBAdapter db = new DBAdapter(this);
db.open();
switch (id) {
case 0:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle("Select a option")
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
int choice = 0;
// TODO Auto-generated method stub
mChoice = choice;
}
})
.setPositiveButton("OK", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
// TODO Auto-generated method stub
if (mChoice == 0) {
Toast.makeText(getBaseContext(), "Test 1",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 1) {
Toast.makeText(getBaseContext(), "Test2",
Toast.LENGTH_SHORT).show();
} else if (mChoice == 2) {
TextView friends = (TextView) findViewById(R.id.textview_friends);
String deletedfriend = friends.getText()
.toString();
db.DeleteFriends(deletedfriend);
Toast.makeText(getBaseContext(),
"Friend Removed", Toast.LENGTH_SHORT)
.show();
}
}
}
)
.setNegativeButton("Cancel", new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int choice) {
}
})
.create();
}
return null;
}
Remove choice = 0;
.setSingleChoiceItems(items, id,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
mChoice = which;
}
})
I currently have this:
Builder yesandno = new AlertDialog.Builder(this);
yesandno.setTitle("QuickResponse");
yesandno.setMessage(message);
yesandno.setPositiveButton("YES", null);
yesandno.setNegativeButton("NO", null);
yesandno.show();
How should I go by setting an event listener that will capture if the user clicked YES or NO?
When you call setPositiveButton() and setNegativeButton() instead of passing in null you should pass in a DialogInterface.OnClickListener.
For example:
yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//User clicked yes!
}
});
Just do something like:
yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked yes
}
});
yesandno.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked no
}
});
and do whatever you want in the button callbacks.