I have created some app and I had like to insert to it a BottomNavigationView.
The code worked perfectly however once I changed at my gradle to androidx it stopped working.
The component in my layout (activity_about):
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="#menu/bottom_navigation"
app:itemBackground="#color/colorWhite"
app:itemIconTint="#drawable/bottom_navigation_foreground"
app:itemTextColor="#drawable/bottom_navigation_foreground" />
The menu file is:
<?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/navigation_library"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Library"
android:icon="#drawable/ic_home_black_24dp"/>
<item
android:id="#+id/navigation_search"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Search"
android:icon="#drawable/ic_search_black_24dp"/>
<item
android:id="#+id/navigation_profile"
android:enabled="true"
app:showAsAction="ifRoom"
android:title="Profile"
android:icon="#drawable/ic_account_circle_black_24dp"/>
</menu>
The drawable file (bottom_navigation_foreground):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#color/colorPurpleFont" />
<item android:state_checked="false" android:color="#color/Gray" />
</selector>
and my code is as follows:
public class AboutActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_library:
startActivity(new Intent(AboutActivity.this, DiscoverActivity.class));
break;
case R.id.navigation_search:
Toast.makeText(AboutActivity.this, "Favorites", Toast.LENGTH_SHORT).show();
break;
case R.id.navigation_profile:
Toast.makeText(AboutActivity.this, "Nearby", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
}
Any reason it is not working since I changed to androidx?
Thank you
Using androidx you have to switch to BottomNavigationView in the Material components library.
Add the dependency in build.gradle
dependencies {
//..
implementation 'com.google.android.material:material:1.2.1'
}
use a material theme and add in your layout:
<com.google.android.material.bottomnavigation.BottomNavigationView
.../>
i have solved same problem by replacing
<android.support.design.widget.BottomNavigationView
>
change it to and also implement the dependencies too
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="56dp"
>
in build.gradle
implementation 'com.google.android.material:material:1.2.0'
refer android documentation
https://developer.android.com/reference/com/google/android/material/bottomnavigation/BottomNavigationView
Related
Thanks for stopping by. So my problem here is, the icons from bottom_navigation_menu.xml from menu source file are not visible. I tried Google, Stackoverflow, and here are things that I have tried but still they are not visible: Restart Android Studio, Add selector, Change dependencies to androidX, Class to androidX, re-read the documentation.
Hope someone can help me out...thanks!!!!
I'm using Android Studio 3.6.3, Gradle 6.4
Class file
package com.example.todayilearnedbeta;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class AddEntriesActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addentries);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavView_Bar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(2);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.ic_main:
Intent intent0 = new Intent(AddEntriesActivity.this, MainActivity.class);
startActivity(intent0);
break;
case R.id.id_search:
Intent intent1 = new Intent(AddEntriesActivity.this, SearchActivity.class);
startActivity(intent1);
break;
case R.id.ic_add_entries:
break;
case R.id.ic_past_entries:
Intent intent3 = new Intent(AddEntriesActivity.this, PastEntriesActivity.class);
startActivity(intent3);
break;
case R.id.ic_profile:
Intent intent4 = new Intent(AddEntriesActivity.this, ProfileActivity.class);
startActivity(intent4);
break;
}
return false;
}
});
}
}
Layout XML codes:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AddEntriesActivity"
android:background="#drawable/add_entry_activity_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:id="#+id/bottomBar"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottomNavView_Bar"
app:menu="#menu/bottom_navigation_menu"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemBackground="#color/colorPrimary"
app:itemTextColor="#drawable/nav_item_color_state">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</RelativeLayout>
bottom_navigation_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/ic_main"
android:icon="#drawable/ic_main"
android:title="MainLogo"
/>
<item
android:id="#+id/ic_search"
android:icon="#drawable/ic_search"
android:enabled="true"
android:title="Search"
/>
<item
android:id="#+id/ic_add_entries"
android:icon="#drawable/ic_add_entries"
android:enabled="true"
android:title="AddEntries"
/>
<item
android:id="#+id/ic_past_entries"
android:icon="#drawable/ic_past_entries"
android:enabled="true"
android:title="PastEntries"
/>
<item
android:id="#+id/ic_profile"
android:icon="#drawable/ic_profile"
android:enabled="true"
android:title="Backup"
/>
Nav bar item colour selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="ffffff" android:state_checked="true" />
<item android:color="#000000" android:state_checked="false" />
</selector>
Dependencies (here just showing that I implement the latest material dependency.
dependencies {
implementation 'com.google.android.material:material:1.2.0-alpha06'
}
In your buttom_navigatiom_menu.xml , add this to set the visibility status
android:visible="true"
Make sure you add visibility status cod to each of the icon items
Example
<item
android:id="#+id/ic_add_entries"
android:icon="#drawable/ic_add_entries"
android:visible="true"
android:enabled="true"
android:title="AddEntries"
/>
If this does not work for you, try this:
Go-to File Menu in Android Studio
Invalid Caches & Restart
Open existing project
Now I have this
But I want to make this:
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/menu"
android:icon="#drawable/ic_menu"
android:title="#string/title_menu"
app:showAsAction="always" />
<item
android:id="#+id/file"
android:icon="#drawable/ic_file"
android:title="#string/title_file"
app:showAsAction="always" />
<item
android:id="#+id/new_file"
android:icon="#drawable/ic_new_file"
android:title="#string/title_new_file"
app:showAsAction="always" />
<item
android:id="#+id/visual"
android:icon="#drawable/ic_eye"
android:title="#string/title_eye"
app:showAsAction="always" />
<item
android:id="#+id/print"
android:icon="#drawable/ic_print"
android:title="#string/title_print"
app:showAsAction="always" />
<item android:id="#+id/help"
android:icon="#drawable/ic_help"
android:title="#string/title_help" />
This menu I add in activity
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.designer_options_menu, menu)
return true
}
And I don't understand how I can install ic_menu at the left side
You can use the Up/Home button for doing that by using a custom toolbar within a CoordinatorLayout:
1. Main layout
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<!-- My Layout -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
2. Your menu
<?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/folder"
android:icon="#drawable/ic_folder_black_24dp"
android:orderInCategory="1"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/file"
android:icon="#drawable/ic_insert_drive_file_black_24dp"
android:orderInCategory="2"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/eye"
android:icon="#drawable/ic_remove_red_eye_black_24dp"
android:orderInCategory="3"
android:title=""
app:showAsAction="always" />
<item
android:id="#+id/print"
android:icon="#drawable/ic_print_black_24dp"
android:orderInCategory="4"
android:title=""
app:showAsAction="always" />
</menu>
3. Java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_black_24dp);
getSupportActionBar().setTitle("");
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
break;
case R.id.eye:
Toast.makeText(this, "Eye", Toast.LENGTH_SHORT).show();
break;
case R.id.file:
Toast.makeText(this, "File", Toast.LENGTH_SHORT).show();
break;
case R.id.folder:
Toast.makeText(this, "Folder", Toast.LENGTH_SHORT).show();
break;
case R.id.print:
Toast.makeText(this, "Print", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
}
4. Style: use NoActionBar theme
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>
5. Gradle:: add design support library for coordinator layout
implementation 'com.android.support:design:28.0.0'
The result
Simpler Version
Add android:icon to your Main Activity or whatever Activity you want this icon:
<activity
android:name=".Main"
android:icon="#drawable/ic_settings_white_24dp"
android:launchMode="singleTop">
Enable the "home" icon in your Main.java:
getActionBar().setDisplayShowHomeEnabled( true ); // In your onCreate() or wherever.
You might also want to hide the back icon in the top left and the app name in the top left. You can do that with these two commands:
// Disable back icon in top left and hide app name.
getActionBar().setDisplayHomeAsUpEnabled( false );
getActionBar().setDisplayShowTitleEnabled( false );
To handle the click event you just need to capture the home in onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case android.R.id.home:
// Do something.
return true;
}
}
Worked for us and the end result is something like this:
With AppCompatActivity
You need to do this slightly differently if your Activity is extending from AppCompatActivity. Here are the changes to the steps above:
No need to add android:icon to AndroidManifest.xml.
Set the Icon to use in your Main.java Activity and use getSupportActionBar():
getSupportActionBar().setHomeAsUpIndicator( R.drawable.ic_settings_white_24dp );
Android 5.0 Material Design it seems impossible.
You can get that kind of effect using Bottom Navigation Tab.
Try this code hope it helps you
For Bottom Navigation Tab you have to add this line into dependency
compile ‘com.android.support:design:25.0.0’
Here is MainActivity.Java
public class MainActivity extends AppCompatActivity {
private BottomBar mBottomBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Notice how you don't use the setContentView method here! Just
// pass your layout to bottom bar, it will be taken care of.
// Everything will be just like you're used to.
mBottomBar = BottomBar.bind(this, R.layout.activity_main,
savedInstanceState);
mBottomBar.setItems(
new BottomBarTab(R.drawable.ic_recents, "Recents"),
new BottomBarTab(R.drawable.ic_favorites, "Favorites"),
new BottomBarTab(R.drawable.ic_nearby, "Nearby"),
new BottomBarTab(R.drawable.ic_friends, "Friends")
);
mBottomBar.setOnItemSelectedListener(new OnTabSelectedListener() {
#Override
public void onItemSelected(final int position) {
// the user selected a new tab
}
});
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mBottomBar.onSaveInstanceState(outState);
}
}
Design.xml
<?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">
<!-- Content Container -->
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#color/white"
app:itemTextColor="#color/white"
app:menu="#menu/bottom_navigation_main" />
</RelativeLayout>
Menu.xml
<?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/action_favorites"
android:enabled="true"
android:icon="#drawable/ic_favorite_white_24dp"
android:title="#string/text_favorites"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_schedules"
android:enabled="true"
android:icon="#drawable/ic_access_time_white_24dp"
android:title="#string/text_schedules"
app:showAsAction="ifRoom" />
<item
android:id="#+id/action_music"
android:enabled="true"
android:icon="#drawable/ic_audiotrack_white_24dp"
android:title="#string/text_music"
app:showAsAction="ifRoom" />
</menu>
Enable/Disable click state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/white" android:state_enabled="true" />
<item android:color="#color/colorPrimaryDark" android:state_enabled="false" />
</selector>
Handle click event using this
BottomNavigationView bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorites:
break;
case R.id.action_schedules:
break;
case R.id.action_music:
break;
}
return false;
}
});
Add view in layout
<View
android:layout_width="fill_parent"
android:layout_height="5dip"
android:background="#drawable/drop_shadow" >
</View>
add in your drawable folder drop_shadow.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#404040"
android:endColor="#F1F1F1"
android:angle="270"
>
</gradient>
</shape>
I am new to android app development and in the process of making my first app.
I am trying to create a bottom navigation bar. I have created the actual navigation bar but i am trying to now get it so when i click on that icon it will go to a different page. I currently have it so it will show some text saying you clicked on this button etc. but i am unsure how to make it so it will go to a different page.
This is the code i currently have
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
Toast.makeText(MainActivity.this, "Action Add Clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.action_exercise:
Toast.makeText(MainActivity .this, "Action Exercise Clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.action_timer:
Toast.makeText(MainActivity.this, "Action Timer Clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
});
}
}
This is my layout:
<?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"
xmlns:design="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.gymapp.HomePage">
<ImageView
android:id="#+id/imageView"
android:layout_width="318dp"
android:layout_height="198dp"
app:srcCompat="#drawable/getinshape"
tools:layout_editor_absoluteX="33dp"
tools:layout_editor_absoluteY="16dp"
tools:ignore="ContentDescription" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/colorPrimary"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:menu="#menu/bottom_nav_bar"
/>
</RelativeLayout>
This is also the code I'm using within the res/menu
<?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">
<item
android:id="#+id/action_add"
android:enabled="true"
android:icon="#drawable/ic_home"
android:title="Home"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_exercise"
android:enabled="true"
android:icon="#drawable/exercise"
android:title="Exercise"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/action_timer"
android:enabled="true"
android:icon="#drawable/ic_timer"
android:title="Timer"
android:showAsAction="ifRoom"
/>
</menu>
I have a drawer layout that needs to have two navigation views in it, because there's a couple of options that need to be aligned on top and another on the bottom, with a divider separating them.
I've managed to make it work, but now I have two different problems with my setup.
1) The first one, I can't change the selected items text color (the background color works as intented via a drawer selector, but for some reason, the selected item's text color is always colorPrimary)
2) The second one is trickier, since there's two navigation views, it can happen that there's a selected row on the first and on the second one, even though they all work loading fragments in the same container. Is there a way to manage that, when an item for the first navigation view is selected, the second navigation view's selected item gets deselected?
This is the drawer's layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 1º Layout de la activity -->
<include layout="#layout/activity_main"/>
<!-- 2º Layout del drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/container_navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemBackground="#drawable/drawer_list_selector"
android:nestedScrollingEnabled="true"
android:scrollIndicators="none">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:background="#color/colorAccent"
android:layout_above="#+id/navigation_view"
android:id="#+id/top_separator"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:layout_above="#+id/bottom_separator"
app:itemBackground="#drawable/drawer_list_selector"
app:menu="#menu/menu_navigation" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/divider_height"
android:background="#color/colorAccent"
android:layout_above="#+id/navigation_bottom"
android:id="#+id/bottom_separator"/>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:menu="#menu/menu_navigation_bottom"
app:itemBackground="#drawable/drawer_list_selector"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
This is the top navigation view's layout:
<?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">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_tus_ofertas"
android:title="#string/tus_ofertas"
android:icon="#drawable/ic_offer" />
<item
android:id="#+id/nav_movimiento_puntos"
android:title="#string/movimiento_puntos"
android:icon="#drawable/ic_points"/>
<item
android:id="#+id/nav_asociaciones"
android:title="#string/asociaciones"
android:icon="#drawable/ic_flag"/>
</group>
</menu>
Bottom navigation view's layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_terminos"
android:title="#string/terminos"
android:icon="#drawable/ic_document" />
<item
android:id="#+id/nav_contacto"
android:title="#string/contacto"
android:icon="#drawable/ic_email"/>
<item
android:id="#+id/nav_valorar"
android:title="#string/valorar"
android:icon="#drawable/ic_rate_review"/>
<item
android:id="#+id/nav_exit"
android:title="#string/exit"
android:icon="#drawable/ic_shutdown"/>
</group>
</menu>
Selected item's background selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/colorAccent" />
<item android:state_activated="true" android:drawable="#color/colorAccent" />
<item android:state_selected="true" android:drawable="#color/colorAccent" />
<item android:state_checked="true" android:drawable="#color/colorAccent" />
<item android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_activated="false" android:drawable="#android:color/transparent" />
<item android:state_selected="false" android:drawable="#android:color/transparent" />
<item android:state_checked="false" android:drawable="#android:color/transparent" />
</selector>
The activity that calls the drawer and handles the events:
public class MainActivity extends AppCompatActivity {
#Bind(R.id.toolbar) Toolbar toolbar;
#Bind(R.id.tabs) TabLayout tabs;
#Bind(R.id.drawer_layout) DrawerLayout drawerLayout;
#Bind(R.id.container_navigation) NavigationView containerNavigator;
#Bind(R.id.navigation_view) NavigationView navigationView;
#Bind(R.id.navigation_bottom) NavigationView navigationBottom;
#Bind(R.id.vsFooter) ViewStubCompat stub;
ImageView userPicture;
TextView userMail;
ViewPager viewPager;
Menu menu;
ActionBar actionBar;
ViewPagerFragment viewPagerFragment;
Usuario currentUser;
public Usuario getCurrentUser() {
return currentUser;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
ButterKnife.bind(this);
currentUser = UserApiManager.getInstance(getApplicationContext()).getCurrentUser();
viewPagerFragment = ViewPagerFragment.newInstance();
setupToolbar();
setupNavigationDrawer();
ApiManager.getInstance(getApplicationContext()).setupFooter(stub, currentUser.getIdLocalidad(), this);
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
}
public void setupViewPager(ViewPager viewPager) {
tabs.setupWithViewPager(viewPager);
}
private void setupToolbar() {
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar!=null){
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_black);
actionBar.setTitle(getString(R.string.app_title));
}
}
private void setupNavigationDrawer() {
View headerView = navigationView.inflateHeaderView(R.layout.navigation_header);
userPicture = (ImageView) headerView.findViewById(R.id.user_picture);
userMail = (TextView) headerView.findViewById(R.id.user_mail);
userMail.setText(currentUser.getEmail());
Picasso.with(this).load(currentUser.getImagenURL()).transform(new CircleTransformation()).into(userPicture);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_tus_ofertas:
showTabLayout();
actionBar.setTitle(getString(R.string.app_title));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,viewPagerFragment).commit();
break;
case R.id.nav_movimiento_puntos:
hideTabLayout();
Bundle bundle = new Bundle();
bundle.putString("idcliente", currentUser.getId());
MovimientoPuntosFragment movimientoPuntosFragment = MovimientoPuntosFragment.newInstance();
movimientoPuntosFragment.setArguments(bundle);
actionBar.setTitle(getString(R.string.movimiento_puntos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, movimientoPuntosFragment).commit();
break;
case R.id.nav_asociaciones:
hideTabLayout();
actionBar.setTitle(getString(R.string.asociaciones));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, AsociacionFragment.newInstance()).commit();
break;
}
drawerLayout.closeDrawers();
return true;
}
});
navigationBottom.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_terminos:
hideTabLayout();
actionBar.setTitle(getString(R.string.terminos));
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout, LegalesFragment.newInstance()).commit();
break;
case R.id.nav_contacto:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + getString(R.string.contacto_email));
intent.setData(data);
startActivity(intent);
break;
case R.id.nav_valorar:
final String appPackageName = BuildConfig.APPLICATION_ID;
try {
Log.i("url", "market://details?id=" + appPackageName);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
break;
case R.id.nav_exit:
break;
}
drawerLayout.closeDrawers();
return true;
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_over, menu);
this.menu = menu;
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
drawerLayout.openDrawer(GravityCompat.START);
break;
case R.id.action_user:
Intent intent = new Intent(this, PerfilUsuarioActivity.class);
startActivity(intent);
break;
}
return super.onOptionsItemSelected(item);
}
public void hideTabLayout(){
tabs.setVisibility(View.GONE);
}
public void showTabLayout(){
tabs.setVisibility(View.VISIBLE);
}
}
Any help would be appreciated!
Thanks in advance!
Solved!
The problem with the color I solved it with a selector, it didn't work before because I had a syntax error (selector tag inside the main selector tag).
This is the color selector's code:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FFFFFFFF" android:state_checked="true" />
<item android:color="#E6000000" android:state_checked="false"/>
</selector>
The second problem I solved it by programmatically deselecting the other navigationview's items onNavigationItemSelected.
This is the function I call:
public void deselectItems(NavigationView view){
int size = view.getMenu().size();
for (int i = 0; i < size; i++) {
view.getMenu().getItem(i).setChecked(false);
}
}
I know there must be a better way to solve the second problem, but this one worked.