Is it possible to have nested context menus? I am working on an app which needs to provide the users with various functionality, and after much thought I came to the conclusion that using a context menu would be quite advantageous (would allow me to clean up my screen by getting rid of a few spinners etc)
However I was wondering if its even possible to have a context menu pop up, and then on the item select present the user with even more choices.
Something along these lines:
Context Menu 1
-> Change Font Color (on select generate context menu 2)
->Red
->Green
-> Change Background Color (on select generate context menu 3)
->Red
->Green
Would something like this be possible?
What IDE are you using? Do you what to do this in xml or Java? If you are using eclipse you could just create a menu in your "menu" folder which contains an xml file and then use their "manager" to create a sub menu.
Here's an example menu named game_menu.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/new_game"
android:icon="#drawable/ic_new_game"
android:title="#string/new_game"
android:showAsAction="ifRoom"/>
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/help" />
</menu>
To create submenu you have to "nest <menu> elements"
http://developer.android.com/guide/topics/ui/menus.html
Go to Defining a Menu in XML
Related
I have a Widget for my Android App. On this Widget there is a button. The user can freely choose the color of the button. So I also have to set the colors for the different button states dynamically.
Normally, I would do something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorButtonRedTransparentPressed"
android:state_pressed="true" />
<item android:drawable="#color/colorButtonRedTransparentPressed"
android:state_focused="true" />
<item android:drawable="#color/colorButtonRedTransparentNotPressed" />
</selector>
As mentioned, I have to do this dynamically because the user is free to choose the color:
StateListDrawable drawable = new StateListDrawable();
drawable.addState(new int[] {}, new ColorDrawable(Color.parseColor(customButton.getColorHex())));
drawable.addState(new int[] { android.R.attr.state_pressed }, new ColorDrawable(Color.parseColor("#0000")));
The challenge:
The button is on a Widget. So i cannot just simply call:
myButton.setBackground(drawable);
I need to call something like this:
remoteView.setInt(R.id.button_fixedvalue, "setBackground", drawable)
But the function expects an integer and I don't have one.
Does anyone have a different approach?
Option 1
If there is a limited number of colors that the user can choose from, you could create a selector resource for each color option and then use the `setInt()` method to assign the resource ID that corresponds to the selected color.
Option 2
If by "freely choose" you mean that the user can select any possible color - or at least from a very large number - option 1 won't work of course.
Depending on what your drawable exactly looks like, you could try this instead:
Create only a single selector resource.
Use an ImageView for your button.
Apply the color as a tint to the ImageView using the ImageView.setColorFilter() method.
That would look something like this:
remoteView.setInt(R.id.button_id, "setColorFilter", Color.parseColor("#0000"))
I am trying to add an item in a bottom menu navigation bar according to a var got from an API.
I am able to delete an item from this navigation bar, like this :
if (restaurant.acceptsBookings == false) {
bottom_navigation_view.menu.removeItem(R.id.bottom_menu_book)
}
The problem is, when I am launching my app, we can see the icon during like, half of a second, then it disappears.
This is not that bad, but I was hoping there is a better and neater way to do this ; for example by adding the elements in an empty navigation bar, instead of removing them.
Here is my navigation bar xml code :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/bottom_menu_home"
android:enabled="true"
android:title="#string/bottom_menu_home"
android:icon="#drawable/ic_home"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_menu"
android:enabled="true"
android:icon="#drawable/ic_menu"
android:title="#string/bottom_menu_menu"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_profile"
android:enabled="true"
android:title="#string/bottom_menu_profile"
android:icon="#drawable/ic_user"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_book"
android:enabled="true"
android:icon="#android:drawable/ic_menu_my_calendar"
android:title="#string/bottom_menu_bookings"
app:showAsAction="ifRoom" />
<item
android:id="#+id/bottom_menu_fidelity"
android:enabled="true"
android:icon="#drawable/giftgrey2"
android:title="#string/bottom_menu_fidelity"
app:showAsAction="ifRoom" />
</menu>
Do someone have a solution for this ?
Thanks in advance.
First, thanks everyone for helping me.
I tried both methods and they work perfectly : Hide the navigation bar with
bottomNavigationMenu?.visibility = View.GONE
just after bottomNavigationMenu declaration, and then set
bottomNavigationMenu?.visibility = View.VISIBLE
just after API response.
The method which create dynamically an item works too, here is how
bottomNavigationMenu?.menu?.add(Menu.NONE, 1, Menu.NONE, "TEST")?.setIcon(R.drawable.ic_home)
Here is what the man tell about the add fun (from https://developer.android.com/reference/android/view/Menu.html ; ctrl + f "add" 43) :
groupId int: The group identifier that this item should be part of. This can be used to define groups of items for batch state changes. Normally use NONE if an item should not be in a group.
itemId int: Unique item ID. Use NONE if you do not need a unique ID.
order int: The order for the item. Use NONE if you do not care about the
order. See getOrder().
title CharSequence: The text to display for the item.
Thanks everyone for helping !
This sounds like a race condition.
What you can do is block the drawing of the navigation menu or the entire screen until after you get a response from the API. This just means your app will take half a second longer to launch.
Instead of using a static menu.xml file, what you can do is dynamically add menus that suits your condition.
Perhaps, this link may help: Inflate Bottom Navigation View menu programmatically
I have been developing an app in Android. I have created a menu with some static elements:
<menu
android:id="#+id/submenu">
<item android:id="#+id/create_new"
android:title="eganaude" />
<item android:id="#+id/open"
android:title="skema" />
</menu>
and I have a rule to add other elements dynamically:
menu_global.add(0, new_hash_value, 0, text);
However, this way each time I restart the app, I should add again the elements to the menu. So I would like the new elements added dynamically to appear at each restart. Is there a way to implement this behaviour?
I recommend you to use an internal database to keep your information, is easy to implement it, look at this link with information:
https://developer.android.com/guide/topics/data/data-storage.html#db
The only way to do that is to save the new added menu items to database or shared preferences or in a file and then later read/load the items and add them to the menu
Note to all: This may be a duplicate question but I could not find a question with an answer that actually helped me out so I was forced to make this.
I am folowing the tutorial on how to make an Android application and I am stuck on this section: https://developer.android.com/training/basics/actionbar/adding-buttons.html
I am stuck on "Respond to Action Buttons" because action_search cannot be resolved or is not a field in this code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openSearch(){
}
public void openSettings(){
}
This line
case R.id.action_search:
This is hindering the progress of my learning and I would like to know how to fix it. To me it just seems like poor tutorial teaching on Android's part but it could be my fault too. Thanks!
EDIT: My XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
</menu>
Just had this exact problem. The tutorial fails to mention these two steps.
If you haven't yet, do the following:
Add an image named ic_action_search.png to the 4 different res/drawable folders
Create a string named action_search in strings.xml
<string name="action_search">Search</string>
The main_activity_actions.xml fails to find those two things and doesn't add action_search to the R.java file, but doesn't give a useful error to point you in that direction.
Good luck!
EDIT: Also make sure you're using the app:showAsAction. As explained here.
What IDE you are using, if it's NetBeans, remove the line contains R.id.action_search and build your project, then add this line, if u are using Android Studion, i think it needs to work, but dont import R from android, import it from your project main package, I think the probleme is in the package where u defined your activity
The above answer (dmarsi) is 100% correct.
The Android Developers tutorial mentions the following:
http://developer.android.com/training/basics/actionbar/adding-buttons.html
The icon attribute requires a resource ID for an image. The name that follows #drawable/ must be the name of a bitmap image you've saved in your project's res/drawable/ directory. For example, "#drawable/ic_action_search" refers to ic_action_search.png. Likewise, the title attribute uses a string resource that's defined by an XML file in your project's res/values/ directory, as discussed in Building a Simple User Interface.
I struggled with the same problem until I created the string and added a random .png image for ic_action_search.png
Good luck with the rest of the tutorial!
I have managed to create a menu with four options. I would now like to create four sub menus of the same style for each option.
In my infinite nooby-ness I have created four classes for the sub menus but I cannot figure out how to move between the menus(Classes). For instance I have four options Prem, Champ, L1, L2--I have created the Prem sub menu.
How do I get the program to move to the class(SubMenu) when the Prem option is selected, and how do I get it to move back?
If you haven't already, check out the Dev guide entries for the Menu resource and creating a menu.
basically, if you're using eclipse you just have to add an Android XML file and specify the the content is a menu. You can then add submenus and the system takes care of navigation for you. (You will need to load the menu using the MenuInflater function)
You should end up with your menu xml looking like this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_item1" android:title="#string/menu_item1"></item>
<item android:title="#string/menu_submenu" android:id="#+id/menu_submenu">
<menu>
<item android:id="#+id/menu_sub1" android:title="#string/menu_sub1"></item>
<item android:id="#+id/menu_sub2" android:title="#string/menu_sub2"></item>
<item android:id="#+id/menu_sub3" android:title="#string/menu_sub3"></item>
</group>
</menu>
</item>