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)
Related
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.
In my android app I have the following code in the OnCreate function:
txtUsername.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View arg0, int arg1, KeyEvent arg2) {
// TODO Auto-generated method stub
String Username = txtUsername.getText().toString();
if (arg1 == KeyEvent.KEYCODE_ENTER) {
DontShowDialog = false;
if ((Username.toLowerCase().endsWith("blabla.com") == false && Username.toLowerCase().endsWith("blabla-bla.nl") == false) || validateEmail(Username) == false) {
final Dialog dialog = new Dialog(arg0.getContext());
dialog.setContentView(R.layout.startdialog);
dialog.setTitle("Warning 1");
Button btOk = (Button) dialog.findViewById(R.id.btOk);
btOk.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DontShowDialog = true;
dialog.dismiss();
return;
}
});
if (DontShowDialog == false) {
dialog.show();
}
return false;
}
}
txtUsername.clearFocus();
txtPassword.setNextFocusDownId(txtPassword.getId());
return false;
}
});
When the user hits the NEXT button and the username is not right, a dialog is shown which can be canceled by the OK button.
But.... after hitting the OK button, the dialog is shown again... I don't want that.
Why is that happening?
rg,
Eric
By returning false in your onKey() event, you're telling Android that the KeyEvent was not consumed - so my guess is that changing the return statement to true might fix the problem, because Android would know the event was consumed, and it would not re-enter the onKey() method. If you could try that out and share the results that would be great!
Solved it.
moved the line
DontShowDialog = true;
from the OnClick part
Changed the if-then below the onClick to:
if (DontShowDialog == false) {
dialog.show();
DontShowDialog = true;
return false;
}
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
}
I need to dismiss system alert window on back pressed and home button event.I have tried with onKeyEvent but in vain. As we can't capture the back pressed event in a service, how to achieve this?
Since it's a service that hosting an overlay window, It's a bit tricky solution but it is possible.
You should handle these 2 cases separately (overriding home button press, and back button press).
1. Overriding home button press:
Create this HomeWatcher class which contains a BroadcastReceiver that will notify when home button was pressed. Register this receiver only when your window comes up.
Android: associate a method to home button of smartphone
Inside your service onCreate method use this:
HomeWatcher mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new OnHomePressedListener() {
#Override
public void onHomePressed() {
yourWindow.hide() //means: windowManager.removeView(view);
}
#Override
public void onHomeLongPressed() {
}
});
mHomeWatcher.startWatch();
2. Overriding back button press:
The idea is creating an empty layout as a data member of your window class,
and attach your view to it (even if its an inflated XML layout).
For example, this is gonna be your window class:
public class MyWindow
{
private WindowManager windowManager;
private WindowManager.LayoutParams params;
private View view;
// Add this empty layout:
private MyLayout myLayout;
public MyWindow()
{
windowManager = (WindowManager) context.getSystemService(context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.your_original_window_layout, null);
// Add your original view to the new empty layout:
myLayout = new MyLayout(this);
myLayout.addView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
// And show this layout instead of your original view:
public void show()
{
windowManager.addView(myLayout, params);
}
public void hide()
{
windowManager.removeView(myLayout);
}
}
And now create the MyLayout class to override the back button press:
public class MyLayout extends LinearLayout
{
private MyWindow myWindow;
public MyLayout(MyWindow myWindow)
{
super(myWindow.context);
this.myWindow = myWindow;
}
#Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK)
{
if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0)
{
getKeyDispatcherState().startTracking(event, this);
return true;
}
else if (event.getAction() == KeyEvent.ACTION_UP)
{
getKeyDispatcherState().handleUpEvent(event);
if (event.isTracking() && !event.isCanceled())
{
// dismiss your window:
myWindow.hide();
return true;
}
}
}
return super.dispatchKeyEvent(event);
}
}
I know it's a bit complicated as I said since it's a system alert window hosted by a service, BUT it's working. I have the same issue as well and it has been solved exactly like that.
Good luck.
use below method to handle back button pressed.
public void onBackPressed()
{
super.onBackPressed();
}
You need to overwrite the onBackPressed method.
#Override
public void onBackPressed() {
super.onBackPressed(); // remove this if u want to handle this event
}
Use the code below
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exitByBackKey();
//moveTaskToBack(false);
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void exitByBackKey() {
AlertDialog alertbox = new AlertDialog.Builder(this)
.setMessage("Do you want to exit application?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
finish();
//close();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
}
})
.show();
}
#Override
public void onBackPressed()
{
super.onBackPressed();
}
Declare this on your activity. super.OnBackPressed automatically calls back method in android. it will surely cancel your dialog.
in addition, your dialog must look like this.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder1.setMessage("TEST DIALOG.\n");
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(MainActivity.this, "This Is test Dialog", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
or you can set Negative button..
Hope this helps!
Define a custom layout and override dispatchKeyEvent, for example:
public class CustomSystemAlertWindow extends FrameLayout {
public static final String TAG = "CustomSystemAlertWindow";
private WeakReference<Context> mContext;
public CustomSystemAlertWindow(Context context) {
super(context);
mContext = new WeakReference<Context>(context);
// Set a background color to identify the view on the screen
this.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event != null && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.d(TAG, "back button pressed");
if (mContext != null && mContext.get() != null) {
WindowManager wm = (WindowManager) mContext.get().getSystemService(Context.WINDOW_SERVICE);
wm.removeView(this);
}
return true;
}
return super.dispatchKeyEvent(event);
}
}
Then add the view with this code:
CustomSystemAlertWindow customSystemAlertWindow = new CustomSystemAlertWindow(context);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
wm.addView(customSystemAlertWindow, params);
When you press the back button the view will dismiss.
Show the Alert window through the Activity so you can detect it.
Implement the code to detect easily Back Button or Home Button pressed.
public class alertPopup extends Activity {
Context context;
final AlertDialog alertDialog;
String TAG = "your Activity Name"
boolean homePressed = false; // to detect the Homebutton pressed
#Override
public void onCreate(Bundle savedInstanceState) {
AlertDialog.Builder builder = newAlertDialog.Builder(YourActivity.this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("AlertDialog Title");
..........
....... // Build ur AlertDialog
alertDialog= builder.create();
alertDialog.show();
//to detect Alert Dialog cancel when user touches outside the Dialog prompt
alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
Log.v(TAG,"Alert Dialog cancelled when user touches outside the Dialog prompt")
}
});
}
#Override
public void onBackPressed()
{
Log.v(TAG,"Back Button Pressed");
super.onBackPressed();
alertDialog.dismiss(); //dismiss the alertDialog
alertPopup.this.finish(); // Destroy the current activity
homePressed = false;
}
#Override
public void onResume() {
super.onResume();
homePressed = true; // default: other wise onBackPressed will set it to false
}
#Override
public void onPause() {
super.onPause();
if(homePressed) {
alertDialog.dismiss(); //dismiss the alertDialog
alertPopup.this.finish(); // Destroy the current activity
Log.v(TAG, "Home Button Pressed"); }
}
public void onDestroy(){
super.onDestroy();
}
}
Note:
Add this Permission in Android Manifest to show the alert Window .
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
Happy Coding :)
I understand that you are using the <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> permission for showing a floating view.
Using a floating view you can intercept the back button press, but the home button press cannot be intercepted (android won't let you primarily because of security reasons).
To intercept the back button press you need to add a wrapper when you inflate your floating view.
Your wrapper should look like this:
// Wrapper for intercepting System/Hardware key events
ViewGroup wrapper = new FrameLayout(this) {
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
hideAddNotesFloatingView();
return true;
}
return super.dispatchKeyEvent(event);
}
};
Then you add it to your floating view as a root:
mAddNoteFloatingView = mInflater.inflate(R.layout.floating_add_note, wrapper);
My complete code looks like this:
private void addFloatingView() {
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
0,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.LEFT;
params.x = 0;
params.y = 0;
// Wrapper for intercepting System/Hardware key events
FrameLayout wrapper = new FrameLayout(this) {
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
// Add your code for handling the back button press
return true; // Return true means that the event was handled
}
return super.dispatchKeyEvent(event);
}
};
mAddNoteFloatingView = mInflater.inflate(R.layout.floating_view, wrapper);
mWindowManager.addView(mAddNoteFloatingView, params);
}
It's simple. Follow these steps:
Create a view like Relative Layout, Linear Layout or Frame Layout Dynamically. 2. Override the dispatchKeyEvent while creating the view.
Add your original view into this dynamically created view with addView() method.
Add the dynamically created view to your Window Manager or Alert Dialog whichever you want.
In addition to #Eliran Kuta's solution, this is more simple answer for Back button.
val view = getAlertView()
val windowParam = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT, // whatever
WindowManager.LayoutParams.WRAP_CONTENT, // whatever
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // use WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY before Oreo
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, // whatever
PixelFormat.TRANSLUCENT // whatever
)
view.isFocusableInTouchMode = true
view.setOnKeyListener { view, keyCode, event ->
when (keyCode) {
KeyEvent.KEYCODE_BACK -> {
// do your work here
true
}
else -> false
}
}
val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
windowManager.addView(view, windowParam)