I tried setting the menu items in my app as actions with app:showAsAction="always". This works on a phone and also on the tablet when I use the same layout.
But when I use a two-pane layout, the items don't show up in the action bar but in the overflow menu even though there is plenty of room.
Here is the menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/menu_item_maps"
android:icon="#drawable/ic_action_place"
android:orderInCategory="20"
android:title="#string/maps_menu_item"
app:showAsAction="always">
</item>
<item
android:id="#+id/menu_item_event"
android:icon="#drawable/ic_action_event"
android:orderInCategory="30"
android:title="#string/menu_item_event"
app:showAsAction="always">
</item>
<item
android:id="#+id/menu_item_share_gig"
android:orderInCategory="100"
android:title="#string/menu_item_share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="always">
</item>
</menu>
Here is the two-pane layout:
<LinearLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:baselineAligned="false"
android:divider="?android:attr/dividerHorizontal"
android:orientation="horizontal"
android:showDividers="middle"
>
<fragment
android:id="#+id/gig_list"
android:name="de.nobodyknows.app.GigListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="#+id/gig_detail_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
</LinearLayout>
In my code I use the callback method to replace the FrameLayout with a detail fragment:
/**
* Callback method from {#link GigListFragment.Callbacks} indicating that
* the item with the given ID was selected.
*/
#Override
public void onItemSelected(Long id) {
if (mTwoPane) {
// In two-pane mode, show the detail view in this activity by
// adding or replacing the detail fragment using a
// fragment transaction.
Bundle arguments = new Bundle();
arguments.putLong(GigDetailFragment.GIG_ID, id);
GigDetailFragment fragment = new GigDetailFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction()
.replace(R.id.gig_detail_container, fragment).commit();
} else {
// In single-pane mode, simply start the detail activity
// for the selected item ID.
Intent detailIntent = new Intent(this, GigDetailActivity.class);
detailIntent.putExtra(GigDetailFragment.GIG_ID, id);
startActivity(detailIntent);
}
}
The detail Fragment inflates the options menu that was shown above:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detail, menu);
}
Now these menu items show up fine in the phone layout when a new acticity is started. But when the fragment replaces the FrameLayout in the first activity, the menu items just show up in the overflow menu. I can't figure out why that happens, though. Has it something to do with the support library? Or is it something special about how fragments work?
Thanks for your help.
Okay, I found the solution.
My Activity did not extend theActionBarActivityand therefore was not correctly set up to work with MenuItemCompat.
The reason why it still worked on the phone was because that was in a different Activity that correctly extended ActionBarActivity.
To correct this I just replaced the superclass of the Activity that acts as the two-pane Activity to be ActionBarActivity.
Related
I created a navigation bar and ,I didn't understand how to create an ID for each fragment in grph.
navigation graph:
<navigation 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/nav_graph"
app:startDestination="#id/nav_home">
<fragment
android:id="#+id/nav_home"
android:name="com.mordechay.yemotapp.HomeFragment"
android:label="fragment_home"
tools:layout="#layout/fragment_home" >
</fragment>
<fragment
android:id="#id/nav_explorer"
android:name="com.mordechay.yemotapp.filseExplorerFragment"
android:label="fragment_filse_explorer"
tools:layout="#layout/fragment_filse_explorer" />
</navigation>
in the activity:
<fragment
android:id="#+id/nvgv_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph"
/>
And I wrote in the activity:
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_explorer);
It comes out null, what do I need to write to get the fragment?
I tried to get the fragment by writing the ID of the fragment found in the navigation graph.
But I get null.
You've already set your id's => android:id="#+id/nav_home" and android:id="#+id/nav_explorer".
At first, you need to declare your navigation controller.
val navigationController = findNavController(R.id.nvgv_fragment)
then you can check destination fragment with listener
navigationController.addOnDestinationChangedListener { _, destination, _ ->
if (destination.id == R.id.nav_explorer) {
// whatever you want to do
}
}
But, if you only need an instance of displayed fragment, you can find the answer here:
Current fragment instance
I'm new to Android / Kotlin and I have an application with multiple activities.
So far I have created all the activities needed and the app works fine except for the fact now I need to add a Navigation Drawer.
Coming from iOS, I designed all my layouts thinking I can add the Navigation Drawer after:
<?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"
android:background="#color/backgroundColor"
android:backgroundTint="#color/backgroundColor">
<RelativeLayout
android:id="#+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="44dp"
android:background="#drawable/navbar_border"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="#+id/ivNavBarLogo"
android:layout_width="102dp"
android:layout_height="24dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/empty_description"
app:srcCompat="#mipmap/navbar_logo"/>
<ImageButton
android:id="#+id/btnSideMenu"
android:layout_width="25dp"
android:layout_height="15dp"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"
android:background="#mipmap/sidemenu_button"
android:contentDescription="#string/empty_description"
app:srcCompat="#mipmap/sidemenu_button"/>
</RelativeLayout>
<!-- STUFF TO BE ADDED THERE -->
</android.support.constraint.ConstraintLayout>
My intention was to create a NavigationDrawer activity and call it when tapping the btnSideMenu on each activity.
However, I've been trying to get around this for a few days now without any luck. I never thought such a basic feature could be so hard to implement. I tried almost every suggestion and example found here, and also this: https://developer.android.com/training/implementing-navigation/nav-drawer.html
Am I thinking this issue the wrong way? Is there any way of doing this without having to recreate all my activities based on the template provided by Android Studio or using fragments?
LE: Forgot to mention that I removed the ActionBar from all activities.
To add a navigation drawer to multiple activities without going through and re-writing every activity's layout, you can leverage a base activity class and a ViewStub tag.
Create a layout like this (I'll refer to it as navdrawer_activity.xml later):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<ViewStub
android:id="#+id/navdrawer_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!--
your drawer here
you can use android.support.design.widget.NavigationView
or some other view if you want
-->
</android.support.v4.widget.DrawerLayout>
Then create a class for all of your activities to extend:
public class NavDrawerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.navdrawer_activity);
}
#Override
public final void setContentView(#LayoutRes int layoutResID) {
ViewStub stub = findViewById(R.id.navdrawer_stub);
stub.setLayoutResource(layoutResID);
stub.inflate();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// set up nav drawer as normal, using whatever
// callback methods or helper methods you want
}
}
With these in place, the only thing you have to do is change any activity where you want the nav drawer to extend NavDrawerActivity instead of AppCompatActivity.
Well as the guide you included says:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
<!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
<content goes here
... />
<!-- Container for contents of drawer -->
<left pane of drawer goes here
...
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
You could define the left pane of the drawer as a compound view, and the content as a compound view. Kinda like this:
your_view.xml
<YourView 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">
<Button android:id="#+id/..."
android:layout_width="..."
android:layout_height="..."
android:layout_centerInParent="true"/>
</YourView>
And
public class YourView extends RelativeLayout {
public YourView(Context context) {
super(context);
}
public YourView(Context context, AttributeSet attrs) {
super(context, attrs);
}
// ... two more constructors are possible here
private Button button;
#Override
public void onFinishInflate() {
super.onFinishInflate();
button = findViewById(R.id.button);
button.setOnClickListener((view) -> {
...
}
}
}
And you can do the same thing for the view of the left pane. Make sure you include the android:layout_gravity="start" as an attribute of <YourLeftPane in its xml.
And now you can do in your activity_whatever.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
<include layout="#layout/your_view"/>
<include layout="#layout/your_left_pane" />
</android.support.v4.widget.DrawerLayout>
I'm using android SearchView to have a search button in my action bar. I followed this tutorial from Google Developers on YouTube, and added a few tweaks based on my own needs. However, whenever I exit the search field that comes up when clicking the button, the search button disappears!
Here is my onCreateOptionsMenu and onOptionsItemSelected functions
onCreateOptionsMenu:
#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;
}
onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(final 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_settings:
// TODO: settings activity
break;
case R.id.action_logout:
FirebaseAuth.getInstance().signOut();
break;
case R.id.action_search:
// Initialize everything here, because it produces a NullPointerException if initialized in onCreateOptionsMenu
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return false;
}
#Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
break;
}
return super.onOptionsItemSelected(item);
}
And here's my menu_main.xml layout file
<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.magnus.inline.MainActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never" />
<item
android:id="#+id/action_logout"
android:orderInCategory="200"
android:title="#string/action_logout"
app:showAsAction="never" />
And searchable.xml layout file
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</searchable>
Images
EDIT:
I noticed that the only time the search button disappears is when I've pressed the options button while in "search mode"(see the last image in link). I have a feeling that when the options menu is displayed, the "action_search" is somehow converted to a options menu item, instead of an action bar item (?). For anyone asking, whenever I try to call MenuItemCompat.getActionView() of a searchview MenuItem in the onCreateOptionsMenu function, my app just crashes and says that it tries to perform it on a null object reference.
Here's my activity_main.xml layout file
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.magnus.inline.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/main_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AlertDialog.AppCompat.Light"
android:background="#color/colorPrimaryDark" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Instead of
app:showAsAction="ifRoom|collapseActionView"
Change to
app:showAsAction="always|collapseActionView
Hello member stackoverflow
porblem with bottomnavihationview
in my app I used BottomNavigationView with 4 item . it make my app easy and beauty
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.action_one:
break;
case R.id.action_two:
FragmentTransaction manger= getSupportFragmentManager().beginTransaction();
pop_web_view pop3 =new pop_web_view();
pop3.show(manger,null);
break;
case R.id.action_three:
break;
case R.id.action_four:
break;
}
return false;
}
});
in activity_main :
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_anchorGravity="bottom"
android:paddingTop="560dp"
app:itemBackground="#color/colorDivider"
app:itemIconTint="#color/colorPrimaryDark"
app:itemTextColor="#color/colorPrimaryDark"
app:menu="#menu/menu_bottom_navigation" />
in menu xml :
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_one"
android:icon="#android:drawable/ic_secure"
android:title="One"/>
<item
android:id="#+id/action_two"
android:icon="#android:drawable/ic_dialog_info"
android:title="Two"/>
<item
android:id="#+id/action_three"
android:icon="#android:drawable/ic_dialog_email"
android:title="Three"/>
<item
android:id="#+id/action_four"
android:icon="#android:drawable/ic_popup_reminder"
android:title="Four"/>
</menu>
BUT I have problem Caused by:
java.lang.IllegalArgumentException: Maximum number of items supported by BottomNavigationView is 5. Limit can be checked with BottomNavigationView#getMaxItemCount()
The error said that maximum number of items supported by BottomNavigationView is 5.
And try to delete
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);
because you are already inflating it in
app:menu="#menu/menu_bottom_navigation"
And you are modifying it by calling
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);
The documentation says Existing items in the menu will not be modified or removed.
Check this documention
And check this answer
Implementation of BottomNavigationView has condition: when there is more than 3 items then use shift mode.
I know you accepted the current answer, but it's incomplete.
You added a menu in your XML to the BottomNavigationView, then you try to call inflateMenu(...), however, the documentation clearly says:
Existing items in the menu will not be modified or removed.
This means that you are adding menu items to this view, not replacing them.
What you can do to fix it is the following:
bottomNavigationView.getMenu().clear();
bottomNavigationView.inflateMenu(R.menu.menu_bottom_navigation);
Also it's worth mentioning that you do the same things twice: once in the XML (by adding app:menu="..." attribute to the layout item), once in the Java class by calling the inflateMenu(...) method. Delete either of them and it will work. Keep in mind though that if you want to change the menu items later dynamically, you'll need to clear the existing items the way I posted.
In my app I used BottomNavigationView with 2 item, but there are 4 items like this:
My XML:
<?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:id="#+id/bottom_navigation_constraintlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
tools:context=".BottomNavigation">
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#android:color/holo_red_dark"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
and my code:
public class BottomNavigation extends AppCompatActivity {
private static final String TAG = "BottomNavigation";
#Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
updateMenu();
} catch (Exception e) {
Log.e(TAG, "onCreate: ", e);;
}
}
#UiThread
private void updateMenu() {
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
if (bottomNavigationView != null) {
bottomNavigationView.inflateMenu(R.menu.navigation);
for (Integer i = 0;
i < bottomNavigationView.getMenu().size();
++i ) {
Log.i(TAG, "updateMenu: " + bottomNavigationView.getMenu().getItem(i).getItemId());
}
}
}
}
Delete either of them and it will work. thanks.
I'm currently creating and defining a navigation drawer. I want now a header view, like their on the google apps, above the item rows. I only found examples with RecyclerViews, and i dont want to use it. I have all finished with a ListView and all other stuff. Maybe someone can help me :) Thanks in advance
You can create NavigationView using android design support library without having pain to create listview or RecyclerView, its all created by android.
To add it to your project you need to add the android design support library to your project, add below line in build.gradle
compile 'com.android.support:design:22.2.0
Check out android design support features here
First create a header(header.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="190dp"
android:background="#drawable/background_material"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="nyname"
</RelativeLayout>
Next create a menu resource file, the items in the menu will be the items displayed in the drawer(drawer.xml)
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/first1"
android:checked="false"
android:icon="#drawable/icon1"
android:title="#string/string1" />
<item
android:id="#+id/second2"
android:checked="false"
android:icon="#drawable/icon2"
android:title="#string/string2" />
</menu>
Next create a DrawerLayout file, within the drawerlayout you can see I have included a Toolbar and a 'FrameLayout`. When the item in the drawerlayout is clicked you can replace fragment.
Also within it is the NavigationView with these parameters:
app:headerLayout="#layout/header"
app:menu="#menu/drawer"
android:layout_gravity="start"
app:headerLayout is the header.xml that we created in step 1.
app:menu is the menu resource item i.e drawer.xml
<android.support.v4.widget.DrawerLayout 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"
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="#+id/toolbar"
layout="#layout/tool_bar"
/>
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/header"
app:menu="#menu/drawer"
/>
</android.support.v4.widget.DrawerLayout>
Next in your MainActivity extend AppcompatActivity,
public class MainActivity extends AppCompatActivity {
............................................
Intialise NavigationView and call setNavigationItemSelectedListener to get click events,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
//Replacing the main content with ContentFragment
case R.id.first1:
SomeFragment fragment = new SomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame,fragment);
fragmentTransaction.commit();
return true;
...................
Step by step procedure to create navigationview go here
How it would look: