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.
Related
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);
Do i necessairly need to change my recyclerView to expandableRecyclerView for doing animation of expand + animation of arrow drop down -> arrow up? Is there any way to implement simple slide down animation?
On button drop down arrow click i change visibility of textView and imageView from GONE to VISIBLE (also changing src of arrow button in code)
From:
To:
Here is some code just in case
taskViewHolder.showDesc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(taskViewHolder.description.getVisibility()==View.GONE){
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_up_arrow));
taskViewHolder.imgDesc.setVisibility(View.VISIBLE);
taskViewHolder.description.setText(task.getDescription());
taskViewHolder.description.setVisibility(View.VISIBLE);
}
else {
taskViewHolder.showDesc.setImageDrawable(resources.getDrawable(R.drawable.drop_down_arrow));
taskViewHolder.description.setVisibility(View.GONE);
taskViewHolder.imgDesc.setVisibility(View.GONE);
}
}
});
Have a Look on my old project, I had to do something like you have told:
https://bitbucket.org/MauzerTheCat/music-example-app/src/master/
I'm trying to make custom lock screen. So, there I need not to allow user to press Home Button. In the beginning I write
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lock_screen);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
Then I override OnKeyDown
#Override
public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)||(keyCode == KeyEvent.KEYCODE_POWER)||(keyCode == KeyEvent.KEYCODE_VOLUME_UP)||(keyCode == KeyEvent.KEYCODE_CAMERA)) {
//this is where I can do my stuff
return true; //because I handled the event
}
if((keyCode == KeyEvent.KEYCODE_HOME)){
return true;
}
return false;
}
Here I override onAttacheToWindow
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
But it's giving me error IllegalArgumentException: Window type can not be changed after the window is added. Where is my mistake?
How can I handle Home Button?
There is no "good" way to handle the home button to "do nothing". You really only have two options which may or may not be good enough for you:
Create your own home launcher. Users must select your app as their home launcher.
You could also create a Service where you add a view to your window and it gets plastered on top of everything else. See my previous answer here with "displaying" a webview in a Service: Android: Using WebView outside an Activity context. You will need to modify params to fill the screen and also fix WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE to use the right flags to allow clicks. This won't actually block the home button but it will basically disable it's functionality. Lastly, this might not work on Android 5.0+ as the Status bar's dropdown may overlay your window.
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.
HomeActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Button btnopen = (Button)findViewById(R.id.btnWindowAnimation);
btnopen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent slideactivity = new Intent(HomeActivity.this, CartActivity.class);
Bundle bndlanimation =
ActivityOptions.makeCustomAnimation(getApplicationContext(), R.anim.animation,R.anim.animation2).toBundle();
startActivity(slideactivity, bndlanimation);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.layout.home, menu);
return true;
}
HomeActivity has a button and it will help to slide the 1st screen and bring the second screen.
animation.xml
android:fromXDelta="100%p"
android:toXDelta="25%p"
android:duration="5000"
This animation code is helping me to slide my screen after pressing the button. As shown in this video
https://www.youtube.com/watch?v=EzgGGWpRES0
I want to stop the new screen at the middle without covering the 1st screen completely.
Any suggestions? I have used another activity called cart( i haven't used fragments here)
Sounds like you want a NavigationDrawer.
You can find out more information on the Android developer site:
https://developer.android.com/design/patterns/navigation-drawer.html
https://developer.android.com/training/implementing-navigation/nav-drawer.htmlA
Your Requirement is to partially Display an Overlapping Activity on an Existing Activity. This is Possible with Sliding Menu Library (Which does not mean to technically create a floating Activity, but to create a Floating View integrated within the Sliding Menu that will be displayed partially on top on an Activity).
In case, if you are regarding this as some sort of Default Menu then please take a look at this Image
If this is your actual Requirement, then here are the details for you.
Sliding Menu Library is an OpenSource Library, you can use it for displaying a partially visible Sliding Menu on top of an Activity.
Here is the link of Sliding Menu Lib :
https://github.com/jfeinstein10/SlidingMenu
You can navigate to the example directory present in this Project for the demonstration of it's Usage.
I hope thie helps :)