Android Lollipop, add popup menu from title in toolbar - android

I'm unable to see how adding a popup menu from the title is accomplished like is shown in many of the material design examples. Any help would be much appreciated.

You're going to need to add a Spinner to the Toolbar:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">
<Spinner
android:id="#+id/spinner_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
You will then need to disable the default title:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
You can then retrieve and setup the Spinner as needed in your Activity/Fragment.

I came across this question while I was trying to find a solution to prevent the popup to overlay the spinner and I would like to leave here an alternative solution to this question as its possible to add the spinner to the toolbar using the menu.xml as well
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorAccent" />
</android.support.design.widget.AppBarLayout>
<!-- Other layout widgets -->
</LinearLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/spinner"
android:title="Spinning"
app:actionViewClass="android.widget.Spinner"
app:showAsAction="always" />
<!-- Other items -->
</menu>
Your Activity
It will be necessary to override the onCreateOptionMenu() method, then use getMenuInflater() to inflate the menu file created earlier.
You will also need to get the Spinner item and set an adapter to it as you would normally do.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
//Get Spinner item from menu
MenuItem spinnerMenuItem = menu.findItem(R.id.spinner);
final Spinner spinner = (Spinner) MenuItemCompat.getActionView(spinnerMenuItem);
//Set adapter whichever way you prefer (from the resource or manually)
final ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter
.createFromResource(this, R.array.items_array, android.R.layout.simple_spinner_dropdown_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
return true;
}
Style.xml
Finally, if you want to customize your spinner
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:spinnerStyle">#style/spinner_style</item>
</style>
<style name="spinner_style" parent="Widget.AppCompat.Spinner">
<item name="android:dropDownVerticalOffset">40dip</item>
<!--<item name="android:dropDownHorizontalOffset">0dip</item>-->
<item name="overlapAnchor">false</item>
<!--Other customizations-->
</style>

Related

SearchView icons are not showing in Android

I am trying to understand how to implement a SearchView in Android in my toolbar. The problem is that depending on the configurations I choose I get different interface results.
This is the layout of my Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="fill_parent"
tools:context="com.construct.v2.activities.teams.ActivityTeams"
android:background="#ffffff"
android:id="#+id/relativeLayout">
<include android:id="#+id/toolbar" layout="#layout/toolbar_shadow"/>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/toolbar" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toolbar">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
app:tabMode="fixed"
app:tabGravity="fill"
app:tabIndicatorColor="#125688"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</RelativeLayout>
This is my toolbar_shadow layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/PopupMenuStyle"
android:id="#+id/my_awesome_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:titleTextColor="#android:color/black"
android:layout_alignParentTop="true"
android:gravity="right"
android:elevation="5dp">
<TextView
android:id="#+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/s_text_size"
android:padding="#dimen/small_margin"
android:layout_gravity="right|end"
android:layout_marginEnd="#dimen/s_margin"
android:textColor="#000000"
android:text="#string/edit"/>
</android.support.v7.widget.Toolbar>
And this is the layout file for my search item:
<?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:id="#+id/search"
android:title="#string/search"
app:icon="#drawable/ic_search_black_24dp"
android:icon="#drawable/ic_search_black_24dp"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
With this configuration I get this output:
But if I click on the magnifying glass I get this:
If I start typing I get this:
So I thought that it was because I have this configuration app:showAsAction="collapseActionView|ifRoom" but I tried to change to always but i got this output:
The magnifying glass disappears. But if I click on the place where it is supposed to be I get this output:
Which is the correct output I am looking for, but I don't know why the magnifying glass is not showing.
This is the part in my activity where I initialize the searchview:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search_team, menu);
SearchManager searchManager = (SearchManager)
getSystemService(Context.SEARCH_SERVICE);
searchMenuItem = menu.findItem(R.id.search);
searchView = (SearchView) searchMenuItem.getActionView();
ImageView searchClose = (ImageView) searchView.findViewById(android.support.v7.appcompat.R.id.search_close_btn);
searchClose.setImageResource(R.drawable.ic_action_cancel_black);
for (TextView textView : findChildrenByClass(searchView, TextView.class)) {
textView.setTextColor(Color.BLACK);
textView.setHintTextColor(Color.BLACK);
}
searchView.setSearchableInfo(searchManager.
getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return true;
}
And this is how I initialize my toolbar:
#Override
protected void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar!=null) {
if (user_type == 1) {
toolbar.setTitle(R.string.collaborators_title);
} else if (user_type == 2) {
toolbar.setTitle(R.string.admin_title);
}
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_back_black);
//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//toolbar.setTitle("Teste");
//toolbar.setTitleTextColor(Color.parseColor("#000000"));
//final TextView edit = (TextView) findViewById(R.id.delete);
//edit.setText(" ");
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
}
}
Since I can't see the rest of the code where you implement the functionality for the x button, I'm just gonna take a stab at it. Add a listener for when the user leaves the search view to go back to the view with the magnifying glass. In that listener add this line:
<Your activity>.findViewById(R.id.search).setVisibility(View.VISIBLE);
It could be possible that the searchview automatically set the view to invisible, but you never reset the visibility for the user.
Edit:
app:icon="#drawable/ic_search_black_24dp"
android:icon="#drawable/ic_search_black_24dp"
there is difference between app: prefix and android: prefix. With appcompat-v7 library you can use android: prefix and remove app:icon line .
app:showAsAction="collapseActionView|ifRoom"
Change this line to
app:showAsAction="always|collapseActionView"
In searchview if we select only always as showasAction then back arrow is not visible.
Check the theme and styles of your app because a I had had the same problem that you and it made me crazy.
I found the solution changing the theme and styles.
If you are using Toolbar I assume that your main theme is
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
For the AppBarLayout use this
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark" />
For the Toolbar use this
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
Note that one is Dark and the other is Light. You can exchange both if it adjust better to your main theme
Use the suggest from Zohra Khan
Change this line to
app:showAsAction="always|collapseActionView" In searchview if we select only always as showasAction then back arrow is not visible.
If it's not clear, take a look to this example from where I took this ideas
Hope this helps you

Menu with Text only not showing in Toolbar

I have to show back icon, Logo and text (Next) in Toolbar. Next is right aligned. I tried placing text via menu. I have set logo and back button to toolbar.
toolbar.setLogo(R.mipmap.logo);
toolbar.setNavigationIcon(R.mipmap.back);
Also, I have inflated following menu in onCreateOptionsMenu
Menu inflated:
<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"
>
<item
android:id="#+id/action_next"
android:title="#string/next"
app:showAsAction="always" />
Text Menu is not visible at all. if I put icon in menu item, it gets displayed.
I have used android.support.v7.widget.Toolbar.
Edited:
I solved it. In case someone else gets struck with such issue.
Please add
<item name="android:actionMenuTextColor">#color/blue</item>
in styles. Menu itemw as getting selected on click but was not visible, means it was showing white text color on white toolbar. Setting menuText color solved my problem.
Android Documentation explains that the withText option for showAsAction will
Also include the title text (defined by android:title) with the action item.
Since that is what you want, code the following.
<item
android:id="#+id/action_next"
android:title="#string/next"
app:showAsAction="always|withText" />
You can do something like this:
First, remove default title in your Activity:
getSupportActionBar().setDisplayShowTitleEnabled(false);
Then, your toolbar layout can be like this:
<LinearLayout
android:id="#+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/material_blue"
android:elevation="5dp"
android:minHeight="?attr/actionBarSize" >
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:gravity="end"
android:text="#string/app_name"
android:textColor="#android:color/white" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
And, in your activity handle the click event:
toolbarTop.findViewById(R.id.toolbar_title).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG,"Clicked");
}
});
Try Changing to support widget. This worked for me
app:actionViewClass="android.support.v7.widget.SearchView"
This what it looked after the change
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<item android:id="#+id/search"
android:title="#string/search_title"
android:icon="#drawable/ic_search"
app:showAsAction="always|withText"
app:actionViewClass="android.support.v7.widget.SearchView"
tools:context=".AppHome.HomeActivity"/>
</menu>
If you are getting an empty action with no text but blank space in it,
it might happen if you have created your own tool bar in the layout and have also used the Action bar in your theme's parent.
If you are creating your own toolbar then use parent="Theme.MaterialComponents.DayNight.NoActionBar" in your themes, both normal and night themes.
It's happening because of the style used in your layout.
To solve this, define a new style and use it in your layout.
<style name="ToolBarStyle" parent="ThemeOverlay.MaterialComponents.Dark.ActionBar">
<item name="android:itemBackground">#fff</item> // background color of the menus
<item name="android:textColor">#000</item>// text color of the menus
</style>
Use it like this in your layout
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/ToolBarStyle"
app:popupTheme="#style/ThemeOverlay.MaterialComponents.Light"
android:background="#color/primary_pink"
android:minHeight="?attr/actionBarSize"
android:elevation="4dp"
/>

How can I override my menu bar to come on my application bar?

I have recycler view + viewpager + toolbar in my activity. When I long click on the row in recycler view it should show me a menu bar with a delete option. But this is what I get. Any ideas as to why it is coming above my application name?
This is the xml of my main activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kaad.sampleproject.MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/main_activity_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="#+id/main_activity_tablayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/main_activity_tool_bar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:tabIndicatorColor="#ff00ff"
app:tabMode="fixed"
app:tabSelectedTextColor="#ffffff"
app:tabTextColor="#d3d3d3" />
<android.support.v4.view.ViewPager
android:id="#+id/main_activity_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/main_activity_tablayout" />
</RelativeLayout>
I kept an add and about menu which never gets displayed for some reason:
Here is the xml of both my menus:
<?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"
android:id="#+id/menu_bar_main"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
tools:context=".MainActivity">
<item
android:id="#+id/menu_item_new"
android:icon="#drawable/ic_add_white_36dp"
android:title="#string/menu_new"
app:showAsAction="ifRoom|withText" />
<item
android:id="#+id/menu_item_about"
android:title="#string/menu_about"
app:showAsAction="never" />
</menu>
<?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"
android:id="#+id/menu_bar_delete"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
tools:context=".MainActivity">
<item android:id="#+id/menu_item_delete"
android:icon="#android:drawable/ic_menu_delete"
android:title="#string/delete" />
</menu>
The xml of my styles
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--<item name="colorPrimary">#FF595951</item>-->
<!--<item name="colorPrimaryDark">#FF5C5C5C</item>-->
<!--<item name="colorAccent">#FFCCCCCC</item>-->
<!--<item name="android:textColorPrimary">#FF0FF0</item>-->
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
This is Main Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_main);
database = new Database(this);
categories = database.getCategories();
myToolbar = (Toolbar) findViewById(R.id.main_activity_tool_bar);
setSupportActionBar(myToolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
myViewPager = (ViewPager) findViewById(R.id.main_activity_viewpager);
myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), MyMainActivity.this, categories);
myViewPager.setAdapter(myPagerAdapter);
// Give the TabLayout the ViewPager
myTabLayout = (TabLayout) findViewById(R.id.main_activity_tablayout);
myTabLayout.setupWithViewPager(myViewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < myTabLayout.getTabCount(); i++) {
TabLayout.Tab myTab = myTabLayout.getTabAt(i);
myTab.setCustomView(myPagerAdapter.getTabView(i));
}
bus.getInstance().register(this);
}
This is how I am calling my menu bar in my main prescription fragment
public class CustomMultiSelectorCallback extends ModalMultiSelectorCallback {
private int count;
public CustomMultiSelectorCallback(MultiSelector multiSelector) {
super(multiSelector);
}
#Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
super.onCreateActionMode(actionMode, menu);
count = 0;
actionMode.getMenuInflater().inflate(R.menu.list_item_delete, menu);
return true;
}
Issue for About button:
<item
android:id="#+id/menu_item_about"
android:title="#string/menu_about"
app:showAsAction="never" />
update the never part to "ifRoom"
Regarding the menu appearing, can you be more explicit, as to me it seems in Relative Layout you wanted the Toolbar to be top and the views to come after it and that is what it shows.
So I have solved one of the problems. This is what I edited in my code to display add and about buttons in the toolbar.
First, in your onCreate in the fragments, add this line:
setHasOptionsMenu(true);
Second, in your styles, change your theme to something which has NoActionBar. In my case I did this:
<style name="AppThemeOfStyles"parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#FF595951</item>
<item name="colorPrimaryDark">#FF5C5C5C</item>
<item name="colorAccent">#FFCCCCCC</item>
<item name="android:textColorPrimary">#FF0FF0</item>
<item name="windowActionBar">true</item>
<item name="windowNoTitle">true</item>
</style>
I also changed the name of my theme from AppTheme to AppTheme of styles since AppTheme is already a theme defined. So better to change your theme name to something else.
This leads us to the result below:
But my delete is still coming on the top of the toolbar on longpress.
Any suggestions, advice will be appreciated :)
UPDATED:
Use this in your theme for the overlay to work:
<item name="windowActionModeOverlay">true</item>
Here is the end result:

Dynamic Menus in NavigationView

I have this Layout:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/nav_header"/>
So, looking that in NavigationView we have the attribute:
app:menu="#menu/drawer_menu"
And I have this XML id menu folder.
I wanna make dynamic menus, ie, in code I should mount the 'MenuItem' objects and set into NavigationView.
Is this correct?
Is this best pratice?
Is this possible?
Note: My code is working with 'static drawer_menu', I want to improve it.
I'm waiting.
[EDIT]
I make it:
Menu menu = nvDrawer.getMenu();
for (KSMenuItem kmi : menus.values()) {
if (menu.size() == 0) {
menu.add(kmi.getId());
}
if (menu.getItem(kmi.getId()) == null) {
menu.add(kmi.getId());
}
MenuItem mi = menu.getItem(kmi.getId());
mi.setIcon(kmi.getIcon());
mi.setTitle(kmi.getTittle());
}
But this error happened:
06-27 15:26:15.538 15335-15335/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.sticdev30.newdrawer, PID: 15335
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sticdev30.newdrawer/com.example.sticdev30.newdrawer.MainActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
KSMenuItem is a POJO with my menu data. In kmi.id I informed incremental integers...
I'm waitning
You can re-inflate NavigationViewat runtime with 2 lines of code using public method inflateMenu. In this example i re-inflate with new_navigation_drawer_items.xml
navigationView.getMenu().clear(); //clear old inflated items.
navigationView.inflateMenu(R.menu.new_navigation_drawer_items); //inflate new items.
You can add menus dynamically by following steps :
Step1. Obtain menu object from Navigation View NavigationView.getMenu()
Step2. Add any item to the menu using Menu.add()
We can dynamically add/remove menu items.
Suppose we have this menu items `
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_home"
android:icon="#mipmap/home_icon_x48"
android:title="Home" />
<item
android:id="#+id/nav_part_catalouge"
android:icon="#mipmap/catalogue_icon_x48"
android:title="Parts Catalogue" />
<item
android:id="#+id/nav_favourite"
android:icon="#mipmap/my_favourate_x48"
android:title="My-Favourite" />\
<item
android:id="#+id/nav_opencarrequest"
android:icon="#mipmap/cart_request"
android:title="Cart-Request" />
<item
android:id="#+id/nav_setting"
android:icon="#mipmap/settings_x48"
android:title="Settings" />
</group>
<item android:title="">
<menu>
<item
android:id="#+id/nav_logout"
android:icon="#mipmap/logout_icon_x48"
android:title="Logout" />
</menu>
</item>
`
In Activity we can add or remove menuitems based on our condition
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// your activity layout here
NavigationView navigationView= (NavigationView) findViewById(R.id.nav_view); // navigation view which holds menu items
navigationView.setNavigationItemSelectedListener(this);
if(yourCondition){
navigationView.getMenu().getItem(0).setVisible(false); //if you want to hide first item
navigationView.getMenu().getItem(1).setVisible(true); // if you want to show second menu item should be visible
}
}
Hope it will help.
Seems like kmi.getId() returns int(or long).
But Menu.add(int) adds menu with title from the given string resources, which is usually represented as R.string.something, and not for usual integer values.
Menu.add(CharSequence) does add menu with title of CharSequence, so you need to do some int-to-string conversion like menu.add(kmi.getId() + "");

AppCompat Toolbar showing an unwanted settings button

I just started using toolbar instead of actionbar in my android app. But it shows a ridiculous settings button at the bottom of the screen and i couldn't find a way to erase it. Please help me with this.
Here is a screenshot for you to understand the problem:
http://i.stack.imgur.com/x1Gwe.png
Here is my code:
SetContentView(Resource.Layout.Main_Activity_Container);
var toolbar = FindViewById<Toolbar>(Resource.Id.Toolbar);
var toolbarBottom = FindViewById<Toolbar>(Resource.Id.BottomToolbar);
SetSupportActionBar(toolbar);
SupportActionBar.Title = "Hello from Toolbar";
SetSupportProgressBarIndeterminate(false);
toolbarBottom.Title = "";
toolbarBottom.InflateMenu (Resource.Menu.tabs);
toolbarBottom.MenuItemClick += delegate(object sender, Toolbar.MenuItemClickEventArgs e)
{
Toast.MakeText(this,
"Bottom toolbar pressed: " + e.Item.TitleFormatted, duration: ToastLength.Short).Show();
indeterminateVisible = !indeterminateVisible;
SetSupportProgressBarIndeterminateVisibility(indeterminateVisible);
};
axml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/Toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
android:layout_alignParentTop="true"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:background="?attr/colorPrimary" />
<android.support.v7.widget.Toolbar
android:id="#+id/BottomToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/actionBarSize"
style="#style/HeaderBar"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/Toolbar"
android:layout_above="#id/BottomToolbar" />
</RelativeLayout>
styles.xml
<style name="MyTheme" parent ="#style/Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#1976D2</item>
<item name="windowActionModeOverlay">true</item>
<item name="colorAccent">#FF4081</item>
<item name="android:actionMenuTextColor">#24598a</item>
<item name="android:actionButtonStyle">#style/ActionButtonStyle</item>
</style>
<style name="ActionButtonStyle" parent="#style/Widget.AppCompat.ActionButton">
<item name="android:minWidth">70dip</item>
<item name="android:paddingStart">0dip</item>
<item name="android:paddingEnd">0dip</item>
</style>
tabs.xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_share"
android:icon="#drawable/icon"
local:showAsAction="always"
android:title="Edit" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon"
local:showAsAction="always"
android:title="Undo" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon"
local:showAsAction="always"
android:title="Redo" />
<item android:id="#+id/menu_share"
android:icon="#drawable/icon"
local:showAsAction="always"
android:title="Save" />
I also want to change the positions of these menu items. I know that i can use custom components inside of toolbars but i don't get why these menu items always appear in the right hand side.
Thank you.
I had the same issue, I resolved it by following below steps :
First of all to remove the disgusting settings button at bottom do the following :
In your activity, comment getMenuInflater().inflate(...) :
....
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.display_map, menu);
return true;
}
....
This will surely remove settings button from bottom, it did mine.
Now you can create your own menu items as normal views, add as many buttons as you want in your toolbar, whereever you want, see an example :
Toolbar is just a ViewGroup, So, in your toolbar.xml file or whereever you have defined android.support.v7.widget.Toolbar, change as per your requirements of buttons and their positions.
Below shows how to add an button at the center of toolbar, you can change it to your required position :
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:background="#drawable/color_toolbar"
android:layout_width="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="button"
android:onClick="buttonHandler"/>
</android.support.v7.widget.Toolbar>

Categories

Resources