I have a navigation drawer & on click of certain items of navigation drawer, i am opening Fragment.These fragments does not have an activity. I have an edittext in my fragment,on click of it i am setting some view to invisible and on click of back button of keyboard i want to again show the view which were invisible on click of edittext.
My fragment does not have an activity so i cant implement onBackPressed. Please help
You can apply a listener in your fragment view doing this:
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
// handle back button's click listener
return true;
}
return false;
}
});
Referenced answer
Related
I'm displaying a layout over all the other apps with ApplicationOverlay and the WindowManager.
When I click the back button. The OnBackPressed() override of my activity doesn't fire because of the overlay.
How can I detect and cancel the back button from a Service on android so that when I press back I can make my application change its layout?
If you have the overlay attached to the WindowsManager, you just need to add a listener to the view:
view.setFocusableInTouchMode(true);
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
stopSelf();
return true;
}
return false;
}
});
When you create WindowManagerLayoutParams add the below flags:
layoutParams.Flags = WindowManagerFlags.NotTouchModal;
layoutParams.Flags = WindowManagerFlags.NotFocusable;
then you could fire the OnBackPressed() method in your activity.
My AppCompatActivity axml uses the Android.Support.v7.Widget.Toolbar and the Activity sets the toolbar with SetSupportActionBar(toolbar);. Everything works as expected, however I'd like to override the OnKeyDown event of the whole activity to prevent a hardware button click. It appears that SupportActionBar (just an ActionBar) consumes the event for the hardware menu button and so the OnKeyDown of the activity never catches the menu button click.
I've tried overriding the Toolbar view and its OnKeyDown event, but the SupportActionBar consumes this event too.
My work around was to add a menu item that looks like the Overflow Menu icon and opens a popup in OnOptionsItemSelected. However, this is not ideal.
You can override onKeyDown / onKeyUp handlers in your Activity.
#Override
public boolean onKeyDown(int keyCode, final KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_MENU ) {
return true; // Ignore the keypress
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, final KeyEvent event) {
if( keyCode == KeyEvent.KEYCODE_MENU )
return true; // Ignore the keypress
}
return super.onKeyUp(keyCode, event);
}
In default, when the drawer is showing and you press the BackButton it automatically close.
but I want when it's showing and user clicks on BackButton, close the app.
I tried these solution but didn't work :
Navigation drawer, handling the back button to go to previous fragments
Backbutton press from android navigation drawer
onBackPressed will not be called on drawer open or lets say if your keyboard is open then also your back key changes to down arrow. So when you press that down key your keyboard hides and your onBackPressed will not be called still.
Your app will be closed when you press the back button again. Same is the case with keyboard.
So first close the drawer, then press back button again, and then your app will finish
First override back pressed of your activity and then can check if drawer is open
mDrawerLayout.isDrawerOpen(GravityCompat.START);
If this return true simply close your app.
Important : Check your drawer gravity.
Try registering you fragment to back pressed like this:
public void registerBackButton()
{
if (getView() != null)
{
getView().setFocusableInTouchMode(true);
getView().requestFocus();
final BaseFragment frag = this;
getView().setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() != KeyEvent.ACTION_DOWN)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (isVisible())
{
return frag.onFragmentBackPressed();
}
}
}
return false;
}
});
}
}
Now in onFragmentBackPressed check if drawer is opened or not as mentioned above.
i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.
I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.
Any advices?
You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)
I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
//add your code here
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You may try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your back key press logic
}
return true;
}
Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.
I am new to android, I have a small doubt regarding how to handle the hardware Keyboard and if I click the search button in any part of my application it should be handled means I need to pass the intent of search activity?
How I can reach this goal.
Try this,
#Override
public boolean onSearchRequested() {
// your stuff here
return false;
}
It will also trigger onKeyDown with a keyCode of KeyEvent.KEYCODE_SEARCH before calling onSearchRequested as stated above
Add a listener for your EditText to listen on the Search button as following:
myEditText.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_ENTER) {
//Do some stuff
}
//Leave the return value false to hide the SoftKeyboard if it is shown
return false;
}
});
For me, it happened before that the softkey search button is detected as Enter button not search. That's why I'm ORing them