Back press event of device to dismiss a window - android

I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BACK) {
Log.d("LOG", "back button is pressed");
}
return true;
}
});
But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?

Maintain global reference for Window and override onBackPressed()
Try this:
#Override
public void onBackPressed() {
if (view != null && view.isShowing()) {
view.dismiss();
} else {
super.onBackPressed();
}
}

Related

Save data on home button press

I'm making app that counts user's income and outcome. And recently I found out that if user has pressed home button from calculator(I use basic calculator, by android) than all data that he/she didn't save will be erased. How can I notify users about this so they could save all unsaved data? I want to show dialog when this happens but I don't know how to detect home button press. May be somebody had this issue too and could help me?
Thank you.
On press of home button the activity will call onPause() method you can override this method to write actions.Also you can use
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
to restore your values.
You can override onKeyDown as follows
public boolean onKeyDown( int keyCode, KeyEvent event) {
if (keyCode == KeyEvent .KEYCODE_HOME) {
//Dialog here to notify user
return true ;
}else{
return super .onKeyDown(keyCode, event );
}
}
Try this.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//YOUR CODE
}
});

Avoid Alert Dialog getting closed when back button pressed?

First thing I want to say, this was done by seeing a tutorial. Here is the Custom Alert Dialog activity part I am calling from a broadcast receiver. The only problem is the back button click. Once the Alert dialog activity got started, when I press the back button it is getting closed.
public class AlertDialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setFinishOnTouchOutside(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.activity_inmsgdialog);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
}
I have tried onBackPressed and I'm able to see the toast message but the activity is getting closed.
See here:
#Override
public void onBackPressed()
{
super.onBackPressed(); //Remove this line
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
Do not call super.onBackPressed(); code if you want to disable back button for activity. So remove this line. Hope it helps.
You can use the below option to handle back button press
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your code
return true;
} else {
}
}
Don't propagate the event and you should be good.
#Override
public void onBackPressed()
{
//don't call super
}

onKey event for editText not firing

I'm having a really hard time getting the OnKey event to trigger on my EditText view. I've been Googling for over an hour now and it seems everyone says all you have to do is add a OnKeyListener to the view but it's not triggering it. I tried adding it to the entire Activity and also tried adding it to the view itself but neither way is triggering the function.
My code:
XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/interests_editText" />
Java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_post);
interests_editText = (EditText) findViewById(R.id.interests_editText);
interests_editText.setOnKeyListener(new OnKeyListener(){
public boolean onKey( View view, int keyCode, KeyEvent event){
Log.i("DEBUG","TeST");
if (keyCode == KeyEvent.KEYCODE_ENTER) {
return true;
}
else {
return false;
}
}
}
);
Update:
My final goal is to get it to recognize when you press space while in that editText view. I added to the XML
android:inputType="text"
and then changed the code to
public boolean onKey( View view, int keyCode, KeyEvent event){
Log.i("DEBUG","Outside");
if (keyCode == KeyEvent.KEYCODE_SPACE) {
Log.i("DEBUG","Space");
return true;
}
else {
return false;
}
Now the keyboard shows a Next button that when pressed logs "Outside" but it doesn't log "Space"
Do you wanna capture Enter event? If so, try setOnEditorActionListener
For your Update Info: capture space key, try addTextChangedListener
Following up on #BNK's answer I got it to work using the following code:
interests_editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
String str = s.toString();
if(str.substring(str.length()-1, str.length()).equals(" "))
Log.i("DEBUG","Space pressed");
else if(str.substring(str.length()-1, str.length()).equals("\n"))
Log.i("DEBUG", "Enter pressed");
else
Log.i("DEBUG", "Pressed : " +str.substring(str.length()-1, str.length()) );
}}

how to listen key when popup is opened

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.

Override Dialog's OnBackPressed

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
}
});

Categories

Resources