I'm trying to open up a pop-up window with 4 buttons on it that will dismiss when a button is pressed or when the user clicks outside of the pop-up window. I would just make an alert dialogue, but that will only support 3 buttons.
There have been a lot of questions about this same thing, and I can't find any consistent answer or any answer that works for me (including the deprecated Bitmap Drawable). I've put all of the suggestions I've seen into my code, but to no avail.
Here's everything I've used so far:
//to create new popup window
LayoutInflater chooseMealInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View chooseMealLayout = chooseMealInflater.inflate(R.layout.choose_meal_dialog, null);
PopupWindow chooseMealPopup = new PopupWindow(chooseMealLayout, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
//to make popup dismiss on touch outside
chooseMealPopup.setOutsideTouchable(true);
chooseMealPopup.setFocusable(true);
chooseMealPopup.setContentView(chooseMealLayout);
chooseMealPopup.showAtLocation (chooseMealLayout, Gravity.CENTER,0,0);
chooseMealPopup.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
I've tried to find everything I can, like keeping setFocusable before showAtLocation, but when I run the app, nothing happens when I click. Figured it might be something individual to my code, since I'm new and don't really know what I'm doing.
don't know what you really want to do...
if you want show dialog when click button
YourBtn.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//what you want to do like show dialog
}
});
Related
I found on Stackoverflow something like that about checking when dialog is visible:
onView(withText("Yes"))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(click());
Of course this works if Dialog with button 'yes' is visible, but in different scenario, if dialog will be invisible I got crash:
android.support.test.espresso.NoMatchingViewException: No views in
hierarchy found matching: with text: is "Yes"
So how to write that if the dialog exists, click yes, and if it does not exist, then nothing will be clicked?
You could try this:
onView(withText("Yes")).inRoot(isDialog()).withFailureHandler(new FailureHandler() {
#Override
public void handle(Throwable error, Matcher<View> viewMatcher){
}
}).check(matches(isDisplayed())).perform(customClick());
//if dialog is visible, perform click, otherwise do nothing.
I am showing a list with messages and each message's row has a comment button.When i click on comment button opens a comment box with edit text and button for submitting the comment.When comment box appears on screen keypad also appears for entering text.If i pressed home button before entering text then application goes background but keypad remains on screen.This is the thing irritating me.For custom list i am using a custom adapter and code for comment box is written in that adapter.I tried using
inputmgr.hideSoftInputFromWindow(txtComments.getWindowToken(), 0);
but it is not working. So how i can hide this keypad programmatically.
Try using the code in https://stackoverflow.com/a/1109108/1904479. Hope you are not testing it in Android version 4.1.
please use this method to hide soft keyboard.
public static void hideSoftKeyboard(Activity context) {
InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputManager != null)
inputManager.hideSoftInputFromWindow(context.getWindow().getDecorView().getApplicationWindowToken(), 0);
context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
KeyEvent.KEYCODE_HOME can NOT be intercepted. You can hide the keypad inputmgr.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); in onStop() method of your activity.
It does not require token from focused view editText.
In my app, I have a dialog with EditText. I have coded it in such a way that as soon as the dialog shows up,the soft keyboard shows up, the edit text will get focus , all the existing text in the editText is preselected. And user can start typing. This all works fine when I am holding my phone in portrait orientation. But the same thing does not work when I have phone in landscape orientation. Part of my code in my fragment is pasted below.
final EditText inputDeviceId = new EditText(getActivity());
inputDeviceId.setInputType(InputType.TYPE_CLASS_TEXT);
String savedDevId = getDeviceID();
inputDeviceId.setText(savedDevId);
inputDeviceId.setSelectAllOnFocus(true);
// some more code ... to create dialog , ok cancel buttons then,
final AlertDialog alertDialog = alertDialogBuilder.create();
inputDeviceId.setOnFocusChangeListener(new View.OnFocusChangeListener() {
if(hasFocus){
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
});
alertDialog.show();
Now this all works in portrait orientation and does not work in landscape.
In landscape mode the dialog with edittext shows up , it has focus and existing text is preselected but it does not show the keyboard. User has to click in the editText for keyboard to come up. Why? I am using nexus-5 to test.
Is it possible to make the background behind an AlertDialog opaque or black without using a Dialog / CustomDialog, I currently have this,
<item name="android:backgroundDimEnabled">true</item>
in my style which I assign to the constructor of the AlertDialog. but it is not dark enough and users can still see information which I do not want them to yet. To be honest my reason for not wanting to use a Dialog is that I have already got everything working with the AlertDialog and thought it would be a good idea to check if anyone knew a quick way to do this.
private void dimActivity(Dialog dialog, float dimAmount) {
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.dimAmount = dimAmount;
dialog.getWindow().setAttributes(lp);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
}
dimAmount: 0 - no dimming, 1.0f full dimming. You can check this: How to dim a screen on click of a button in android?
use this code this might help you
final Dialog dialog = new Dialog(context);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
I have source code for a slider puzzle. it delivers a random image for the puzzle and you can change alot of settings like tiles and numbers and what not. Its really easy to add pictures you just have to have them start with image_ and put them in the drawable folder. Only thing is it does not have a layout.xml i guess everything is written through the activity.
I want to have all of the pictures in like a gallery view when a button is clicked in the menu. i have seen all the gallery codes need code in the layout which this source code does not have so essentially i would like
rather than this
public void showAbout()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.title_about);
builder.setMessage(R.string.content_about);
builder.setPositiveButton(R.string.label_ok, null);
builder.setIcon(R.drawable.ic_launcher);
AlertDialog dlg = builder.create();
dlg.show();
((TextView) dlg.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
}
i just want it to show up as a photo gallery