Slidingmenu on both sides - android

I am Completely new to android and i've been given a task of having sliding menus on click of a buttons placed on either side of the corners. Very similar to that of Facebook app. I tried using the SherlockAction bar library and the Slidingmenu library but I don't quite understand its functioning. Please help me out.
Thanks in advance

If you need to use ABS with SlidingMenu first of all go into the SlidingActivities that you plan on using make them extend Sherlock__Activity instead of __Activity.
Then simply try:
public class FragmentsHolderActivity extends SlidingFragmentActivity {
public static boolean isChangeCategoryAllow = true;
public static ImageLoader imageLoader = ImageLoader.getInstance();
public MenuItem mRefreshMenuItem;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final SlidingMenu sm = getSlidingMenu();
sm.setFadeEnabled(false);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// set the Above View
setContentView(R.layout.content_frame);
//add if you need dinamically customize your fragment as your liking.
/*getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new CategoryImagesListFragment())
.commit();*/
// set the Behind View
setBehindContentView(R.layout.menu_frame);
//add if you need dinamically customize the SlidingMenu as your liking.
/*getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame, new CategoryFragment())
.commit(); */
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
setBehindContentView will place the view in the "behind" portion of the SlidingMenu.

Related

Viewpager not displaying menu items for first fragment

I have list items onlcick of list item I take to DetailActivity.
DetailActivity is swipable using viewpager and Fragments. The issue is when DetailActivity is opened and Fragment is loaded no menu options are displayed however if i swipe left or right i get the menu options
then when i return to the initial fragment the menu items are visible.
I am inflating the menu options in fragment.
DetailActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_new);
mViewPager = (CustomViewPager) findViewById(R.id.viewPager);
int currentItemIndex = mNewsItemList.indexOf(newsItem);
mArticleSwipeMap = new HashMap<>();
mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
mPagerAdapter.swapList(mNewsItemList);
mViewPager.setOnSwipeOutListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItemIndex);
mViewPager.addOnPageChangeListener(this);
}
Adapter class
private class ArticlePagerAdapter extends FragmentStatePagerAdapter{
private Map<Integer, ArticleFragment> mPageReferenceMap = new HashMap<>();
private List<NewsItem> newsItemList;
public ArticlePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
ArticleFragment articleFragment = ArticleFragment.newInstance(newsItemList.get(position));
mPageReferenceMap.put(position, articleFragment);
return articleFragment;
}
#Override
public int getCount() {
return newsItemList.size();
}
public void swapList(List<NewsItem> newsItemList){
this.newsItemList = newsItemList;
notifyDataSetChanged();
}
}
DetailFragment
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.toolbar_menu, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch(itemId) {
case R.id.action_more:
View menuItemView = mRootView.findViewById(R.id.action_more);
onMenuClick(menuItemView);
break;
case R.id.action_share:
shareTextUrl();
break;
}
return true;
}
Update 1 : When I swipe left, menu item are not shown when i swipe right menu items are shown, now if i swipe left menu item are shown.
Has it got to do anything with setting up viewpager
This is how I am setting up viewpager in onCrete of Activity
mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
mPagerAdapter.swapList(mNewsItemList);
mViewPager.setOnSwipeOutListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItemIndex);
mViewPager.addOnPageChangeListener(this);
Update 2 :
The issue is when I use custom toolbar it is not working. When i use with the DarkActionBar it works fine but when I use with NoActionBar with my custom toolbar it creates prob.
Here is the sample proj I created which has same issue
link to github https://github.com/KumarVelu/viewPagerDemo
The problem is that you are trying to set more than one main ActionBar because each fragment has its own Toolbar. So, when ViewPager loads the current page and preloads the next page, you have 2 fragments trying to set their own Toolbar as the main ActionBar and they are conflicting.
If you want to use a Toolbar as main Action Bar, move the Toolbar in the Activity layout instead of the fragments layout and initialize it once in the Activity.
If you want a separate Toolbar in each fragment, don't set any Toolbar as main action bar and use Toolbar.inflateMenu() and Toolbar.setOnMenuItemClickListener() instead. Each fragment will then inflate its own menu items directly in its own Toolbar, without involving the Activity.
void method(){
int currentItemIndex = mNewsItemList.indexOf(newsItem);
mArticleSwipeMap = new HashMap<>();
mPagerAdapter = new ArticlePagerAdapter(getSupportFragmentManager());
mPagerAdapter.swapList(mNewsItemList);
mViewPager.setOnSwipeOutListener(this);
mViewPager.setAdapter(mPagerAdapter);
mViewPager.setCurrentItem(currentItemIndex);
mViewPager.addOnPageChangeListener(this);
}
Before
Call in onresume your fragment.
See you

Android Navigation Drawer Show Up Indicator for Lower Level Fragments

I have some issues switching navigation drawer functionality to up functionality at lower level fragment. I have read this thread to manage to show the up indicator. But when pressing the button, it'll open up the navigation drawer instead of going back to previous fragment. And I can't set action bar title in EditUserFragment to "Edit Profile".
I'm using the navigation drawer template available in Android Studio.
I have three levels:
MainActivity with navigation drawer that consists of Home and Profile items
UserFragment titled with "Profile" that has an option item that'll bring up EditUserFragment
EditUserFragment (lower level fragment) is triggered by UserFragment
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
R.drawable.ic_launcher, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
}
#Override
public void onBackPressed() {
super.onBackPressed();
// this won't change the drawer indicator back
drawerToggle.setDrawerIndicatorEnabled(true);
// this works
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
public void onSectionAttached(int number) {
// Show the corresponding title on the action bar when clicked and open corresponding
// fragments.
Fragment fragment = null;
switch (number) {
case 1:
mTitle = getString(R.string.title_home);
break;
case 2:
mTitle = getString(R.string.title_profile);
fragment = new UserFragment();
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setTitle(mTitle);
}
public void setActionBarTitle(String title){
getActionBar().setTitle(title);
Log.d("Title 2", getActionBar().getTitle().toString());
}
onBackPressed is working partially (commented in code) when I pressed back button on phone (not up button on action bar).
UserFragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setHasOptionsMenu(true);
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_profile, container, false);
drawerLayout = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout,
R.drawable.ic_launcher, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
return view;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_edit:
drawerToggle.setDrawerIndicatorEnabled(false);
// Disable sliding from edge to open drawer
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
getFragmentManager().beginTransaction()
.replace(R.id.container, new EditUserFragment())
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
.addToBackStack(null)
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
EditUserFragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
((MainActivity) getActivity()).setActionBarTitle("Edit Profile");
Log.d("Title", getActivity().getActionBar().getTitle().toString());
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Get item selected and deal with it
Log.d("KEY: ", String.valueOf(item.getItemId()));
switch (item.getItemId()) {
case android.R.id.home:
Log.d("EditUserFragment", "I'm here");
getActivity().onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true); works as expected that up caret is showing instead of drawer; though when tapping it, it's opening up drawer instead of going back to previous fragment. In addition, the code under case android.R.id.home is never executed.
And I try to set action bar title to "Edit Profile" from "Profile". The log shows me "Edit Profile", but the actual running app shows me "Profile" for some reason.
What I want to achieve is be able to go back to UserFragment from EditUserFragment by tapping the up indicator (right now the up indicator is opening up navigation drawer). And show correct title in EditUserFragment.
Any help is greatly appreciated!!
After days of analyzing, I discovered that the problem is I am using the built-in navigation drawer activity when creating it. The built-in separate the tasks into two. MainActivity and NavigationDrawerFragment. Thus, the drawerToggle I have in MainActivity is not the same as the real one in NavigationDrawerFragment.
Oroginally, MainActivty calls NavigationDrawerFragment to setup all the things needed for navigation drawer. I SOLVED this by implementing navigation drawer in my MainActivity, so I'll have only one drawerToggle. Though, I still can't find the way to make it work if I have everything (the navigation drawer variables) in NavigationDrawerFragment rather than MainActivity. If anyone knows the answer to that, feel free to leave a comment!

how to customize setSecondaryMenu's width of SlidingMenu library of jfeinstein

I am developing an app in which i have to use left and right menu.I googled and found SlidingMenu library of #jfeinstein
Things are working fine i am able to open both left and right menu.But The only problem is that the secondry menu opens with unnecessary extra spacing.I need to remove that extra spacing.I have attached the screen shots also.
here spacing is fine
Please let me know the solution.
But here i need to make the content fit inside the menu.
and here is Activity for displaying menu
public class BaseSlidingMenuActivity extends SlidingFragmentActivity {
private int mTitleRes;
protected ListFragment mFrag;
protected SlideLeftMenuFragment mLeftFrag;
/**
* Title for Sliding Menu.
*
* #param titleRes
*/
public BaseSlidingMenuActivity(int titleRes) {
mTitleRes = titleRes;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle(mTitleRes);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
if (savedInstanceState == null) {
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
mFrag = new SlideRightMenuFragment();
t.replace(R.id.menu_frame, mFrag);
t.commit();
} else {
mFrag = (ListFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
}
// customize the SlidingMenu
final SlidingMenu sm = getSlidingMenu();
sm.setMode(SlidingMenu.LEFT_RIGHT);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setShadowDrawable(R.drawable.shadow);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
sm.setSecondaryMenu(R.layout.menu_frame_two);
sm.setSecondaryShadowDrawable(R.drawable.shadowright);
mLeftFrag = new SlideLeftMenuFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.menu_frame_two, mLeftFrag).commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// setSlidingActionBarEnabled(false);
}
Thanks in advance
I had forked a new branch to add this feature from jfeinstein's slidemenu.
https://github.com/buptcoder/SlidingMenu
in my branch, I added a new method for SlideMenu.
You can use
sm.setRightBehindOffset(200);
to adjust the right menu's width.
Any problems please let me know.

toggle() when home button click on slidingmenu with actionbarsherlock

Here is my MainActivity
public class MainActivity extends SherlockActivity implements ActionBar.OnNavigationListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); //Used for theme switching in samples
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Hide title bar
getSupportActionBar().setDisplayShowTitleEnabled(false);
//Enable home button
getSupportActionBar().setHomeButtonEnabled(true);
//Home as up display
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Sliding menu
SlidingMenu menu = new SlidingMenu(getBaseContext());
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.slide_menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//should be something in here that makes it slide to the left
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Used to put dark icons on light action bar
//boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
menu.add("New")
.setIcon(R.drawable.contentnew)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Search")
.setIcon(R.drawable.actionsearch)
.setActionView(R.layout.collapsible_edittext)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
I want to set when home button is clicked, it slides to the left. Thing is, i can't extend SlidingFragmentActivity like examples in jfeinstein10's project, because i already extended SherlockActivity. How can i achieve this?
You need to make a change in the SlidingMenu code. Make SlidingFragmentActivity extend SherlockFragmentActivity. Then add ActionBarSherlock as library project to SlidingMenu. Now your project only has to add SlidingMenu as library project, because that references ActionBarSherlock.
Quoted from https://github.com/jfeinstein10/SlidingMenu#setup-with-actionbarsherlock:
Setup with ActionBarSherlock
Setup as above.
Checkout a clean copy of ActionBarSherlock and import into your Eclipse workspace.
Add ActionBarSherlock as a dependency to SlidingMenu
Go into the SlidingActivities that you plan on using make them extend Sherlock___Activity instead of ___Activity.
You need to extend from SlidingFragmentActivity, If you don´t do it you can´t even call toggle or set the menu fragment.If you extended from SFA it would be something like this:
case android.R.id.home:
toggle();
return true;

Android slide menu that slide from both sides left and right

I'm trying to make sliding drawer menu like the one in the Facebook app. I navigated many questions like this amazing one here.
and found a lot of libraries but all of them slide from left to right or from right to left in different one. I want to make it slide from both sides, left to right and right to left via two buttons in the top bar. Can any one help me with this.
Thanks in advance.
this is the one I use and does exactly what you want:
SlidingMenu
You will have to implement the button feature yourself but it shouldn't be too hard!
EDIT:
An example:
SlidingMenu menuS = new SlidingMenu(this);
menuS.setMode(SlidingMenu.LEFT_RIGHT);
menuS.setMenu(R.layout.slideout_list);
menuS.setSecondaryMenu(R.layout.slideout_list2);
As the code shows you need to set the mode to LEFT_RIGHT and must specify a layout for both the left menu (setMenu()) and the right menu (setSecondaryMenu()) along with the other options specifying menu size and shadows etc.
Try this
https://github.com/srikanthgr/FacebookSlideOutmenu
implement this in to your project, This is exactly what you want.
There is a branch for a right to left sliding menu of the jfeinstein's SlidingMenu original here:
https://github.com/jfeinstein10/SlidingMenu/tree/slidingright
Alternatively, there is Simon's implementation which has simple configurations for making the switch from Left-to-Right and Right-to-Left here: https://github.com/SimonVT/android-menudrawer. There is a simple example on the page.
Although, I'm not one to publicly voice an opinion, I find Simon's library a tad easier to use. ;-)
That being said, I don't, however, take away absolutely any credit from jfeinstein either. His library powers one of my better selling apps. :-)
This question is now very old, but it has been built into Android now. So if people are searching and come across this post, hit up the Navigation Drawer section on the developer pages of Android.
http://developer.android.com/design/patterns/navigation-drawer.html
Use this one, it's good for you :
abstract public class BaseSlideFragmentActivity extends SlidingFragmentActivity implements IActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
Utils.log(new Exception(), "[onCreate]");
super.onCreate(savedInstanceState);
getSlidingMenu().setMode(SlidingMenu.LEFT_RIGHT);
getSlidingMenu().setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
setBehindContentView(R.layout.menu_right);
getSupportFragmentManager().beginTransaction().replace(R.id.menuRight, new MenuRightFragment()).commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_left);
getSupportFragmentManager().beginTransaction().replace(R.id.menuLeft, new MenuLeftFragment()).commit();
getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
getSlidingMenu().setBehindOffsetRes(R.dimen.slidingmenu_offset);
getSlidingMenu().setFadeDegree(0.35f);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setSlidingActionBarEnabled(false);
}
public Activity getActivity() {
return this;
}
#Override
public MyApplication getApplicationContext() {
return (MyApplication) super.getApplicationContext();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Utils.log(new Exception(), "[onOptionsItemSelected]");
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
case R.id.setting:
if (getSlidingMenu().isMenuShowing()) {
toggle();
}
else {
showSecondaryMenu();
}
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Utils.log(new Exception(), "[onCreateOptionsMenu]");
getSupportMenuInflater().inflate(R.menu.menu_setting, menu);
return true;
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setTitle(R.string.attach);
// set the content view
setContentView(R.layout.activity_main);
// configure the Sliding right Menu
SlidingMenu menuR = new SlidingMenu(this);
menuR.setMode(SlidingMenu.RIGHT);
menuR.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
// menuR.setShadowWidthRes(R.dimen.abc_action_bar_default_height);
// menu.setShadowDrawable(R.drawable.shadow);right menu
menuR.setBehindOffsetRes(R.dimen.abc_action_bar_default_height);
menuR.setFadeDegree(0.35f);
menuR.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuR.setMenu(R.layout.right_menu_layout);
// configure the Sliding left Menu
SlidingMenu menuL = new SlidingMenu(this);
menuL.setMode(SlidingMenu.LEFT);
menuL.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
// menuL.setShadowWidthRes(R.dimen.abc_action_bar_default_height);
// menu.setShadowDrawable(R.drawable.shadow);left menu
menuL.setBehindOffsetRes(R.dimen.abc_action_bar_default_height);
menuL.setFadeDegree(0.35f);
menuL.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuL.setMenu(R.layout.left_menu_layout);
}

Categories

Resources