I currently have an existing project in which I would like to implement an navigation drawer activity. Currently, if I add a new navigation drawer activity, it generates the following layout:
However, instead of the layout above I would like my navigation drawer to look exactly like the default template one which looks like this (consisting of a nav header, etc):
How can I go about achieving this? I'm new to Android development.
EDIT: I just needed to change my app theme to achieve what I wanted
Create a new Drawer Activity :
File->new->activity->Navigation Drawer Activity
private void setupNavigationDrawer() {
//if you want to update the items at a later time it is recommended to keep it in a variable
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(0).withName(R.string.drawer_item_home).withIcon(getResources().getDrawable(R.drawable.ic_home_white_24dp));
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.drawer_item_login).withIcon(getResources().getDrawable(R.drawable.ic_account_circle_white_24dp));
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(2).withName(R.string.drawer_item_movies).withIcon(getResources().getDrawable(R.drawable.ic_movie_white_24dp));
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(3).withName(R.string.drawer_item_trailers).withIcon(getResources().getDrawable(R.drawable.ic_videocam_white_24dp));
PrimaryDrawerItem item5 = new PrimaryDrawerItem().withIdentifier(4).withName(R.string.drawer_item_theatres).withIcon(getResources().getDrawable(R.drawable.ic_theaters_white_24dp));
PrimaryDrawerItem item6 = new PrimaryDrawerItem().withIdentifier(5).withName(R.string.drawer_item_location).withIcon(getResources().getDrawable(R.drawable.ic_location_on_white_24dp));
SecondaryDrawerItem item7 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(6).withName(R.string.drawer_item_about_us).withIcon(FontAwesome.Icon.faw_info_circle);
SecondaryDrawerItem item8 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(7).withName(R.string.drawer_item_contact_us).withIcon(FontAwesome.Icon.faw_whatsapp);
SecondaryDrawerItem item9 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(8).withName(R.string.drawer_item_feedback).withIcon(FontAwesome.Icon.faw_commenting);
SecondaryDrawerItem item10 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(9).withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_wrench);
// Create the AccountHeader
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.color.colorMaterialDark)
.addProfiles(
new ProfileDrawerItem().withName(getResources().getString(R.string.app_name)).withEmail(getResources().getString(R.string.email_id)).withIcon(getResources().getDrawable(R.drawable.profile))
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
//Any activity or Intent
}
})
.build();
//Create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3, item4, item5, item6,
new SectionDrawerItem().withName("Extras"),
item7, item8, item9, item10
)
//Set onClick options for drawer item click
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
switch (position) {
//Home
case 1: {
break;
}
//Login
case 2: {
//Login Activity
break;
}
case 3: {
//Third activity
break;
}
case 4: {
break;
}
case 5: {
break;
}
//Location
case 6: {
break;
}
//About us
case 8: {
break;
}
//Contact Us
case 9: {
break;
}
//Feedback
case 10: {
break;
}
//Settings
case 11:{
break;
}
}
return true;
}
})
.build();
result.addStickyFooterItem(new PrimaryDrawerItem().withName("Visit us again")); //Adding footer to nav drawer
}
You will have to add the following lines to build.gradle file in android studio
compile('com.mikepenz:materialdrawer:5.3.3#aar') {
transitive = true
}
compile 'com.mikepenz:fontawesome-typeface:4.6.0.2#aar'
Related
Within my navigation drawer, I am trying to execute a code to start a new activity so it moves from one activity to another. However, When I use a DividerDrawerItem() it does not execute the last case statement (4). I've tried it without the DividerDrawerItem() and it works but when I add it in, it does not.
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Home Page");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Report");
PrimaryDrawerItem item4 = new PrimaryDrawerItem().withIdentifier(4).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2,item3,
new DividerDrawerItem(),
item4
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
switch(position){
case 1: startActivity(new Intent(HomePage.this, HomePage.class));
break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: startActivity(new Intent(HomePage.this,Report.class));
break;
case 4: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this,MainActivity.class));
}
return true;
}
I think your code is using position where it should use identifier.
Change switch(position) to switch((int) drawerItem.getIdentifier())
I am using a navigation drawer and want it to do different activities for different cases. I am using the switch method to implement the activities. I have implemented 3 switch cases but however, it keeps doing the case 3 instead of case 2. Here is my code that i am using:
PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName("Discover");
PrimaryDrawerItem item2 = new PrimaryDrawerItem().withIdentifier(2).withName("Rank Table");
PrimaryDrawerItem item3 = new PrimaryDrawerItem().withIdentifier(3).withName("Log out");
//create the drawer and remember the `Drawer` result object
Drawer result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.addDrawerItems(
item1, item2, item3
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
}
return true;
})
.build();
You forgot to add break; at the end of each switch case.
switch(position){
case 1: break;
case 2: startActivity(new Intent(HomePage.this, RankT.class));
break;
case 3: firebaseAuth.signOut();
finish();
startActivity(new Intent(HomePage.this, MainActivity.class));
break;
}
Hope this will help!!
I've got fragment with SwipeMenuListView. Data comes from API. I'm binding buttons of SwipeMenuList in fragment and sending objects to Adapter to get
this
Everything is fine
But after filtering listview buttons are broken. When I press Profile button in filtered listview, app opens wrong profile
Filtered data
So my question is how to update position of buttons?
notifyDataSetChanged and invalidateViews don't work for me.
public void setupSwipeList() {
adapter = new SearchCardAdapter(getContext(), arrayList, a);
final SwipeMenuCreator creator = new SwipeMenuCreator() {
#Override
public void create(SwipeMenu menu) {
// create "open" item
SwipeMenuItem profileItem = new SwipeMenuItem(
getContext());
// set item background
profileItem.setBackground(new ColorDrawable(Color.rgb(38, 153, 251)));
// set item width
profileItem.setWidth(300);
// set item title
profileItem.setTitle("Profile");
// set item title fontsize
profileItem.setTitleSize(18);
profileItem.setIcon(R.drawable.ic_profile);
// set item title font color
profileItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(profileItem);
SwipeMenuItem phoneItem = new SwipeMenuItem(
getContext());
// set item background
phoneItem.setBackground(new ColorDrawable(Color.rgb(38, 153, 251)));
// set item width
phoneItem.setWidth(300);
// set item title
phoneItem.setTitle("Mobile call");
// set item title fontsize
phoneItem.setTitleSize(18);
phoneItem.setIcon(R.drawable.ic_phone);
// set item title font color
phoneItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(phoneItem);
// create "delete" item
SwipeMenuItem requestItem = new SwipeMenuItem(
getContext());
// set item background
requestItem.setBackground(new ColorDrawable(Color.rgb(38, 153, 251)));
// set item width
requestItem.setWidth(300);
// set item title
requestItem.setTitle("Request");
// set item title fontsize
requestItem.setTitleSize(18);
requestItem.setIcon(R.drawable.ic_request);
// set item title font color
requestItem.setTitleColor(Color.WHITE);
// add to menu
menu.addMenuItem(requestItem);
}
};
// Right
listView.setSwipeDirection(SwipeMenuListView.DIRECTION_RIGHT);
// Left
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
listView.smoothOpenMenu(i);
}
});
listView.setSwipeDirection(SwipeMenuListView.DIRECTION_LEFT);
listView.setMenuCreator(creator);
listView.setAdapter(adapter);
final Bundle bundle = new Bundle();
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
SpecialistFragment postobj = new SpecialistFragment();
bundle.putString("spec_id", arrayList.get(position).getId());
bundle.putString("phone", arrayList.get(position).getPhone());
postobj.setArguments(bundle);
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, postobj).addToBackStack(null).commit();
break;
case 1:
Toast.makeText(getContext(), "CALL", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + "+" + arrayList.get(position).getPhone()));
getActivity().getApplicationContext().startActivity(intent);
break;
case 2:
SendRequestFragment obj = new SendRequestFragment();
bundle.putString("spec_id", arrayList.get(position).getId());
bundle.putString("spec_name", arrayList.get(position).getName());
bundle.putSerializable("categories", arrayList.get(position).getCategories());
bundle.putSerializable("districts", arrayList.get(position).getDistricts());
obj.setArguments(bundle);
fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, obj).addToBackStack(null).commit();
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
This problem was because I had called arrayList.get(position) instead of adapter.getItem(position)
When listView was filtered, adapter had another filtered data and it's not connected with SwipeMenuListView. So we should call adapter.getItem(position) to get relevant data.
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
switch (index) {
case 0:
// open
SpecialistFragment postobj = new SpecialistFragment();
bundle.putString("spec_id", adapter.getItem(position).getId());
bundle.putString("phone", adapter.getItem(position).getPhone());
postobj.setArguments(bundle);
android.support.v4.app.FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, postobj).addToBackStack(null).commit();
break;
case 1:
Toast.makeText(getContext(), "CALL", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + "+" + adapter.getItem(position).getPhone()));
getActivity().getApplicationContext().startActivity(intent);
break;
case 2:
SendRequestFragment obj = new SendRequestFragment();
bundle.putString("spec_id", adapter.getItem(position).getId());
bundle.putString("spec_name", adapter.getItem(position).getName());
bundle.putSerializable("categories", adapter.getItem(position).getCategories());
bundle.putSerializable("districts", adapter.getItem(position).getDistricts());
obj.setArguments(bundle);
fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, obj).addToBackStack(null).commit();
break;
}
// false : close the menu; true : not close the menu
return false;
}
});
I am using a Navigation drawer in my Main screen and use the third party library com.mikepenz:materialdrawer:5.1.6#aar, to build my Material Drawer. My requirement is that I want to hide some menu items based on a condition. Is there a way to achieve it? This is how I create my drawer.
private Drawer result = null;
AccountHeader headerResult;
final PrimaryDrawerItem home = new PrimaryDrawerItem().withName("Home").withIdentifier(1).withIcon(GoogleMaterial.Icon.gmd_home);
final PrimaryDrawerItem profile = new PrimaryDrawerItem().withName("Profile").withIdentifier(2).withIcon(GoogleMaterial.Icon.gmd_account);
final PrimaryDrawerItem gallery = new PrimaryDrawerItem().withName("Gallery").withIdentifier(3).withIcon(R.drawable.ic_perm_media_black_24dp);
final PrimaryDrawerItem recognition = new PrimaryDrawerItem().withName("Recognition").withIdentifier(4).withIcon(GoogleMaterial.Icon.gmd_face);
final PrimaryDrawerItem maps = new PrimaryDrawerItem().withName("Maps").withIdentifier(5).withIcon(R.drawable.ic_place_black_24dp);
final PrimaryDrawerItem tagAndLocate = new PrimaryDrawerItem().withName("Tag & Locate").withIdentifier(6).withIcon(R.drawable.ic_remove_red_eye_black_24dp);
final PrimaryDrawerItem gamesAndPuzzle = new PrimaryDrawerItem().withName("Games & Puzzles").withIdentifier(7).withIcon(R.drawable.ic_casino_black_24dp);
final PrimaryDrawerItem backup = new PrimaryDrawerItem().withName("Backup").withIdentifier(8).withIcon(GoogleMaterial.Icon.gmd_save);
final PrimaryDrawerItem logout = new PrimaryDrawerItem().withName("Logout").withIdentifier(9).withIcon(FontAwesome.Icon.faw_sign_out);
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.header)
.withSelectionListEnabledForSingleProfile(false)
.addProfiles(userProfile)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
#Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
return false;
}
})
.build();
result = new DrawerBuilder()
.withActivity(this)
.withAccountHeader(headerResult)
.withToolbar(toolbar)
.withDisplayBelowStatusBar(false)
.withTranslucentStatusBar(true)
.withSavedInstance(savedInstanceState)
.withActionBarDrawerToggle(true)
.withActionBarDrawerToggleAnimated(true)
.addDrawerItems(home)
.addDrawerItems(profile)
.addDrawerItems(gallery)
.addDrawerItems(recognition)
.addDrawerItems(maps)
.addDrawerItems(tagAndLocate)
.addDrawerItems(gamesAndPuzzle)
.addDrawerItems(backup)
.addDrawerItems(new DividerDrawerItem())
.addDrawerItems(logout)
.buildForFragment();
resultStandard.setOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
int drawItemId = (int) drawerItem.getIdentifier();
Intent intent;
Fragment fragment;
switch (drawItemId) {
case 1:
fragment = new HomeFragment();
gaFragmentStack.add(home);
break;
case 2:
fragment = new ProfileFragment();
gaFragmentStack.add(profile);
break;
case 3:
fragment = new GalleryFragment();
gaFragmentStack.add(gallery);
break;
case 4:
fragment = new RecognitionFragment();
gaFragmentStack.add(recognition);
break;
case 5:
fragment = new MapsFragment();
gaFragmentStack.add(maps);
break;
case 6:
fragment = new TagLocateFragment();
gaFragmentStack.add(tagAndLocate);
break;
case 7:
fragment = new GamesPuzzlesFragment();
gaFragmentStack.add(gamesAndPuzzle);
break;
case 8:
fragment = new BackupFragment();
gaFragmentStack.add(backup);
break;
default:
fragment = new HomeFragment();
break;
}
if (drawItemId == 9) {
FirebaseAuth.getInstance().signOut();
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.apply();
intent = new Intent(MainMenuActivity.this, SplashScreen.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container_gaFragments, fragment);
transaction.commit();
return false;
}
});
I just want to hide some Drawer item based on a condition. Is there a way to do that? Any help is appreciated.
There is no way to hide a particular item from the drawer. You can resolve this by removing all items (removeAllItems() and removeAllStickyFooterItems()) and adding them selectively again. Below is the example code for Kotlin where you can handle this with IF statement inside the slider.apply {...}
fun refreshMenuItems() {
slider.apply {
removeAllItems()
removeAllStickyFooterItems()
if (isLoggedUser == false) {
addItems(
PrimaryDrawerItem().apply {
nameText = "Login"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "Register"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
} else {
addItems(
PrimaryDrawerItem().apply {
nameText = "My profile"
iconicsIcon = GoogleMaterial.Icon.gmd_verified_user
identifier = 70
},
DividerDrawerItem(),
PrimaryDrawerItem().apply {
nameText = "My reservations"
iconicsIcon = GoogleMaterial.Icon.gmd_how_to_reg
identifier = 80
}
)
addStickyDrawerItems(
PrimaryDrawerItem().apply {
nameText = "Log out"
isSelectable = false
identifier = 100
}
)
}
}
}
For example, you can call this method from onResume() method
override fun onResume() {
super.onResume()
changeDrawerUserName()
refreshMenuItems()
}
I have a project navigation drawer using library (https://github.com/mikepenz/MaterialDrawer) with sliding tab which contained 3 fragment (HomeFragment, LiveFragment, MovieFragment).
I was created navigation drawer in MainActivity.class there are some items (Home, Live TV, Movie). I want when i click item Home from navigation drawer and then go to HomeFragment. I have tried with code below but nothing happen when i click item Home from navigation drawer.
Please help me to resolve this problem. Thank you :)
this is my MainAactivity.class
public class MainActivity extends AppCompatActivity{
public final static int NAV_ID_FRAG_ONE = 1;
public final static int NAV_ID_FRAG_TWO = 2;
public final static int NAV_ID_FRAG_THREE = 3;
public final static int NAV_ID_ABOUT_ACTIVITY = 6;
Toolbar toolbar;
private SlidingTabLayout mSlidingTabLayout;
private ViewPager mViewPager;
private Drawer result;
private AccountHeader headerResult;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setTitle("JMN Anywhere");
setSupportActionBar(toolbar);
mViewPager = (ViewPager) findViewById(R.id.vp_tabs);
mViewPager.setAdapter(new TabAdapter(getSupportFragmentManager(), this));
mSlidingTabLayout = (SlidingTabLayout) findViewById(R.id.st1_tabs);
mSlidingTabLayout.setDistributeEvenly(true);//meratakan posisi icon
mSlidingTabLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
mSlidingTabLayout.setSelectedIndicatorColors(getResources().getColor(R.color.slidingcolor));
mSlidingTabLayout.setCustomTabView(R.layout.tab_view, R.id.tv_tab);
mSlidingTabLayout.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
mSlidingTabLayout.setViewPager(mViewPager);
//Navigation Drawer
//Header
AccountHeader headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withCompactStyle(false)
.withSavedInstance(savedInstanceState)
.withThreeSmallProfileImages(false)
.withHeaderBackground(R.drawable.header)
.build();
//List Drawer
Drawer result = new DrawerBuilder()
.withActivity(this)
.withToolbar(toolbar)
.withDisplayBelowStatusBar(true)
.withActionBarDrawerToggleAnimated(true)
.withDrawerGravity(Gravity.LEFT)
.withSavedInstance(savedInstanceState)
.withAccountHeader(headerResult)
.withHasStableIds(true)
.withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
.addDrawerItems(//set item drawer
new PrimaryDrawerItem().withName("Home").withDescription("Beranda").withIcon(R.drawable.ic_home_black_48dp).withIdentifier(NAV_ID_FRAG_ONE).withSelectable(false),
new PrimaryDrawerItem().withName("Live TV").withDescription("Siaran Televisi").withIcon(R.drawable.ic_live_tv_black_48dp).withIdentifier(NAV_ID_FRAG_TWO).withSelectable(false),
new PrimaryDrawerItem().withName("Movies").withDescription("Film-film").withIcon(R.drawable.ic_local_movies_black_48dp).withIdentifier(NAV_ID_FRAG_THREE).withSelectable(false),
new ExpandableDrawerItem().withName("Categories").withLevel(2).withIdentifier(4).withSelectable(false).withSubItems(
new SecondaryDrawerItem().withName("Action").withLevel(3).withIdentifier(2000),
new SecondaryDrawerItem().withName("Comedy").withLevel(3).withIdentifier(2001)
),
new SectionDrawerItem().withName("Others"),
new SecondaryDrawerItem().withName("Account").withIcon(R.drawable.ic_account_box_black_48dp).withIdentifier(5).withSelectable(false),
new SecondaryDrawerItem().withName("About").withIcon(R.drawable.ic_info_black_48dp).withIdentifier(NAV_ID_ABOUT_ACTIVITY).withSelectable(false),
new SecondaryDrawerItem().withName("Logout").withIcon(R.drawable.ic_exit_to_app_black_48dp).withIdentifier(7).withSelectable(false)
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
#Override
public boolean onItemClick(View view, int i, IDrawerItem drawerItem) {
Fragment fragment = null;
switch ((int) drawerItem.getIdentifier()) {
case NAV_ID_FRAG_ONE:
fragment = new HomeFragment();
break;
case NAV_ID_FRAG_TWO:
fragment = new LiveFragment();
break;
case NAV_ID_FRAG_THREE:
fragment = new MovieFragment();
break;
case NAV_ID_ABOUT_ACTIVITY:
Intent intent = new Intent(MainActivity.this, AboutActivity.class);
startActivity(intent);
break;
}
return false;
}
})
.withSelectedItem(1)
.withFireOnInitialOnClick(true)
// add the items we want to use with our Drawer
.build();
new RecyclerViewCacheUtil<IDrawerItem>().withCacheSize(2).apply(result.getRecyclerView(), result.getDrawerItems());
}
#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) {
switch (item.getItemId()) {
case R.id.action_settings:
// User chose the "Settings" item, show the app settings UI...
Intent in = new Intent("com.ajjunaedi.jmnanywhere.AboutActivity");
startActivity(in);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You need to add FragmentManager to add/remove/replace a fragment.
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment,fragment).commit();
getSupportFragmentManager() = Return the FragmentManager for interacting with fragments associated with this activity.
.beginTransaction() = Start a series of edit operations on the Fragments associated with this FragmentManager.
R.id.fragment is the id of the container where you be putting your fragment.
for more info refer to this post - What does FragmentManager and FragmentTransaction exactly do?
EDIT: you will add this after your switch statement. I hope this is the answer that you are looking for.
Add this to your xml file. inside the baselayout
<RelativeLayout
android:id="#+id/fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Resize it to your desire. or you can simply add the line android:id="#+id/fragment" to your baselayout.