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
Related
I am trying to show icons with overflow menu with below codes
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu,menu);
if(menu instanceof MenuBuilder){
MenuBuilder menuBuilder = (MenuBuilder) menu;
menuBuilder.setOptionalIconsVisible(true);
}
It gives me this error
menuBuilder.setOptionalIconsVisible can only be called from within the same library group
on line
menuBuilder.setOptionalIconsVisible(true);
I know I can suppress it for ignore but I want know why its coming and there any another way to fix it ?
Thanks
Edit
Ok, after investigating more on the subject this seems to be a bug as stated in comment and answers to this question and should be safe to suppress it. It may be fixed in one of the next releases of support libraries.
OLD ANSWER
Why not make menu items visible in XML? Use attribute android:showAsAction. There are several values available: ifroom | always | collapseActionView | never | withText - read more.
For instance if you want to always show first item and show second item if there is room for it:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/item_id1"
android:icon="#drawable/ic_icon1"
app:iconTint="#color/white"
app:showAsAction="always"
android:title="First item"/>
<item android:id="#+id/item_id2"
android:icon="#drawable/ic_icon2"
app:iconTint="#color/white"
app:showAsAction="ifRoom"
android:title="Second item"/>
</menu>
I am trying to check a default item in a menu group. I am not changing anything in the menu using code.
The menu is as follows:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:title="Map type">
<menu>
<group
android:checkableBehavior="single"
>
<item
android:id="#+id/nav_map_type_normal"
android:title="Normal"
android:checked="true"
/>
<item
android:id="#+id/nav_map_type_satellite"
android:title="Satellite"
/>
<item
android:id="#+id/nav_map_type_terrain"
android:title="Terrain"
/>
<item
android:id="#+id/nav_map_type_hybrid"
android:title="Hybrid"
/>
</group>
</menu>
</item>
<item
android:id="#+id/nav_toggle_traffic"
android:title="Toggle traffic"
/>
<item
android:id="#+id/nav_settings"
android:icon="#drawable/ic_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
/>
</menu>
When the menu is first displayed, the first item (Normal) is highlighted to indicate it is checked. This is what I want to happen. I then want the highlighting to move through the group as each item is pressed.
The problem is:
If I press Satellite, BOTH Normal and Satellite are highlighted
If I then press Hybrid, Normal remains highlighted, Hybrid is
highlighted and Satellite reverts to its unchecked state.
Only after actually pressing Normal does its checked state behave as it should.
I have tried removing the android:checked="true" and using performIdentifierAction in code, but this did not change the checked state.
Since you are making NORMAL as default TRUE, you need explicitly SET it to false as and when Satellite, Hybrid or Terrain is selected.
I believe currently, you would be setting NORMAL as android:checked=false on the onclickmenu selection code of nav_map_type_normal. i.e you are basically
un-checking it as by default it is selected as TRUE. Please do the same when other options are selected.
Sorry, I forgot to mention that the menu is used in a NavigationView and as it it turns out that changes everything.
navigationView.setCheckedItem(R.id.nav_map_type_normal);
performs exactly the way I want. I found it in a comment here Navigation drawer: How do I set the selected item at startup?
I am trying to use a custom title to include an image button to the title bar.
I got a lot of help form this post: android: adding button to the title of the app?, but could not get it work for my ListActivity.
In a nutshell, following is what I have:
I hide the titlebar in the AndroidManifest.xml
The specify a relative layout for the custom title (workorder_list_titlebar.xml)
My Activity Class looks like the following:
public class WorkOrderListActivity extends ListActivity {
String[] orders={"WO-12022009", "WO-12302009","WO-02122010", "02152010"};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
setContentView(R.layout.workorder_list);
setListAdapter(new ArrayAdapter(this,R.layout.workorder_list, R.id.label,orders));
}
}
When I ran the app, I got AndroidRuntimeException: You cannot combine custom titles with other title features.
Base on the stack trace, the exception was thrown by com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:183), that was triggered by setlistAdapter call.
Does anyone have the same problem with ListActivity?
Also once I manage to get this work, how do I attach listeners to the image button for it to do something?
Thanks in advance.
I had the same issue and I fix it deleting
<item name="android:windowNoTitle">true</item>
from my theme.xml
Make you create custom style in “values” folder. Make sure you code as below.
<style name="CustomTheme" parent="android:Theme">
Don't modify parent parameter.
This did work for me.
Instead of modifying your theme.xml you may also:
create a new XML style file my_theme.xml in values folder like this:
<style name="MyWindowTitleBackground">
<item name="android:background">#444444</item>
</style>
<style name="MyTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">#style/MyWindowTitleBackground</item>
</style>
You may define other settings as you like in this theme.
Then just use this theme in your manifest within the activity's attributes
android:theme="#style/MyTheme"
Finally set your custom title as always in your activity.java:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if (window.getContainer() == null) {
useTitleFeature = window
.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(R.layout.screen_main);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.custom_title);
// Set up the custom title
main_title = (TextView) findViewById(R.id.title_left_text);
main_title.setText(R.string.app_name);
main_title = (TextView) findViewById(R.id.title_right_text);
main_title.setText(R.string.Main_titleInfo);
}
Don't forget to define the custom_title.xml file in your layout folder. For example...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/title_left_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:ellipsize="end"
android:singleLine="true" />
<TextView
android:id="#+id/title_right_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:ellipsize="end"
android:singleLine="true"
android:textColor="#fff" />
</RelativeLayout>
I think notenking is right, that this is a problem in activities within tabs. Since some of my activities can either be stand-alone or within a tab, I've found the following helps:
final Window window = getWindow();
boolean useTitleFeature = false;
// If the window has a container, then we are not free
// to request window features.
if(window.getContainer() == null) {
useTitleFeature = window.requestFeature(Window.FEATURE_CUSTOM_TITLE);
}
setContentView(layoutId);
if (useTitleFeature) {
window.setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
May be you find this problem when use it in tab,for there already have a title and you can not add a custom title again.
you should add this custom title in the activity which you get the Tab
I did exactly as Sunny Dasari did but with one small change I put the # before and android in the parent attribute.
So my code looked like this.
<style name="CustomTheme" parent="#android:Theme">
To avoid crashing, you can simply add
android:theme="#style/android:Theme"
to the <Activity> tag in your AndroidManifest.xml:
<activity
android:name="test.TestActivity"
android:label="#string/app_name"
android:theme="#style/android:Theme">
This is because the styles defined in your default theme conflict with FEATURE_CUSTOM_TITLE (such as the attribute android:windowNoTitle). By using another theme, you can avoid such problems.
However, you might further need to define your own theme to change other attributes, such as android:windowTitleSize, background color, text color and font, etc. In this case, you can derive your theme from an existing theme (e.g., Theme.Light) and modify its attributes:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#323331</item>
</style>
<style name="CustomTheme" parent="#style/android:Theme.Light">
<item name="android:windowNoTitle">false</item>
<item name="android:windowTitleSize">60dip</item>
<item name="android:windowTitleBackgroundStyle">#style/CustomWindowTitleBackground</item>
</style>
</resources>
Try swapping following lines:
setContentView(R.layout.workorder_list);
this.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.workorder_list_titlebar);
I have run into this issue as well and it looks like it is an issue with what theme is applied to an activity in the AndroidManifest.xml file. If I use a theme like:
android:theme="#android:style/Theme.Holo
Then it will throw the error
android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
However if I use a different theme like:
android:theme="#android:style/Theme.Black"
then it will not throw the error and subsequently will not crash. However I am trying to use a theme like Theme.Holo. I'm not sure if there is a way around this.
Since, I was trying to compile my program in android 4.0, I was facing the same problem. None of these solutions helped.So, I copied my style contents from values > styles.xml and pasted it in values-v11 styles.xml file and values-v14 styles.xml file. Bingo, the trick worked.
As a beginner most of the answers didn't help me for my case. So here is my answer.
Go to res/values folder in your android project and check for strings.xml (this file may vary in your case, something like themes.xml)
Inside the file under resource tag check whether you have style tags. If you don't find it, add the code below as mentioned below as a child to resources tag
something like below
<resources>
<style name="SomeNameHere">
<item name="android:windowNoTitle">true</item>
</style>
</resources>
if you already have style tag, just add the code below to your style tag
<item name="android:windowNoTitle">true</item>
I've been trying to follow the official Android development tutorial for a couple of days but I've noticed there's a few discrepancies between the two available IDEs, Eclipse with the ADT plugin and Android Studio. I heard nice things about the latter so that's the one I'm using, but there seems to be differences in the way the files are laid out when you first create a new project. I've mostly been able to solve stuff here and there so it works just like on Eclipse (the IDE used on the tutorial) but I can't figure out why my Action Buttons aren't showing up in the ActionBar. Here's the tutorial for reference.
So, this is what my res/menu/main.xml file looks like:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.myapplicationn.app.MainActivity" >
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
</menu>
All I did was replace action_settings and copy action_search directly from the tutorial's sample code. There's a subtle (maybe not that subtle) difference here between AS and the tutorial: the tutorial's file is supposed to be res/menu/main_activity_actions.xml but its AS equivalent seems to be the default file res/menu/main.xml, so I went with that. Also, the tutorial sample only has the first attribute (xmlns:android="http://schemas.android.com/apk/res/android"), but the file created by AS had those additional ones so I left them untouched. Then, I modified the onCreateOptionsMenu method with this:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
Once again, the original code used R.menu.main_activity_actions, so I changed it to R.menu.main. The project compiles nicely and MainActivity shows up in my phone after a few seconds, but action_search is still hidden under the action overflow (isn't that what it's called?) button. Am I doing something wrong? Any help is appreciated. English isn't my first language and my writing style is kind of lousy, so sorry for that.
I had the same issue recently.
The showAsAction property is in the app namespace. It is explained in the tutorial that it is used when using the compatibility library. But if you are targeting an API level high enough that shouldn't be needed.
I was able to use the android namespace and it works fine for me. So change app:showAsAction to android: showAsAction and it will show up where you want it.
try this menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="ifRoom"
android:title="#string/action_search"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="#string/action_settings"/>
Make sure the activity which is creating the menu inherits from ActionBarActivity, which is the base activity class when using the support library.
Otherwise, whatever you set in app:showAsAction will not work.
I've scratched my head wondering why this didn't work for me in the past, dunno why they don't mention that in that tutorial you linked.
Hopefully this is your issue.
Bit late to the party. For those who still are having problem with this,
Try Removing this line
xmlns:app="http://schemas.android.com/apk/res-auto"
and change
app:showAsAction="always"
to
android:showAsAction="always"
IMPORTANT:
Make sure that you PNG Image is not larger than 27x27
I had the exact same problem while using Android Studio and following the official tutorial. Here is what worked for me, no idea why though.
1) I used the ic_launcher instead of the ic_action_search just to check if the search icon file itself had some issues.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_launcher"
android:title="#string/action_search"
app:showAsAction="ifRoom" />
Now I could see that the launcher icon was visible.
2) Then I downloaded again the icons from the "Action Bar Icon pack" and changed the android:icon back to ic_action_search.
And now the search button is visible.
Like I said not sure why this worked.
Me too, have been following the tutorials, the link you have poosted. Same thing, the buttons just not show in the ToolBar, tried different solutions. Finally, one from Udacity is working.
Solution:
on your MainActivity, add the follwing code:
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.your_menu,menu);
return true;
}
The rest is the same as the code in the official link, here, https://developer.android.com/training/appbar/actions.html#add-actions
I am having a list in my android application. The list items are custom objects and depending on the custom object property, list item color will be decided.
Now the problem is, when I select any item for such list, List selector is not displayed.
How can I fix this issue? To set list item color, I am using following method in adapter.
convertView.setBackgroundColor(Color.LTGRAY)
Is this the right way to set color? if not what else I can use.
Thanks in advance.
Swapnil Dalal.
You can fix this by two ways:
1.Write a selector for the itemview which set the background to the transparent in the pressed state, then set the selector as the backgound of the itemview.
<item android:state_enabled="true"
android:state_pressed="false"
android:drawable="#color/gray" />
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="#color/transparent" />
2.Remove the listselector, just write a selector for the itemview with the color you wanted by the different state and set it as the background of the itemview.
Please add your getView method code so that we can help you better. One common mistake we do is that we don't always create new view for each item in the list view for loading different layout list Items.
For example common approach for identical itemed list:
if(view == null)
{
vi = inflater.inflate(R.layout.fb_list_row, parent, false);
}
While for different object listview items you need to remove if statement like this;
vi = inflater.inflate(R.layout.fb_list_row, parent, false);
and then do the changing for each list item..
Hope this will help. Else put some more code.
I got solution for this question,
I created to different xml files for different objects in the application.
So in the getView of adapter, depending on condition we can load either one of the xml.
Example:
`if(true) {
convertView.setBackgroundResource(R.drawable.firstxml);
}
else {
convertView.setBackgroundResource(R.drawable.secondxml);
}`
In these xmls, we can specify colors as required.
Example:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#color/transperent"/>
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/listview_selector" />
</selector>
Thanks,
Swapnil Dalal.