How to use a dialogue box within a popup window? - java

I have a popup window that gets executed when a button is pressed. When the user clicks "home" within the popup window I want a dialogue message to pop up asking the user if they are sure they want to quit.
Here is how I declare the popup window:
LayoutInflater inflater = (LayoutInflater) endlessflags.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.success_menu, (ViewGroup) findViewById(R.id.myPop5));
pwindo4 = new PopupWindow(layout, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
pwindo4.setAnimationStyle(R.style.AnimationPopup);
pwindo4.showAtLocation(layout, Gravity.TOP | Gravity.CENTER, 0, 0);
And then I have a onTouchListener for the button that are also called in the popup window. I try to call the dialogue function I created but when I do nothing happens and I get no errors or crashes:
AlertDialog.Builder myAlert = new AlertDialog.Builder(mainActivity.this);
myAlert.setMessage("Would you like to quit?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveState = true;
saveState();
//finish();
dialog.cancel();
}
}).setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
saveState = false;
dialog.cancel();
}
});
My question is why is it that when I call the dialogue event function nothing seems to happen and how can I fix this so the dialogue box shows up when I click a button within my popup window?

You need to show the dialog:
AlertDialog dialog = myAlert.create();
dialog.show();

Related

How to change the color of buttons in Alert Dialog? [duplicate]

This question already has answers here:
How can I change default dialog button text color in android 5
(18 answers)
Closed 4 years ago.
i am working in a social app and while the user wants to edit their feeds i want to give them a pop up alert dialog from where the user can edit their post. I tried the following code but the result that gave was not good
i want to change the color and want a better design .. what can be done for this
private void EditCurrentPost(String description)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ClickPostActivity.this);
builder.setTitle("Edit post");
final EditText inputField = new EditText(ClickPostActivity.this);
inputField.setText(description);
builder.setView(inputField);
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ClickPostRef.child("description").setValue(inputField.getText().toString());
Toast.makeText(ClickPostActivity.this,"Post Updated",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Dialog dialog = builder.create();
dialog.show();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.holo_purple);
}
There is a very simple way to make dialog with custom layout: Please review the code below:
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_news_description);//Your custom layout
TextView sometextview = dialog.findViewById(R.id.textView);// Textview in your custom layout
Button somebutton = dialog.findViewById(R.id.button_done);// Button in your layout
somebutton.setOnClickListener(new View.OnClickListener() {//on button click listener
#Override
public void onClick(View view) {
//DO your job....
//then...
dialog.dismiss();//dismiss the dialog
}
});
dialog.show();
You cannot change the default AlertDialog layout but you can inflate a custom layout to it. To start with you can check this post http://android-coding.blogspot.com/2011/07/create-custom-dialog-using.html

Android : Multiple Dialogs (or multiple dialog views)

In my app I need multiple dialogs or multiple views which will be updated after clicking positive and negative dialog buttons.
How should it looks in example:
1) Call first Dialog1
2) Inside Dialog1 I have some data and 2 buttons (positive and negative) onClick possitive Button I go to next Dialog2 on negative I exit dialogs.
3) Inside Dialog2 similar situation click on possitive button provides me to next dialog or dialog view but negative button leeds back to Dialog1
for now my code looks like :
public class DialogChoiceActivity extends DialogFragment {
LayoutInflater inflater;
View v;
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.dialog_email,null);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final Dialog dialog2 = builder.create();
builder.setTitle("Email " + " 1/10");
builder.setView(v).setPositiveButton("NEXT", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
}).setNegativeButton("Exit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.create();
}
First of all I'm not sure which way is better create multiple dialogs or one Dialog with multiple Views. Which way is better if I want easily move from one dialog to another (or view). There are some problems because I cant update builder object or dissmiss it so how shold all this looks like ? what is best way to do that
Sorry for chaotic and weak language.
Create DialogFragment as you needed.
Make clicking on the positiveButton to show next dialog and dismiss current dialog.
From next dialog, clicking on negativeButton to show previous dialog and dismiss current one.

Place icon at right of title in a AlertDialog builder

All the answers and info that i find at internet seems old and deprecated, i could add a simple icon at my title bar at left (user image), but i need to have 2 more images, a message and a phone at the right, like a simple user details view, the problem is that i don't know how to set the position of those images, the only thing i could do was this:
AlertDialog.Builder builder = new AlertDialog.Builder(Friends.this);
final User user = usersList.get(position);
builder.setTitle(user.getUsername());
builder.setMessage("You wanna delete friendgroup: " + user.getUsername());
builder.setIcon(R.drawable.ic_user);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//myRef.child("Users").child(userId).child("FriendLists").child(friend.getName()).removeValue();
//friendList.remove(friend);
//mAdapter.notifyDataSetChanged();
}
});
builder.show();
with the builder.setIcon i could add a image at the left, but how can i add more icons and at the right of my title ? do i really need a custom builder to do this?
Thanks
You have to make a custom AlertDialog.
Try this:
Design a RelativeLayout that contains your icon, title and right message and phone icon and other things.
Set this layout to your AlertDialog using setView() method.
AlertDialog.Builder builder = new AlertDialog.Builder(Friends.this);
final User user = usersList.get(position);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_dialog_layout, null);
builder.setView(dialogView);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//myRef.child("Users").child(userId).child("FriendLists").child(friend.getName()).removeValue();
//friendList.remove(friend);
//mAdapter.notifyDataSetChanged();
}
});
builder.show();
Here is a good tutorial
Hope this will help~

java Android - two dialogs, prevent first dialog from closing after second one exits

Initially in my app I am creating an AlertDialog which has three buttons, in which the middle button opens up another AlertDialog. The problem is that when the second AlertDialog closes after a button is pressed, the first one closes with it. I think both AlertDialogs get closed after I press a button on the second AlertDialog.
What I want is for the first AlertDialog to open another AlertDialog that has its own buttons, and when second AlertDialog presses a button, it only closes itself and goes back to the first one. Is there any way to achieve this?
Here is the code for the button used to open the AlertDialog:
final ImageButton fabgroup = (ImageButton) findViewById(R.id.groupButton);
Here's the code for a button that opens an AlertDialog that contains another button that opens another AlertDialog using the middle button (create button) on itself, but closes them both when a button on the second one is pressed (either the yes or no button, which is not what I want as I only want the second one to close itself and go back to the first AlertDialog, and yea this sounds pretty confusing in theory so I can try to clarify if needed):
fabgroup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder helpBuilder = new AlertDialog.Builder(CreateNote.this);
helpBuilder.setTitle("Select a group");
helpBuilder.setMessage("Add to group?");
final TextView input = new TextView(mainactiv.this);
input.setSingleLine();
input.setText("");
helpBuilder.setView(input);
helpBuilder.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
Toast.makeText(CreateNote.this, "Page has been added to group", Toast.LENGTH_SHORT).show();
}
});
helpBuilder.setNeutralButton("Create", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//open another alertbox
AlertDialog.Builder helpBuilder2 = new AlertDialog.Builder(CreateNote.this);
helpBuilder2.setTitle("Assign a new group");
helpBuilder2.setMessage("Create group?");
final EditText input = new EditText(CreateNote.this);
input.setSingleLine();
input.setText("");
helpBuilder2.setView(input);
helpBuilder2.setNegativeButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Create Group
Toast.makeText(CreateNote.this, "Group has been created", Toast.LENGTH_SHORT).show();
}
});
helpBuilder2.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog2 = helpBuilder2.create();
helpDialog2.show();
}
});
helpBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing
}
});
// Remember, create doesn't show the dialog
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
});
Help would be greatly appreciated.
I eventually managed to solve this problem by creating two separate functions to generate each dialog box, and when one closes it calls the function to create the other one, kinda like recycling (or maybe closer to looping functions). Although, I'm not entirely sure how performance heavy this is, but it seems to do the job without any issues from what I'm testing. If anyone would like to chime in on how this could be an issue, then I'm open to hearing what others have to say about the negative points of using alert dialog boxes this way.
You can show an activity as dialog. Put this in your manifest file.
<activity android:theme="#android:style/Theme.Dialog" android:excludeFromRecents="true"/>
From this answer: Android Activity as a dialog

How to make the virtual keyboard go away?

I have an EditBox in my AlertDialog popup. I made it to popup the virtual keyboard the moment the AlertDialog pops up (so that I don't need to click on that white field to show the keyboard), this way:
InputMethodManager imm = (InputMethodManager)
Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
But when I'm done typing I need to click "Done" on the keyboard and then OK in AlertDialog. The problem is, that users go right to the OK button once they're done typing and after clicking OK, the virtual keyboard stays onscreen. They now have to click back button on their device. How to clear the keyboard once the OK button is pressed?
Here's my whole AlertDialog code, if it helps:
case R.id.bKonacno:
}
LayoutInflater layoutInflaterK = LayoutInflater.from(context);
View promptViewK = layoutInflaterK.inflate(R.layout.popup_answer, null);
AlertDialog.Builder alertDialogBuilderK = new AlertDialog.Builder(context);
// set prompts.xml to be the layout file of the alertdialog builder
alertDialogBuilderK.setView(promptViewK);
final EditText inputK = (EditText)promptViewK.findViewById(R.id.userInput);
InputMethodManager imm = (InputMethodManager)
Asocijacije.this.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null){
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
}
alertDialogBuilderK
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
//some code of mine
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alertK = alertDialogBuilderK.create();
alertK.show();
break;
You can do the following when Ok/Cancel button is pressed.
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Read Close/hide the Android Soft Keyboard for more. Hope it helps.
Try adding the following to your buttons:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Categories