Android Toolbar Menu Not Displaying on All Activities - java

I created a Toolbar and a "3-dot menu" that goes on the Toolbar. In my HomeActivity, the menu displays. However, in other activities, the menu doesn't (but the Toolbar does). I'm not sure what's happening here.
Here is the toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/Theme.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>
Here is the settings_menu.xml that should show the 3-dot 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/item2"
android:title="Item 2"
app:showAsAction="never" />
<item android:id="#+id/item3"
android:title="Item 3"
app:showAsAction="never">
<menu>
<item android:id="#+id/subitem1"
android:title="Sub Item 1"/>
<item android:id="#+id/subitem2"
android:title="Sub Item 2"/>
</menu>
</item>
</menu>
Here is the onCreate() method of HomeActivity, where the menu DOES appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// deactivate the fullscreen mode used by the splash screen
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.activity_home);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
tvClients = findViewById(R.id.tvClients);
tvClientList = findViewById(R.id.tvClientList);
tvClientName = findViewById(R.id.tvClientName);
buttonSettings = findViewById(R.id.buttonSettings);
buttonLogout = findViewById(R.id.buttonLogout);
buttonCreateNewClient = findViewById(R.id.buttonCreateNewClient);
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
And here is the onCreate() method of another activity, accessed through a button click in the HomeActivity, where the menu button does NOT appear.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_new_client);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Create New Client");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
etClientFirstName = findViewById(R.id.etClientFirstName);
etClientLastName = findViewById(R.id.etClientLastName);
etClientAge = findViewById(R.id.etClientAge);
buttonCreateClient = findViewById(R.id.buttonCreateClient);
buttonCreateClient.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createClient();
}
});
// Initialize Firebase Auth
mAuth = FirebaseAuth.getInstance();
}

you have to override the onCreateOptionsMenu in every activity you need menu to be shown within
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
// return true so that the menu pop up is opened
return true;
}
check this thread for full details

Related

How to solve navigation bar with navigation bottom problem

My question is that I was trying to make a simple app, and I used both navigation bar and bottom navigation in the same activity, but I am having a simple problem, such as When I run the app, I'm in home fragment, but when I drag the navigation bar and click on movies, it goes into movies fragment, and my movies item got checked, but the problem is when I again click on home in bottom navigation menu than in my navigation menu the movies item still become checked. Please tell how should I do that if I click on home in bottom navigation then in my navigation bar my movies item become unchecked.
Hope you will understand my problem
Here is my code:-
MainActivity : -
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth mAuth;
private DrawerLayout drawerLayout;
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.nav_home:
selectedFragment = new HomeFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
case R.id.nav_features:
selectedFragment = new FeaturesFragment();
break;
case R.id.nav_myMusic:
selectedFragment = new MyMusicFragment();
break;
}
assert selectedFragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
selectedFragment).commit();
return true;
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
drawerLayout = findViewById(R.id.Drawyer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
mAuth = FirebaseAuth.getInstance();
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
Fragment selections = null;
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
}
#Override
protected void onStart() {
super.onStart();
FirebaseUser currentuser;
currentuser = mAuth.getCurrentUser();
if (currentuser == null) {
SendUserToSelectTypeActivity();
}
}
private void SendUserToSelectTypeActivity() {
Intent testintent = new Intent(MainActivity.this, Selecttype.class);
testintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(testintent);
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
finish();
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_Movies:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new MoviesFragment()).commit();
break;
case R.id.nav_log_out:
SendUserToSelectTypeActivity();
mAuth.signOut();
break;
case R.id.nav_live:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container
, new LiveVideosFragment()).commit();
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
HomeFragment : -
public class HomeFragment extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home , container , false);
}
}
navigation menu:-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_Movies"
android:icon="#drawable/ic_movie"
android:title="Movies" />
<item
android:id="#+id/nav_live"
android:icon="#drawable/ic_movie"
android:title="Live Videos" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_share_black_24dp"
android:title="Share" />
<item
android:id="#+id/nav_contact"
android:icon="#drawable/ic_report_black_24dp"
android:title="Report us" />
<item
android:id="#+id/nav_log_out"
android:icon="#drawable/ic_arrow_back_black_24dp"
android:title="Log Out" />
</menu>
</item>
</menu>
activity main.xml:-
<?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:id="#+id/Drawyer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:fitsSystemWindows="true"
tools:context=".MainClasses.MainActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottom_navigation">
</FrameLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/design_default_color_primary_dark"
app:itemIconTint="#color/White"
app:itemTextColor="#color/White"
app:menu="#menu/bottom_navigation_menu" />
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/naviation_menu_main"
>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
Please answer me if you found the solution
Make one file in drawable like "bottom_icon_color.xml"
<?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/red" />
<item android:state_checked="false" android:color="#color/lightgray" />
</selector>
Then apply it to your bottom navigation
app:itemIconTint="#drawable/bottom_icon_color"
app:itemTextColor="#drawable/bottom_icon_color"
According to the problem you had mentioned in the question
you have to first store the previously clicked MenuItem and then make it as unchecked by using setCheckable(false) of MenuItem.
I'm assuming that the rest of all the functions are working, for example, check the item of NavigationView when the bottom navigation item is selected.
In your onNavigationItemSelected store, the clicked MenuItem in one variable say prevMenuItem of type MenuItem.
Now in your BottomNavigationView.OnNavigationItemSelectedListener() make this prevMenuItem as unchecked using this.
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.getMenu().findItem(previousMenuItem.getId()).setCheckable(false);

Navigation Drawer is not clickable [duplicate]

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.

Menu action items goes to overflow menu

You will have to forgive me, because I am still pretty new to android development. For my android app, I am staying to create a custom menu and place on the tool bar with the java code in a base activity that is extended by the other activities. But for some reason, the menu buttons are hiding in the overflow menu even though there is more than enough room for them to be showing. I feel like my code is correct, but I need to add a reference to something somewhere else that I am missing. Thanks in advance!
Current toolbar appearance
BaseActivity.java
public abstract class BaseActivity extends AppCompatActivity {
private Toolbar toolBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResource());
toolBar = (Toolbar) this.findViewById(R.id.toolbar);
if (toolBar != null) {
setSupportActionBar(toolBar);
if (getSupportActionBar() != null) {
setDisplayHomeEnabled(true);
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
protected abstract int getLayoutResource();
public void setDisplayHomeEnabled(boolean b) {
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(b);
}
}
#Override
public void setTitle(CharSequence title) {
toolBar.setTitle(title);
}
#Override
public void setTitle(int titleId) {
toolBar.setTitle(titleId);
}
public Toolbar getToolBar() {
return toolBar;
}
}
toolbar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:titleTextColor="#android:color/white"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
</LinearLayout>
main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/menu_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="always"/>
<item
android:id="#+id/menu_search"
android:title="Search"
android:showAsAction="always"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>
In your main_menu.xml file, add an 'app' tag for showAsAction as I have done below. It worked for me. See if that works for you too.
<?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/menu_settings"
android:title="Settings"
android:icon="#android:drawable/ic_menu_preferences"
app:showAsAction="always"/>
<item
android:id="#+id/menu_search"
android:title="Search"
app:showAsAction="always"
android:icon="#android:drawable/ic_menu_search"
android:actionViewClass="android.widget.SearchView"/>
</menu>

Unable to show icon on toolbar

I tried all the solutions but still unable to show icon on toolbar, in my main project I have a toolbar and under it toolbar and inside toolbar, fragments, no matter what I tried I'm unable to show icon on toolbar, here is mainActivity.java which extends AppCompatActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_act_main_screen);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ActionBar ab = getSupportActionBar();
ab.setTitle("Title");
tabs = (TabLayout) findViewById(R.id.tabs);
tabs.addTab(tabs.newTab().setText("Today"),true);
tabs.addTab(tabs.newTab().setText("Settings"));
tabs.setOnTabSelectedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="address">
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
app:showAsAction="always"
//android:showAsAction="always" tried this as well
android:icon="#drawable/share"/>
mainactivity.xml
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/toolbarColor"
android:elevation="3dp">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"
/>
Nothing is working, not sure abou how to implement custom namespace but I changed name from app to custom but didn't work also there is enough space but still doesn't show.
I always do this for toolbar's title and icon :
Because I don't have access to a computer right now , I cant post a complete sample but I'll describe :
First set your toolbar height like this : layout_height="android:?attr/actionBarSize" then in the android.support.v7.widget.Toolbar tag put a LinearLayout with these attributes : android:layout_width="match_parent and android:layout_height="match_parent" and android:gravity="center_vertical|left" then put an ImageView in the LinearLayout with these attributes:
width="wrap_content"
height="match_parent"
scaleType="fitXY"
adjustviewbounds="true"
src="#drawable/icon"
if you want to add a title just add a text view after adding the ImageView.
On your activity class you need this :
#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, menu);
return true;
}
Try to use both
android:showAsAction="always"
app:showAsAction="always"
Ignore the warning that you should use app:showAsAction

Creating the Sub Menu in Navigation Drawer in Android

I have created the Navigation Drawer using the below link which is very nice :
http://blog.teamtreehouse.com/add-navigation-drawer-android
You can download the Navigation Drawer from this URl -
https://github.com/sanjaisy/Android-Navigation-Drawer.git
Now I want to add Submenu to this navigation drawer.Please help me with a solution.
Here is my Full java Code
public class SmoothBanlanceHome extends ActionBarActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smooth_banlance_home);
mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
String[] MenuArray = getResources().getStringArray(R.array.Naviagation_Menu_List);
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MenuArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(SmoothBanlanceHome.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
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("Menu");
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);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}}
Here is the layout code
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
The drawer is given a fixed width in dp and extends the full height of
the container. A solid background is used for contrast
with the content view. -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Please can anyone tell me how can I make Sub menu with this code. You can also download the full Navigation Working code from git url as I have mention above.
Here is the picture which i want
You should use NavigationView from Android Support Design Library instead of this NavigationDrawer.
Check this official sample:
https://github.com/chrisbanes/cheesesquare
It's quite easier.
Using Android Suppor Design Library you will create your submenus like this:
<?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_home"
android:icon="#drawable/ic_event"
android:title="Home" />
<item
android:id="#+id/nav_profile"
android:icon="#drawable/ic_dashboard"
android:title="Perfil" />
</group>
<item android:title="More Options">
<menu>
<item
android:icon="#drawable/ic_forum"
android:title="Forum" />
<item
android:icon="#drawable/ic_headset"
android:title="Headset" />
</menu>
</item>
</menu>
Best regards.

Categories