I have a simple ProgressDialog but I realized if I press the search button the dialog will be dismiss. What can I do to disable search button press during this process?
dialog = new ProgressDialog(Main.this);
dialog.setTitle("Working in progress");
dialog.setMessage("Please wait...");
dialog.setCancelable(false);
dialog.show();
I put this and it didn't work.
dialog = new ProgressDialog(Main.this){
#Override
public boolean onSearchRequested() {
return false;
}
};
This don't work either.
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH && event.getRepeatCount() == 0) {
return true; // Pretend we processed it
}
return false; // Any other keys are still processed as normal
}
});
Try putting
#Override
public boolean onSearchRequested() {
return false;
}
in the activity, instead of the dialog.
Edit: Also try adding the key listener code to the activity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// your code
}
Related
I have this dialog, with just a textView and no buttons. It displays some info and I need to change info on key press left and right. Unfortunately the dialog closes on any key.
This code in MainActivity invokes the dialog (redundant code omitted)
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
if (event.getAction() == KeyEvent.ACTION_UP) {
InfoDialog infoDialog = new InfoDialog();
infoDialog.showDialog(this,currentDateAndTime,chn);
return true;
}
}
return super.dispatchKeyEvent(event);
}
and this is the dialog code
public class InfoDialog {
private int counter = 0;
private Dialog dialog;
public void showDialog(final Activity activity, final String DateTime, final ChannelList.Channel chn){
dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.info_dialog);
counter = 0;
setDialogText(chn,DateTime,counter);
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter>0) counter--;
setDialogText(chn,DateTime,counter);
return true;
}
}
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
if (keyEvent.getAction() == KeyEvent.ACTION_UP) {
if (counter<chn.infoText.size()-1) counter++;
setDialogText(chn,DateTime,counter);
return true;
}
}
dialog.dismiss();
return false;
}
});
dialog.show();
}
}
when I press any button, the dialog closes, even if the onKey has been invoked.
Did I miss something? how do I handle keypresses for my dialog only? (other dialogs may use same keys for different operation)
well, inside InfoDialog you are setting DialogInterface.OnKeyListener and on the bottom of onKey method you are calling dialog.dismiss(), try to remove this line... (and also return true for any case)
I'm showing up a Dialog on app-start, where you have to select a config. Since it is essential to select one config, I want to "disable" the back-button via a empty onBackPressed().
I got the following code in a DialogFragment:
public class ChangeConfigDialogFragment extends DialogFragment {
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.dialog_config_change)
.setItems(R.array.config_array,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// The 'which' argument contains the index
// position
// of the selected item
if (which == 0){
Initiation.BAUDRATE = 500;
Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
if (Initiation.getADK() == null){
Initiation.initiateCAN();
}
} else if (which == 1) {
Initiation.BAUDRATE = 600;
Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
if (Initiation.getADK() == null){
Initiation.initiateCAN();
}
} else if (which == 2) {
Initiation.BAUDRATE = 700;
Toast.makeText(getActivity(), "Baudrate at " + Initiation.BAUDRATE, Toast.LENGTH_LONG).show();
if (Initiation.getADK() == null){
Initiation.initiateCAN();
}
}
}
});
// Create the AlertDialog object and return it
return builder.create();
}
public void onBackPressed(){
Log.d(getTag(), "are you there?");
}
}
The problem is, that the onBackPressed() is never been called. Even the log message does not appear.
I tried to clean the project, but no success. Also tried to use a onKeyDown-method from some other topics here on SO. Does anyone has a clue how to solve this?
EDIT:
It works now. .setCancelable(false); worked, but I was to stupid to add it to the Dialog from which the Fragment was called. (instead added it to the builder
Thanks for all your help and time.
From your question it seems that while your dialog is open you need not to close dialog untill user can select any 1 open from dialog if i am not wrong then,so for this you need not to disable back key but you have to set this two properties for dialog.
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
OR
If you want to disable back key then use the below code,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event); }
OnbackkeyPressed Requires Api Level 5 Or higher.
#Override
public void onBackPressed() {
}
Use builder.setCancelable(false)
http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setCancelable(boolean)
I did not try this, but it might work if you set the OnKeyListeneron the builder like this:
builder.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
}
});
Try
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Handle the back button
if (keyCode == KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
or
dialog.setCancelable(false);
I am writing an application to animate popup window. It was working great with my code.
I want to close the popup window(i.e to slide-down it), when back button is pressed on device.
But I couldn't listen any one key from device. I used setOnKeyListener of that popup window, I didn't even get log from it.
My Code is given below:
popup_layout = layoutInflater.inflate(R.layout.popup_addchannel, null);
popupWindow = new PopupWindow(popup_layout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
subscribeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Log.d(TAG,
// "Button is clicked for animation.... Visibility is"
// + subscribeButton.getVisibility());
openMenu(view);
}
});
popup_layout.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.d(TAG, "on key button click called.........");
return false;
}
});
public void openMenu(View view) {
if (!flag) {
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.showAtLocation(view.findViewById(R.id.button1),
Gravity.CENTER, 0, 0);
popupWindow.setFocusable(true);
popupWindow.update();
flag = true;
} else {
popupWindow.dismiss();
popupWindow.setFocusable(false);
flag = false;
}
}
What is the problem behind this?
Shall i achieve my requirement?
Please, Guide me.
Thank you in Advance!
try this....
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,true);
...
popupWindow.getContentView().setFocusableInTouchMode(true);
popupMenu.getContentView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU &&
event.getRepeatCount() == 0 &&
event.getAction() == KeyEvent.ACTION_DOWN) {
// ... payload action here. e.g. popupMenu.dismiss();
return true;
}
return false;
}
});
The dialog has a property of cancelling the dialog on back press.
dialog.setCancelable(true);
EDIT: Check Qberticus answer on this link: Android popup window dismissal
and you can also see: Issue dismissing popup window
Yes you can use this
dialog_obj.setCancelable(true);
You should override the onKeyPressed() method of your activity as below.
#Override
public void onBackPressed() {
super.onBackPressed();
//your code to close the popup window.
popupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
popupWindow.showAtLocation(view.findViewById(R.id.button1),
Gravity.CENTER, 0, 0);
opupWindow.setFocusable(true);
popupWindow.update();
flag = true;
}
popupWindow.getContentView().setOnClickListener(new OnClickListener()) {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
Try this.
You can't listen a key press in popup window, but when you listen to system back key, you can override dismiss() method to intercept it.
How can I specifically override just the back button while within a dialog to finish the entire activity and not just the dialog.
Using setOnCancelListener and setOnDismissListener do not work because there are other times that I simply close the dialog without closing the whole activity behind it.
Edit
Thanks Shubayu that may work!
I was also able to access just the back button in a dialog through this function.
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
return false;
}
});
Override
public void onBackPressed ()
of the activity and put in the way you want the behavior in it. Also set a boolean from your dialog which you use inside onBackPressed() of the Activity. if the boolean is true, run the disabling part of the onBackPressed() code else don't.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK){
// your logic goes here
return true;
}
return super.onKeyDown(keyCode, event);
}
use the above code::
You can use : dialog.setOnCancelListener(.....)
first set dialog.setCancelable(true);
than you can place below code :
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
// add code backpress
}
});
I would like to be able to close the editpreference dialog (as shown here http://twitpic.com/18ttdp) by pressing the 'Done' button on the keyboard.
Currently, pressing 'Done' just dismisses the keyboard but leaves the dialog.
In other parts of my application I use code similar to the following to intercept the 'Done' key press and execute actions in my activity:
text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
//do stuff here
return true;
}
return false;
}
});
However, I am unsure of how to do achieve this same effect in my preference activity or layout xml.
Instead of adding the listener there, you should do something similar to this:
getDialog().setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
dialog.dismiss();
return true;
}
});
This code will dismiss the dialog when a key is pressed.
I had the same problem, this is how I solved it:
// edit text to get input
final EditText input = new EditText(this);
//input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE);
alert.setView(input);
// ok button
alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do stuff
}
});
For my needs, the input was a number (hence the commented out line) but if you want text use what's there.
Here is how I solved it:
final EditTextPreference namePref = (EditTextPreference) findPreference("name");
namePref.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_ACTION_DONE) {
namePref.onClick(null, DialogInterface.BUTTON_POSITIVE);
namePref.getDialog().dismiss();
return true;
}
return false;
}
});
But you may want to consider subclassing EditTextPreference instead, since this onClick call is a hack, and its implementation may change in the future.