I develop an android application and I need to prevent the user to close the application from navigation bar buttons .. there is another way to close the application.
I search a lot and didn't find any way to Permanently hide bottom navigation bar or at least stop all button control
Use this method to disable the Home button
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
If you meant to disable the back button on device, you could override
#Override
public void onBackPressed() {
super.onBackPressed(); //delete or comment this line
.......//do something you want
}
Related
I am using the navigation ui with single activity. In a particular fragment, I would like to perform an action when the arrow key on the toolbar is clicked. The toolbar currently works (takes user back to previous fragment), but I would want to perform an action before that is done (the action may even be to not go back to previous fragment).
The solution turned out to be using setNavigationOnClickListener on the toolbar instance, checking if the destination you are in matches the destination you wished to have that custom behavior, if yes, you perform the custom action there, if no, you navigate up normally. Here is the code snippet:
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (navController.getCurrentDestination().getId() == R.id.addPersonalNoteFragment) {
System.out.println("do what ever you want to do on the back arrow press here");
} else {
NavigationUI.navigateUp(navController, appBarConfiguration);
}
}
});
As per the design rule is it really a good idea to introduce a back button for Activity in Android?
I believe every device, now a days, or running Android OS has a hardware back button.
What is your suggestion on implementing software back button?
How can I enable it, if at all hardware back button support is not present?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//enable soft back button
ActionBar ab =getSupportActionBar();
ab.setDisplayHomeAsUpEnabled(true);
}
//handle click event
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==android.R.id.home){
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Hope it helps you!
There are new way for going back is home button of Toolbar.you can enable home button in your toolbar(actionbar) for go to back.
Lollipop introduced a new software back button in the Action Bar. You should read all docs about Material design
To navigate back from an activity, just call
finish()
Read some info on the back stack, as this is key to navigate using both activities and fragments
I have a Navigation Drawer (appcompat v7) in my app which is working perfectly fine.
Now I want to disable it, until the user buys an in-app-purchase to unlock additional functionality. So in my Activity.onCreate(), after initializing the drawer and populating it, I am calling this function:
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
This function is not doing anything. The drawer continues to open and close as normal after tapping the drawer carat in the actionbar. I tried calling this function in Activity.onResume() without any difference.
What is the correct way to use this function?
(I tried looking online for answers, but couldn't find anything which addresses my issue). Any help is appreciated, as I am stuck on this issue for quite sometime now.
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
is only disabling the opening drawer layout by swiping till you click navigation drawer icon
keep a boolean variable
write mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); in onStart() and also write below lines of code
#Override
public boolean onOptionsItemSelected(android.view.MenuItem item) {
if(!disabled)
{
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerLinearLayout)) {
mDrawerLayout.closeDrawer(mDrawerLinearLayout);
} else {
mDrawerLayout.openDrawer(mDrawerLinearLayout);
}
}
}
return super.onOptionsItemSelected(item);
}
this will work for sure
When you call setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) it locks opening and closing drawer only by swipes.
The drawer continues to open and close as normal after tapping the drawer carat in the action bar because your drawer will still respond to calls to openDrawer(int), closeDrawer(int) although a drawer is locked.
You need to add some logic in your action bar menu button listener and not to call openDrawer(int) when you don't want it to open.
Btw, it is okay to call setDrawerLockMode(int) in onСreate
There is a bug with DrawerLayout and used gravity. I have reported it here:
https://issuetracker.google.com/issues/136738274
I am using a Navigation Drawer in my Android app.
As this is a pattern that I find really hard to discover, my plan was to add a little message at the bottom of the screen until the user finally discover it and succesfully opened it with a swipe.
I know that I could use:
public void onDrawerOpened(View drawerView) {
// Stop telling the user, he know how it works
}
But this action is also triggered when opening it with the upper left button in the ActionBar.
Any suggestion to detect a succesfull swipe would be warmly welcome.
You need to listen to the state change callback:
#Override
public void onDrawerStateChanged(int newState) {
if (newState == DrawerLayout.STATE_DRAGGING) {
Log.v(TAG, "Drawer opened by dragging");
}
}
This state only occurs when user drags the drawer into view. Tapping on home icon does not induce this - which is exactly what you want.
Without extending DrawerLayout you could use a combination of the DrawerListener and a boolean flag for Home button press.
Implement DrawerListener with onDrawerSlide method. Inside this method you can check the Home Button flag and if this is false and the drawer is sliding open then you know this was action through the slide.
Bit messy.
Other than that override DrawerLayout and override the onTouchEvent and catch move events.
I have overwritten the functions of the back and the homebutton, to prevent users to use the phone as a phone. We´re giving phones to clients (students) and we don´t want them to abuse the phones in a certain mode.
The thing is, the HOME button is disabled, but when I first open the option menu and then don´t select an option, but press the HOME button, the HOME still works as normal.
Is there anyway to overwrite this and use a boolean, sometimes yes, sometimes no..
What I got now is this:
#Override
public void onBackPressed() {
if (buttonslocked) {
//backbutton blocked!
} else {
super.onBackPressed();
}
}
#Override
public void onAttachedToWindow()
{ //HOMEBUTTON
if(buttonslocked)
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
else
{
this.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION);
super.onAttachedToWindow();
}
}
You cannot override the home button functionality.
Just as a clarification, although you may see some hacks that might provide what you are looking for.. This is unsupported and discouraged!