Click on navigation does not open the activity - android

I'm working on bottom navigation. The problem is that, when I click on one icon on the navigation bar, it is open the activity I want but the icon is appeared it doesn't clicked it just by default the first icon appear as it clicked all the time
public class MainActivity
extends AppCompatActivity
implements
BottomNavigationView.OnNavigationItemSelectedListener {
BottomNavigationView nav;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nav = (BottomNavigationView) findViewById(R.id.navigation);
nav.setOnNavigationItemSelectedListener(this);
} // ------------------end of on create--------------------
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.prof:
Intent intent = new Intent(MainActivity.this,
profile.class);
menuItem.setIcon(R.drawable.profile);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent);
break;
case R.id.hm:
Intent intent1 = new Intent(MainActivity.this,
MainActivity.class);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent1);
break;
case R.id.consult:
Intent intent3 = new Intent(MainActivity.this, tabs.class);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent3);
break;
case R.id.dash:
Intent intent2 = new Intent(MainActivity.this,
Dashboard.class);
nav.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
startActivity(intent2);
break;
}
return false;
}
}
my main activity layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/purple"
app:itemIconTint="#drawable/nav_item_color_state"
app:itemTextColor="#drawable/nav_item_color_state"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
my navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/hm"
android:icon="#drawable/hom"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
<item
android:id="#+id/prof"
android:icon="#drawable/profile"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
<item
android:id="#+id/consult"
android:icon="#drawable/consult"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
<item
android:id="#+id/dash"
android:icon="#drawable/dashboard"
android:enabled="true"
app:showAsAction="ifRoom"
tools:ignore="MenuTitle" />
</menu>
this my nav_item_color_state.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/white"
android:state_enabled="true"/>
<item android:color="#color/colorPrimaryDark"
android:state_enabled="false"/>
</selector>
this my interface in all my activity
enter image description here
please help me I update the question

You don't bind any onNavigationItemSelected() listener there; for example:
nav = (BottomNavigationView) findViewById(R.id.navigation);
nav.setNavigationItemSelectedListener(this);

Related

how to set menu item title in java File?

curently done: menu working fine.
i want: i want to set menu title in MainActivity.java
attched: 1. activitymain.xml 2. MAinActivity.java 3. menu_nevigation.xml
can i set menu item title in MainActivity.java because i want to change title in some conditions how to settitle in java file...
activityMain.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:textSize="50sp"
android:textStyle="bold"
android:layout_centerInParent="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/bottom_navigation"
app:itemBackground="#color/colorcustom"
app:itemTextColor="#drawable/selector"
app:itemIconTint="#drawable/selector"
app:menu="#menu/menu_navigation"
app:labelVisibilityMode="labeled"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize And Assign Variable
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
//Set Home Selected
bottomNavigationView.setSelectedItemId(R.id.home);
//Perform ItemSelected
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.about:
startActivity(new Intent(getApplicationContext()
,about.class));
overridePendingTransition(0,0);
return true;
case R.id.home:
return true;
case R.id.term:
startActivity(new Intent(getApplicationContext()
,term.class));
overridePendingTransition(0,0);
return true;
case R.id.test:
startActivity(new Intent(getApplicationContext()
,test.class));
overridePendingTransition(0,0);
return true;
case R.id.setting:
startActivity(new Intent(getApplicationContext()
,setting.class));
overridePendingTransition(0,0);
return true;
}
return false;
}
});
}
}
menu_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_home"/>
<item
android:id="#+id/about"
android:title="About"
android:icon="#drawable/ic_about"/>
<item
android:id="#+id/test"
android:title="Test"
android:icon="#drawable/ic_test"/>
<item
android:id="#+id/term"
android:title="Term"
android:icon="#drawable/ic_term"/>
<item
android:id="#+id/setting"
android:title="Setting"
android:icon="#drawable/ic_setting"/>
</menu>
Put this anywhere in your MainActivity
invalidateOptionsMenu();
Override
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item = menu.findItem(R.id.home);
if (item.getTitle().equals("Home")) {
item.setTitle("Your new title"); }
return super.onPrepareOptionsMenu(menu);
}

Android : BottomNavigationView icons are not visible

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

Android how to do the upward shadows

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>

Android Bottom Navigation Bar

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>

Drawer layout with two navigation views and selected item colours

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.

Categories

Resources