I want to implement a BottomNavigationView and have added one of material.io's icons as a png to my drawables. When I insert it as:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/navigation_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="#string/title_home" />
<item
android:id="#+id/navigation_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="#drawable/ic_dashboard_black_24dp"
android:title="#string/title_dashboard" />
<item
android:id="#+id/navigation_notifications"
android:icon="#drawable/ic_notifications_black_24dp"
android:title="#string/title_notifications" />
<!-- This is my item added to the normal template -->
<item
android:id="#+id/navigation_more"
android:icon="#drawable/ic_more_horiz_black_24dp"
android:title="#string/title_more" />
</menu>
And use it in the main activity, It shows up, but the item will not highlight when pressed on the simulator, whereas the others will ( by highlight, I mean it will slightly blow up and change to the primary color, showing some text underneath). I tried both vectors and .pngs, nothing will work. I am running backwards compatible to Android 5.0 (Target Version 27).
The home Activity currently looks like:
public class HomeActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener
mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
The text view is set correctly, the icon just won't highlight.
your navigation method always return false, try return true when there is a case handled by the switch ;)
for example:
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
case R.id.navigation_more:
mTextMessage.setText(R.string.title_more);
return true; // this was my mistake...
default:
return false;
}
return false;
}
Related
i have two layouts that use a drawerLayout and they use the same code for the navigationView, the problem is that one of them changes the color of the selected item while the other doesn't even though it's the same exact code. here is the xml code:
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view_passager"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/menu_passager" />
and java for the 1st layout:
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.acceuil_passager_item:
toolbar.setTitle("Accueil");
fm.beginTransaction().replace(R.id.frame_passager, new AcceuilPassagerFragment()).commit();
break;
case R.id.profile_item:
toolbar.setTitle("Profil");
fm.beginTransaction().replace(R.id.frame_passager, new PassagerProfileFragment()).commit();
break;
case R.id.historique_voyages_item_pass:
toolbar.setTitle("Historique des voyages");
fm.beginTransaction().replace(R.id.frame, new ListeTrajetsFragment()).commit();
break;
case R.id.futurs_voyages_item_pass:
toolbar.setTitle("Futurs voyages");
fm.beginTransaction().replace(R.id.frame_passager, new FutursVoyagesFragment()).commit();
break;
case R.id.log_out_item_pass:
Intent intent = new Intent(PassagerActivity.this, LoginActivity.class);
startActivity(intent);
mAuth.signOut();
finish();
Log.d(TAG, "onNavigationItemSelected: " + (mAuth == null));
break;
default:
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
and the java code for the 2nd layout :
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.acceuil_item_conducteur:
setUpToolbar(item);
fm.beginTransaction().replace(R.id.frame_conducteur, new AcceuilConducteurFragment()).commit();
break;
case R.id.profile_item_cond:
setUpToolbar(item);
fm.beginTransaction().replace(R.id.frame_conducteur, new ConducteurProfileFragment()).commit();
break;
case R.id.historique_voyages_item_cond:
setUpToolbar(item);
fm.beginTransaction().replace(R.id.frame_conducteur, new HistoriqueVoyagesFragment()).commit();
break;
case R.id.log_out_item_cond:
Intent intent = new Intent(ConducteurActivity.this, LoginActivity.class);
startActivity(intent);
mAuth.signOut();
finish();
Log.d(TAG, "onNavigationItemSelected: " + (mAuth == null));
break;
default:
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
and here is the first layout
and the 2nd one
ps: "Profil" is selected in both layouts
For navigation view you have to write a color selector like: nav_view_item_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/primary" android:state_checked="true" />
<item android:drawable="#android:color/transparent" />
</selector>
and set app:itemBackground="#drawable/nav_view_item_background"
For text color you have to same thing. Create a textColor selector and set app:itemTextColor
For Drawer Layout
return true (In Navigation item selection) change the color and selection.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group
android:id="#+id/menu_grp"
android:checkableBehavior="single">
Turned out checkableBehavior = "single" is what made the menu selected item checked and what was missing in my other layout
I have map activity and I am trying to add toggle button to the actionbar next to the setting menu which contains the map type but I am facing a problem: the toggle button is being added to the setting items menu and not directly to the actionbar.
How can I add this toggle button to the actionbar next to the setting menu?
map_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mapTypeNormal"
android:title="Normal"/>
<item
android:id="#+id/mapTypeSatellite"
android:title="Satellitte"/>
<item
android:id="#+id/mapTypeTerrain"
android:title="Terrain"/>
<item
android:id="#+id/mapTypeHybrid"
android:title="Hybrid"/>
<item
android:id="#+id/mapTypeNone"
android:title="None"/>
<item android:id="#+id/menu_toggle"
android:showAsAction="ifRoom"
android:icon="#drawable/off"
android:title="Share"
/>
</menu>
map activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.map_menu, menu);
System.out.println("ABC MAP onCreateOptionsMenu was invoked.");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.mapTypeNone:
map.setMapType(GoogleMap.MAP_TYPE_NONE);
break;
case R.id.mapTypeNormal:
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
break;
case R.id.mapTypeSatellite:
map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
break;
case R.id.mapTypeTerrain:
map.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
break;
case R.id.mapTypeHybrid:
map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
break;
default:
break;
}
switch (item.getItemId()) {
case R.id.menu_toggle:
if (birthSort) {
// change your view and sort it by Alphabet
item.setIcon(R.drawable.on);
item.setTitle("On");
birthSort = false;
} else {
// change your view and sort it by Date of Birth
item.setIcon(R.drawable.off);
item.setTitle("Off");
birthSort = true;
}
}
return super.onOptionsItemSelected(item);
}
}
Use additional attribute always on menu item:
app:showAsAction="always"
and it can be combined with text as well:
app:showAsAction="withText|always"
Adjust your namespace accordingly. Here is a complete sample:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/action_sync"
android:title="#string/action_sync"
app:showAsAction="withText|always"/>
</menu>
Pay attention to the app: prefix for showAsAction and the corresponding namespace declaration.
![Layout Look like this][1]
[1]strong text: http://i.stack.imgur.com/CIsz8.png
I am trying to create a menu, but I am unable to see the menu.i am able to see menu in fragments properly. but cannot see in the sherlockfragmentactivity that is my main frame for fragment.
when i click on menu they doesn't take any action.
menu code
<group android:id="#+id/dashbardmenu">
<item
android:id="#+id/abc"
android:title="ABC">
</item>
<item
android:id="#+id/setting"
android:title="Setting">
</item>
</group>
<group android:id="#+id/fragmentmenu" >
<item
android:id="#+id/form"
android:icon="#drawable/ic_send"
android:showAsAction="ifRoom"
android:title="Form">
</item>
<item
android:id="#+id/resetform"
android:title="Reset Form">
</item>
</group>
im using visibility & invisibility for particular file.
java code
1st way
public boolean onCreateOptionaMenu(Menu menu ){
MenuInflater inflater = this.getSupportMenuInflater();
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return super.onCreateOptionsMenu(menu);
}
2nd way
public boolean onCreateOptionaMenu(Menu menu,MenuInflater inflater ){
inflater.inflate(R.menu.activity, menu);
menu.setGroupVisible(R.id.fragmentmenu, false);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return false;
}
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected((MenuItem)item);
switch (item.getItemId()){
case R.id.abc:
//Some action
case R.id.setting:
//Some action
default:
return true;
}
}
Where is my Mistake Anyone help me???
Did you call this?
setHasOptionsMenu(true);
Also you have a typo - method name is onCreateOptionsMenu but you have twice onCreateOptionaMenu
I have a submenu with buttons set up in xml which works fine.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Single menu item
Set id, icon and Title for each menu item
-->
<item android:id="#+id/menu_level"
android:title="Set Level" >
<menu>
<group android:checkableBehavior="single">
<item android:id="#+id/one"
android:checked="false"
android:title="One" />
<item android:id="#+id/two"
android:title="Two" />
<item android:id="#+id/three"
android:title="Three" />
</group>
</menu>
</item>
<item android:id="#+id/menu_save"
android:icon="#drawable/icon_save"
android:title="Save" />
<item android:id="#+id/menu_about"
android:icon="#drawable/icon_bookmark"
android:title="About">
<menu>
<item android:id="#+id/copyright"
android:title="(c) John Beukema 2012">
</item>
</menu>
<item/>
</menu>
I access it as follows:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Editor editor = PreferenceManager
.getDefaultSharedPreferences(this).edit();
switch (item.getItemId())
{
case R.id.menu_save:
return true;
case R.id.menu_recall:
return true;
case R.id.menu_level: {
return true;
}
case R.id.menu_about:
return true;
case R.id.one:
star = 1;
editor.putInt("Star", 1);
editor.commit();
item.setChecked(true);
return true;
case R.id.two:
star = 2;
editor.putInt("Star", 2);
editor.commit();
item.setChecked(true);
return true;
case R.id.three:
star = 3;
editor.putInt("Star", 3);
editor.commit();
item.setChecked(true);
return true;
case R.id.menu_delete:
return true;
case R.id.menu_preferences:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
So far no problem. But I want to load the buttons on start up from preferences.
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(this);
star = preferences.getInt("Star", 1);
MenuItem one = (MenuItem) findViewById(R.id.one);
MenuItem two = (MenuItem ) findViewById(R.id.two);
MenuItem three = (MenuItem) findViewById(R.id.three);
switch(star)
{
case 1:
one.setChecked(true); break;
case 2:
two.setChecked(true); break;
case 3:
three.setChecked(true); break
}
}
}
This compiles but hangs. How do I address the submenu buttons?
I dont think you can do findviewbyid on menuitem. You need to set or unset the checked items in onprepareoptionsmenu
Menu Items getting disappeared in my app.
Initially i created menu items as follow. but menu getting disappeared in some android versions.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHelper tabHelper = getTabHelper();
CompatTab menuTab = tabHelper.newTab("menu").setText(
R.string.tab_section1).setIcon(R.drawable.home_icon).setTabListener(
new InstantiatingTabListener(this, MenuFragment.class));
tabHelper.addTab(menuTab);
CompatTab favTab = tabHelper.newTab("favourites").setText(
R.string.tab_section2).setIcon(R.drawable.favourites_icon).setTabListener(
new InstantiatingTabListener(this, FavouritesFragment.class));
tabHelper.addTab(favTab);
}
THIS ABOVE CODE WORKS FINE IN LATEST ANDROID 4.1.3 BUT FAILS TO DISPLAY IN 4.0.1 and 4.0.3
SEEMS TO BE SOME LAYOUT ISSUE.
TO RESOLVE i added menu xml as follow
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menuhome"
android:icon="#drawable/map_icon"
android:title="#string/tab_section1"
android:orderInCategory="1"
android:showAsAction="always|withText" />
<item android:id="#+id/menumap"
android:icon="#drawable/map_icon"
android:title="#string/tab_section2"
android:orderInCategory="2"
android:showAsAction="always|withText" />
and added onOptionItemSelected as follow
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.menuhome:
finish();
return true;
case R.id.menumap:
//ERROR
//HOW CAN I CALL FRAGMENT HERE.?
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}