onNavigationItemSelected not being called [duplicate] - android

I am trying to use NavigationView from Android Support Design library in my app. For some reason, OnNavigationItemSelected listener is not being called. Here is my code
Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<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"
android:fitsSystemWindows="true">
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer_menu" />
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.DrawerLayout>
Activity onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutID());
toolbar = (Toolbar) findViewById(R.id.activity_toolbar);
setSupportActionBar(toolbar);
toolbar.inflateMenu(R.menu.common_menu);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeAsUpIndicator(R.drawable.ic_menu_white_24dp);
actionBar.setDisplayHomeAsUpEnabled(true);
}
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
Snackbar.make(contentLayout, menuItem.getTitle() + " pressed", Snackbar.LENGTH_LONG).show();
menuItem.setChecked(true);
// allow some time after closing the drawer before performing real navigation
// so the user can see what is happening
drawerLayout.closeDrawer(GravityCompat.START);
mDrawerActionHandler.postDelayed(new Runnable() {
#Override
public void run() {
navigate(menuItem.getItemId());
}
}, DRAWER_CLOSE_DELAY_MS);
drawerLayout.closeDrawers();
return true;
}
});
usernameTextView = (TextView) findViewById(R.id.drawer_header_username);
usernameTextView.setText(getAppDContext().getAccount().getUsername());
}

When you make XML layout, you should write down NavigationView after BaseLayout (FrameLayout, LinearLayout, etc..)
<DrawerLayout>
<FrameLayout />
<NavigationView />
</DrawerLayout>

For me this did the trick!
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.bringToFront();

Your activity main layout should look like this:
<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/navigationDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/activity_main_content" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
style="#style/NavigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
app:headerLayout="#layout/header"
app:menu="#menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>
In this NavigationView I linked header.xml and menu_drawer.xml (from menu folder)
for example menu_drawer.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav1"
android:checked="true"
android:icon="#drawable/logo"
android:title="Navigation item 1"/>
<item
android:id="#+id/nav2"
android:icon="#drawable/logo"
android:title="Navigation item 2"/>
</group>
</menu>
than your java code:
public class ActivityMain extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpToolbar();
setUpNavDrawer();
}
private void setUpNavDrawer() {
NavigationView view = (NavigationView) findViewById(R.id.navigationView);
mDrawerLayout = (DrawerLayout) findViewById(R.id.navigationDrawer);
view.setNavigationItemSelectedListener(this);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawerOpen, R.string.drawerClose);
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
Check if this work for you. In my project works like a charm.

Related

Always getting the error messsage: No drawer view found with gravity LEFT

I'm working on an app but I have some troubles with drawer layout. I always getting the error message like I mentioned in the title but I have no such Gravity with LEFT value. All I have are gravities with END values and that's all.
Here is my Java code:
public class HomeActivity extends AppCompatActivity {
private ActionBarDrawerToggle toggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar mainToolBar =findViewById(R.id.main_page_toolbar);
DrawerLayout mainDrawerLayout = findViewById(R.id.main_page_drawer_layout);
NavigationView mainNavigationView = findViewById(R.id.main_page_navigation_view);
setSupportActionBar(mainToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setHomeButtonEnabled(true);
toggle = new ActionBarDrawerToggle(this,mainDrawerLayout,mainToolBar,R.string.openDrawer,R.string.closeDrawer);
mainDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
}
And here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:openDrawer="end"
tools:context=".Activities.HomeActivity"
android:id="#+id/main_page_drawer_layout"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/home_main_page"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
app:headerLayout="#layout/home_side_menu_header_layout"
app:menu="#menu/home_side_menu"
android:layoutDirection="rtl"
android:id="#+id/main_page_navigation_view"/>
You need to handle navigation click on Toolbar like below:
mainToolBar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mainDrawerLayout.isDrawerOpen(GravityCompat.END))
mainDrawerLayout.closeDrawer(GravityCompat.END);
else
mainDrawerLayout.openDrawer(GravityCompat.END);
}
});
Also don't forgot to close drawer whenever needed like below:
mainDrawerLayout.closeDrawer(GravityCompat.END)

Can't add a listener to my items in NavigationVIew

I have watched a lot of solutions but I'm in trouble with my NavigationView. I created it and tried to use it but somehow it doesn't react. This is the code I used.
Thank you !
This is my XML for "drawermenu"
<?xml version="1.0" encoding="utf-8"?>
<item
android:id="#+id/add"
android:icon="#drawable/ic_add_circle_black_24px"
android:title="Добави автомобил" />
<item
android:id="#+id/settings"
android:icon="#drawable/ic_build_black_24px"
android:title="Настройки" />
<item
android:id="#+id/export"
android:icon="#drawable/ic_importexport"
android:title="Експорт на инф."
/>
<item
android:id="#+id/about"
android:icon="#drawable/ic_info_outline_black_24px"
android:title="За програмиста" />
<item
android:id="#+id/exit"
android:icon="#drawable/ic_exit_to_app_black_24px"
android:iconTintMode="src_in"
android:title="Изход" />
</group>
This is all I used for NavigationView
<android.support.v4.widget.DrawerLayout 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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/myYellow"
android:paddingRight="10dp"
android:theme="#style/AppTheme2"
tools:context="com.example.hpdemon.diploma.MainForm"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/flcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/myYellow"
android:fitsSystemWindows="true"
app:headerLayout="#layout/header"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#56733b"
app:menu="#menu/drawermenu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And this is the code from MainActivity. I call the Drawer function in onCreate method
public void Drawer() {
NavigationView navigationView = findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
// close drawer when item is tapped
int id = menuItem.getItemId();
if (id == R.id.add) {
//Handle your stuff here
Toast.makeText(MainForm.this, "Ще проработя ;) ", Toast.LENGTH_LONG).show();
}
mDrawerLayout.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
This is for the mToggle element
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);

How to create an android drawer with custom icons on it?

Can anyone please help me on designing this android layout? I want to put two icons on the drawer toolbar like on the image below.
This is LeftNavigationActivity.class
public class LeftNavigationActivity extends BaseActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ImageView profilePic;
Session session;
FragmentManager fragmentManager = getSupportFragmentManager();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_left_navigation);
this.setTitle("Home");
session = new Session(this);
if (!session.loggedIn()) {
logOut();
}
View v = getLayoutInflater().inflate(R.layout.nav_header_left_navigation,
null);
TextView username = (TextView) v.findViewById(R.id.username);
Intent intent = getIntent();
String uname = intent.getStringExtra("username");
Toast.makeText(this, "Welcome " + uname, Toast.LENGTH_SHORT).show();
username.setText(uname);
LoansAndDebtsFragment loansAndDebtsFragment = new LoansAndDebtsFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragment,
loansAndDebtsFragment, loansAndDebtsFragment.getTag()).commit();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
FragmentManager fragmentManager = this.getSupportFragmentManager();
int stackCount = fragmentManager.getBackStackEntryCount();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else if (fragmentManager.getFragments() != null) {
LoansAndDebtsFragment loansAndDebtsFragment = new
LoansAndDebtsFragment();
fragmentManager.beginTransaction().replace(R.id.layout_for_fragment,
loansAndDebtsFragment, loansAndDebtsFragment.getTag()).commit();
}
}
The LeftNavigationActivity is the BaseDrawerActivity which contains the navigation menu.
This is app_bar_left_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<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="com.gebeya.jo.pyf.activities.LeftNavigationActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.NoToolBarWithABrandTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorWhite"
app:titleTextColor="#color/colorBrand1"
app:popupTheme="#style/AppTheme.NoToolBarWithABrandTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_left_navigation" />
</android.support.design.widget.CoordinatorLayout>
This is nav_header_left_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#drawable/nav_background"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/eyasu"
android:layout_marginLeft="20dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
app:civ_border_color="#color/colorBrand1"
app:civ_border_width="4dp"
android:layout_alignParentStart="true"
android:paddingBottom="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/userFullName"
android:textSize="14sp"
android:textColor="#FFF"
android:textStyle="bold"
android:gravity="left"
android:paddingBottom="4dp"
android:id="#+id/username"
android:layout_above="#+id/email"
android:layout_alignLeft="#+id/profile_image"
android:layout_alignStart="#+id/profile_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/email"
android:hint="#string/userEmail"
android:gravity="left"
android:layout_marginBottom="8dp"
android:textSize="14sp"
android:textColor="#fff"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/username"
android:layout_alignStart="#+id/username" />
</RelativeLayout>
Open Android Studio
Start a new Android Studio project
Select Navigation Drawer Activity
Pick your icons here: https://material.io/icons/
1. Create an menu XML with your desired menu items and put this XML into res/menu/ folder.
menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.ferdous.advancefilter.MainActivity">
<item
android:id="#+id/action_notification"
android:title="Notification"
android:icon="#drawable/ic_action_notification"
app:showAsAction="always" />
<item
android:id="#+id/action_warning"
android:title="Notification"
android:icon="#drawable/ic_action_warning"
app:showAsAction="always" />
</menu>
2. In your Activity, override method onCreateOptionsMenu() to add menu items to Toolbar and override method onOptionsItemSelected() to handle the Toolbar item click event.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_notification:
// Do something
break;
case R.id.action_warning:
// Do something
break;
}
return super.onOptionsItemSelected(item);
}
OUTPUT:
#. If you want to add count badge over your menu item like below then you can follow my another answer Notification Badge On Action Item Android. I have added detail steps and codes.
Hope this will help~

Navigation Drawer blocks other components

I want to add Navigation Drawer to my android application.
I followed this tutorial
I successfully added the navigation drawer.
The problem is when I add the component, the other buttons and components be inactive. Like three dot in action bar, it does not list the items. When I set visiblitiy of Drawer layout as gone, then three dot in action bar is working.
Here is my code:
public class MainActivity extends AppCompatActivity {
public android.support.v7.widget.Toolbar toolbar;
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private String mActivityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView) findViewById(R.id.navList);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.app_bar_id);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
This is my content_main.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.hale.myapplication.MainActivity"
tools:showIn="#layout/activity_main">
<include layout="#layout/app_bar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
This is app_bar.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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.hale.myapplication.MainActivity"
tools:showIn="#layout/activity_main">
<include layout="#layout/app_bar"/>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<ListView
android:id="#+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Here is screenshot. The three dots is near the title
Thanks in advance.
first change you theme into something like this
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base">
<!-- Customize your theme here. -->
</style>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<item name="colorControlHighlight">#color/colorAccent</item>
<item name="toolbarStyle">#style/Toolbar</item>
<item name="android:windowBackground">#android:color/white</item>
</style>
<style name="Toolbar" parent="Base.Widget.AppCompat.Toolbar">
<item name="android:background">?attr/colorPrimary</item>
<item name="popupTheme">#style/Theme.AppCompat.Light</item>
</style>
this is how i used my main content layout, where you see a drawerLayout and it has LinearLayout, inside this i have a frameLayout where i add content to show. you should use instead NavigationViewer instead of ListView as google suggests doing it.
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- this is your ActionBar layout -->
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<!-- Your content goes inside this layout -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</LinearLayout>
<!-- you should use such NavigatinView instead of ListView -->
<android.support.design.widget.NavigationView
android:id="#+id/navigation.view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/navigation_profile"
app:itemBackground="#xml/activated_navigation_menu_item"
app:menu="#menu/navigation_menu"/>

Refresh NavigationView menu after change locale

I'm changing locale programmatically from fragment and everything ok. There is one exception, the NavigationView menu, where language didn't changing. How to refresh it without recreate all activity.
Mainactivity code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.setEnabled(false);
DrawerLayout mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="#layout/app_bar_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="#+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main" app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
fragment code after choose locale
LocaleHelper.setLocale(getActivity(), getResources().getStringArray(R.array.locale)[locale]);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
<group android:checkableBehavior="single">
<item android:id="#+id/nav_contacts" android:icon="#drawable/ic_people_black_24dp"
android:title="#string/contacts" android:checked="true"/>
<item android:id="#+id/nav_invoices" android:icon="#drawable/ic_swap_horiz_black_24dp"
android:title="#string/invoices" />
<item android:id="#+id/nav_payments" android:icon="#drawable/ic_payment_black_24dp"
android:title="#string/payments" />
<item android:id="#+id/nav_history" android:icon="#drawable/ic_receipt_black_24dp"
android:title="#string/history" />
<item android:id="#+id/nav_settings" android:icon="#drawable/ic_settings_black_24dp"
android:title="#string/settings" />
<item android:id="#+id/nav_info"
android:title="#string/info" android:icon="#android:drawable/ic_dialog_info"/>
</group>
Thanx
I've decided this problem
private void refreshNavigationView(){
getActivity().setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) getActivity().findViewById(R.id.nav_view);
}
That's enought for refresh NavigationView after change locale
But I lose onNavigationItemSelected
As a result at Mainactivity
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the locale has changed
if (!currentLocale.equals(newConfig.locale)) {
currentLocale = newConfig.locale;
this.setContentView(R.layout.activity_main);
NavigationView navigationView = (NavigationView) this.findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(MainActivity.this);
}
}
At fragment
getActivity().onConfigurationChanged(getActivity().getResources().getConfiguration());
You can try below code. I haven't tested it but I hope it will work.
for (int i = 0, count = navigationView.getChildCount(); i < count; i++) {
final View child = navigationView.getChildAt(i);
if (child != null && child instanceof ListView) {
final ListView menuView = (ListView) child;
final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter();
final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter();
wrapped.notifyDataSetChanged();
}
}
latest solution in kotlin:
upgrade your appcompat version to 1.6.0 or later
implementation 'androidx.appcompat:appcompat:1.6.0'
create a function
fun setLocale(language: String?) {
val appLocale: LocaleListCompat = LocaleListCompat.forLanguageTags(language)
AppCompatDelegate.setApplicationLocales(appLocale)
}
then use it like
setLocale("en-EN")
it will update and refresh all language change/locale including NavigationView

Categories

Resources