In my app I have a menu in my action bar. I need to make that
when a set of users is logged in the first, the menu will appear.
When the second set of users logs in, the second menu will appear.
So this is a beginning I have tried that for one users that I know its id like this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main_menu; this adds items to the action bar if it is present.
currentuser = FirebaseAuth.getInstance().getUid();
if (currentuser.equals("b3C28u0hX3WAIEPvtLnCMcUoUMn1")) {
getMenuInflater().inflate(R.menu.menu2, menu);
} else {
getMenuInflater().inflate(R.menu.menu1, menu);
}
return true;
}
know i need it for a dynamic set of users not just one
You should define properties in your users model that say if it belongs to set1 or set2.
Most probably, the basic Firebase auth is not enough, so you may have to define a FB realtime database, in which you have a user node, and users inside, matching the id of Firebase Authenticator, and which contains that supplementary information.
It's quite a bit more work to add support for the realtime DB, however this has proven to work well in one of my app.
Related
I am developing an Android app. Firstly, let me tell you that I am not professional. What I am doing now is I am adding submenu to menu depending on a condition. But I need to do it very often in my app. But my problem is I added a submenu to the menu as first time.
But second time when I update menu depending on condition, existing submenu is not removed and new submenu is appended to navigation drawer. How can I remove submenu that is programmatically added to menu? Why my code is not removing it?
Here is my code
public void updateAuthUI()
{
isLoggedIn = tempStorage.getBoolean(getResources().getString(R.string.pref_is_logged_in),false);
Menu menu = leftDrawer.getMenu();
menu.removeItem(getResources().getInteger(R.integer.logout_item_id));
menu.removeItem(getResources().getInteger(R.integer.login_item_id));
menu.removeItem(getResources().getInteger(R.integer.register_item_id));
SubMenu authSubMenu = menu.addSubMenu("Auth");
if(isLoggedIn)
{
authSubMenu.add(1,getResources().getInteger(R.integer.logout_item_id),99,"Sign out");
}
else{
authSubMenu.add(1,getResources().getInteger(R.integer.register_item_id),97,"Register");
authSubMenu.add(1,getResources().getInteger(R.integer.login_item_id),98,"Sign in").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
openLoginActivity();
item.setChecked(true);
return true;
}
});
}
}
Here is the screenshot of my problem
As you can see Auth submenu is appended without removing existing one.
Try
authSubMenu.clear();
before your first
authSubMenu.add();
I just used SubMenu.clear() in an Android app where I was using a third-party library, and I needed to clear out an unwanted submenu from the action bar. (I actually wanted to remove the submenu completely, and this was the only way I could find to do it. It seemed to work.)
That's different from your situation, where authSubMenu is a menu you just added via menu.addSubMenu("Auth"), so you would expect it to be empty. But, as you've seen, it apparently isn't empty: rather, addSubMenu("Auth") returns the existing submenu of that title. (I can't find documentation to that effect; I'm just going by the results you've reported.)
If that really is the case, as it appears to be, then authSubMenu.clear() will remove all existing items from the submenu.
As #slymm said in a comment you can remove all menu and submenu items using
navigationView.getMenu().clear();
This can be used to remove submenu (and menu elements) and then recreate them with the new required items
In a project I have 3 Actors and 5 Use cases.I want to regulate Actors access to entities based on their roles(RBAC).Also all Actors see the same UI,but some UI controls are disabled for each actor according to his/her Role.I can use If statements to decide specific control must be enabled for current Actor or disabled? For example:
If (User.Roles(...))
{
btnEditOrder.enabled = false;
}
That is possible,but UI is complex and each layout has many UI controls.So managing all these possible options and hard-code that logic in the application seems daunting.Specially, number of use cases,actors and their permissions may change later.Do you know how I can avoid hard-code such logic and have a good design?
IMHO it would be good to create separate menu.xml files for each role.
Let's say you make separate menu.xml files namely:
options_menu_manager.xml
options_menu_engineer.xml
options_menu_director.xml
You will need to make a Helper class around it. I am assuming you have the role already in the memory, you can do something like:
class Helper{
public static int getOptionsMenu(){
switch(Global.role){
case Constants.MANAGER:
return R.menu.options_menu_manager.xml;
...
...
}
}
}
and wherever you need to get the menus, you can use a helper function instead of using a R.menu.* file.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(Helper.getOptionsMenu(), menu);
return true;
}
Although, this is just a way of getting ROLE based menu. You should still have a good background validation of the authorization of all the performed actions. For that also you can create a helper that matches the current user's role against the allowed actions to see if he is allowed to do what he is doing or not.
I'm trying to implement search into my app and have trouble understanding the difference between these two methods. Why is it that we have to define a search configuration (described here: http://developer.android.com/guide/topics/search/search-dialog.html), add a bunch of meta-data to the manifest, and have a separate activity just for search, when we can treat it like any other TextView, grab it's text, and query the database with that? Is there any advantage to the former method, or any disadvantage to the latter? In my mind directly getting the text and doing my own stuff off of that seems a lot easier, especially when after dealing with the search interface I'll still need to get the query from the intent and perform the actual search with that. Am I missing something here? Thanks!
Here's what I mean by directly getting the search query (without using the search interface):
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
MenuItem search = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) search.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
// Do stuff with query
return true;
}
#Override
public boolean onQueryTextChange(String newText) {
// Do stuff with newText
return true;
}
});
}
You have few advantages in working with the "standard" searching feature:
The search will look as the user is use to have is "search" boxes on any device. If you design something this will be for all devices. I love to be in the "standard" because in this way for the users is very simple to use it and compatibility over devices is much wider
Activity Life Cycle : see here. This a huge advantage.
If the Look & Feel fit's with your design you have a lot of other features that are available and checked and working and almost bugles.
Daniel
Beginner here, targetting sdk v14 and v17 for my learning...no need for older support.
I am using the master/detail template and trying to get an action menu (for SEARCH) to show up both in phone and tablet view. Actually I can get it to work, but I have to duplicate up my code in both ItemDetailActivity.java and ItemListActivity.java
These are the methods that I have to have in both for SEARCH to work:
public boolean onQueryTextChange(String newText) {
public boolean onQueryTextSubmit (String query) {
public boolean onClose () {
I only want to search the "detail", not the "list".
So my question: is there a way to associate the action bar with only the list fragment? That way I can keep the search functions in 1 file.
Thanks!
I'll go ahead and answer what I (think) I know as I don't want to leave this question open.
From tracing in the debugger, it looks to me like the phone activity and the tablet activity are separate and if you want to hook up an actionmenu, you have to hook it up to both separately.
i need to be able to change the options menu (the one that is shown upon pressing the menu button) on android , so that on one case (for example upon a button being pressed) , it will use a specific menu resource (XML file as in /res/menu/... ) for the menu , and on another case , use a different XML file.
so far i've seen only examples of doing it without xml (example here and here) , and they worked fine , but i want to be able to change the entire menu on some cases.
i've tried to modify the solutions i've found , but none of my trials worked.
if possible , i would prefer to re-create the menu only if the it needs to be updated with a menu resource that is different from the current one.
please help me.
If you want to change the Options Menu any time after it's first created, you must override the onPrepareOptionsMenu() method.
public boolean onPrepareOptionsMenu (Menu menu) {
menu.clear();
if (CASE_1 == 0) {
CASE_1 = 1;
getMenuInflater().inflate(R.menu.secondmenu, menu);
}
else {
CASE_1 = 0;
getMenuInflater().inflate(R.menu.firstmenu, menu);
}
return super.onPrepareOptionsMenu(menu);
}
where CASE_1 refer to the which menu you want to display depending on your requirement.