How to use Sliding Menu with ActionBar Sherlock? - android

I am trying to use a sliding menu in my existing project where action bar Sherlock is used. But I am unable to do that. If you can give me a simple demo or tutorial to show me how to do so, that would be great? Do we have to use the fragments for the sliding menu?
Update:
How do I change the Activity when the user clicks on an item of the Sliding Menu list?
Is there a demo? please help.

Edit library SliderMenu to extents SherlockActivity
public class SlidingActivity extends SherlockActivity implements SlidingActivityBase {

Do as it says in https://github.com/jfeinstein10/SlidingMenu
Create your sliding layout.
In onCreate:
setBehindContentView(R.layout.slide_menu);
getSlidingMenu().setShadowWidthRes(R.dimen.shadow_width);
getSlidingMenu().setShadowDrawable(R.drawable.shadow);
getSlidingMenu().setBehindOffsetRes(R.dimen.actionbar_home_width);
getSlidingMenu().setBehindScrollScale(0.25f);
And in the activity:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}

I did not find it very clear from the instructions on the (SlidingMenu GitGub)
Heres a screenshot to make it more clear:

Related

Add icon to fragment actionbar and get reference to it

I have no custom bar, I just set a delete icon to the actionbar, but now I need to set OnClickListener to this icon. how can I do that without custom bar is this possible. Also the icon not apears on the left side, can I set it on the rightside?
activity.getSupportActionBar().setIcon(R.drawable.ic_delete);
I use Navigation Drawer, when I use custom bar the toggle icon despairs.
Looks like you want to set the ActionBar's home button as your delete button. I would suggest not to do it as that is a bad design decision in my opinion. And moreover you also want to show the button on the right hand side which can be done in a much intuitive manner by using a menu.
Please have a look at the official documentaion for adding ActionBar actions here
Basically you need to add an XML menu resource and declare your actions like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_delete"
android:icon="#drawable/ic_delete"
android:title="#string/action_delete"
app:showAsAction="always"/>
</menu>
Then in your Activity override the OnOptionsItemSelected method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_delete:
// Do your stuff here
return true;
default:
return super.onOptionsItemSelected(item);
}
}
actionBar.setDisplayHomeAsUpEnabled(true);
Then you need to override activity method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onIconClicked();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
To create an item in the right side, you need to create custom menu, actually, it is simple.
Here is example how to do this

Actionbar menu onclick

Is there any way to use the standard menu button in the action bar to do something? In my case a sliding menu, I already have code that works in the onClick of a normal button. I just want to use the menu button in the action bar instead. Is this possible? Or will I have to customize the action bar and not use the button that is already there?
I believe you mean the ActionBar Home icon.
First you have to enable it
getActionBar().setHomeButtonEnabled(true);
Then you have to handle the event
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
//do your work
return true;
default:
return super.onOptionsItemSelected(item);
}
}
FYI if you want to follow the Navigation drawer pattern you should read this tutorial.

Enable back button on actionbarsherlock without extend it

I have a class that extends ListActivity and I can't extend my BaseAdapter.java.
Is there any quick fix to use the bar of BaseAdapter or to show the actionbarsherlock with the back arrow?
Thank you in advance.
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);
on the onCreate() function add this line:
getActionBar().setDisplayHomeAsUpEnabled(true);
then add this method:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
this.finish();
break;
// code here for other cases
}
return (true);
}

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