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);
Related
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();
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~
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
I have an Android app, I need to add to it an AlertDialog that shows when the app starts. Also, I need to add to the AlertDialog a button (like: visit website), when the user click that button, it will open the link and browse it in the browser or anything else.
How I can do that?!
Thanks in advance.
Use an AlertDialog.Builder in your activities onCreate() method. You can use setPositiveButton() and launch your website in an intent, or webview when the user clicks it.
For example
new AlertDialog.Builder(mContext)
.setMessage("Launch Website")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Hide the dialog
dialog.dismiss();
// Launch the website
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
startActivity(intent);
}
})
.show();
See http://developer.android.com/reference/android/app/AlertDialog.Builder.html
First you have to add an AlertDialog to your onCreate() method in your Activity.
After then You have to add a button to that AlertDialog.
There are three types of button in AlertDialog.
Positive
Negative
Neutral
Use any one of them.
Then when the button clicked you need to go to the website url, in some browser in the android device.
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);
Try this:
final Context context = this;
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Click to visit website!")
.setCancelable(false)
.setPositiveButton("Go to web site",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.your-web-site-url.com"));
startActivity(browserIntent);
}
}));
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
I have this code in my android app, for an alert dialog and keyboard, and when the dialog is shown I want the keyboard to appear. But its not and I am not sure why?
Here is my code:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
alert.setView(input);
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
Log.i(TAG, "in focus");
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(input, InputMethodManager.SHOW_IMPLICIT);
}
}
});
But the keyboard does not appear even though the edit text is in focus, and I tell it to appear why, doesn't it come up?
Thanks for the help in advance.
I tested your code and the keyboard opened when alert.show() was executed. If you are using an emulator be sure that you have the virtual keyboard enabled. Otherwise a keyboard will never appear on the device screen.