Navigation back button on Android is not working - java

I have wrote this code to enable the navigation back button. Its appearing on run time but after clicking on it is not doing anything. Can someone tell me please what I'm missing?
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
Thank you!

You should override onOptionsItemSelected method in your activity.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {
finish()
true
}
else -> super.onOptionsItemSelected(item)
}
}
Also if i'm not mistaken you should add this to your action bar to:
supportActionBar?.setDisplayShowHomeEnabled(true)

Related

How to remove highlight from one specific icon in a Botton Navigation Bar in Android

I have a Bottom Navigation Bar in Android with 4 icons. At the moment, when I click on an icon it gets highlighted (change of color) until another button is pressed on the Bottom Navigation Bar. While this is good for 3 of the 4 icons, I also have a "Back" button where this should not be the case. So if the Back button is pressed, it should either not be highlited at all, or it should just be highlighted for a very short period of time. Can you tell me how to do this in the Java code?
Here is the code that defines what happens if a button is pressed on the Botton Navigation Bar inside the onCreate method of the main acticity:
binding.bottomNavigation.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId()== R.id.BottomNavigation_Back) {
item.setChecked(false);
navController.navigateUp();
}
if (item.getItemId()== R.id.BottomNavigation_Language) {
navController.navigate(R.id.FR_LanguageSelection);
}
if (item.getItemId()== R.id.BottomNavigation_Info) {
navController.navigate(R.id.FR_Info);
}
if (item.getItemId()== R.id.BottomNavigation_Menu) {
navController.navigate(R.id.FR_Menu);
}
return true;
}
});
The relevant chase is the first one if (item.getItemId()== R.id.BottomNavigation_Back) . I tried to use item.setChecked(false); but this does not work (as you can see in the screenshot). The Back Icon is still highlighted until another button is pressed. Any idea how to do this?
Reminder: Does anybody have an idea how to do this or why the command item.setChecked(false); does not work? Strangely when using item.setChecked(true); the behaviour is exactly the same. Is there a way how to disable the highlight from this specific button after is has been pressed?
You could access the menu item which is being selected like this and not set it checkable and rest should work as is.
MenuItem menuItem =binding.navView.getMenu().findItem(R.id.navigation_home);
menuItem.setCheckable(false);
You can choose which item should be selected by default by (Replace R.id.navigation_dashboard with your relevant id)
binding.navView.setSelectedItemId(R.id.navigation_dashboard);
This will only change the selection now to set the correct fragment according to the id do this.
navController.navigate(R.id.navigation_dashboard);

How to partially disable click on home button in Android toolbar?

I have implemented onOptionsItemSelected to have control over the home button:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
if (mode) {
reset()
}
}
}
return super.onOptionsItemSelected(item)
}
Now, when I press home, I go back to the previous fragment. What I need is, if the mode is true, when I click on home, to trigger ONLY the reset() function without going back to the previous fragment. If it's false, simply go back. How can I achieve this?
You should return true to say the parent that the click on the menu item is consumed.
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
if (mode) {
reset()
return true
}
}
}
return super.onOptionsItemSelected(item)
}

Hide virtual buttons after they are shown again

I found out, that I can hide the virtual buttons of an android phone with:
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
But once the user makes them visible by swiping from the edge, they stay visible.
How can I hide them again after a moment ?
Thanks in advance!
Edit:
I start a new activity.
When this new activity is started I call this in the onCreate-Method to hide the actionbar and the virtual buttons:
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
Edit:
#Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
} else {
}
}
This is what your doing:
https://developer.android.com/training/system-ui/navigation.html
This is what you also want to be doing:
https://developer.android.com/training/system-ui/visibility.html
So basically, when you hide an item, you need to listen (check) whether it's been changed (Turned On/Off) and from there re-enable it. I would copy/paste the code, but it's in those links.

android, how can i close secondary sliding menu programatically

i'm using sliding menu from https://github.com/jfeinstein10/SlidingMenu
and i know how to open and close left menu by toggle
and to show right menu
getSlidingMenu().showSecondaryMenu()
but how to close it programatically
You can close both of the menus with showContent(). hope this helps.
if (getSlidingMenu().isSecondaryMenuShowing())
{
getSlidingMenu().showContent();
}
The method called toggle() in SlidingMenu will close the menu that is currently showing regardless if it is the right or left menu.
I have implemented my "back button" logic like this ;
#Override
public void onBackPressed() {
if (mSlidingMenu.isSecondaryMenuShowing() || mSlidingMenu.isMenuShowing()) {
mSlidingMenu.toggle();
} else {
super.onBackPressed();
}
}
This closes any visible menu.

Hiding title bar / notification bar when device is oriented to landscape

I want my title bar to be hidden when I turn my device to landscape. I have seen the XML option to hide the title bar, and the following example of doing it programmatically:
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
BUT, I am using configChanges parameter for orientation and screenSize, so my activity is not re-created again when it is orienting to landscape (I'm doing this for some reasons). So I cannot use the above methods as these calls need to be made before setContentView().
So any ideas? Thank you.
was searching for an answer, ended up here, put the pieces together and here they are:
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
Assuming you have defined configChangesfor the Activity in the manifest then you can achieve your issue overriding onConfigurationChanged method:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getActionBar().hide();
} else {
getActionBar().show();
}
}
You will have to use getSupportActionBar() instead of getActionBar() if using the support library.
first check your device orientation by using following code
The current configuration, as used to determine which resources to retrieve etc, as available from the Resources' Configuration object as:
getResources().getConfiguration().orientation
Then do the necessary coding related to hide title bar and notification bar.
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getActionBar().hide();
} else {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getActionBar().show();
}
}
You can use this I have tried by my self.
In your "AndroidManifest.xml", in the activity tag you can specify "NoTitleBar" in the theme property so that it will always hide the title:
<activity
android:name="com.my_package.MyActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:configChanges="navigation|orientation|screenLayout|layoutDirection">

Categories

Resources