toggle() when home button click on slidingmenu with actionbarsherlock - android

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;

Related

Slidingmenu on both sides

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.

JFEinstien sliding menu unable to set width of the behind view

I was working on implementing the sliding menu.
I want to set the width of the sliding menu as 2/3 of the screen width when active.
Here is my MainActivity
public class MainActivity extends SlidingActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setBehindContentView(R.layout.slidemenu_layout);
setSlidingActionBarEnabled(false);
SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidthRes(R.dimen.shadow_width);
menu.setShadowDrawable(R.drawable.shadow);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
menu.setBehindWidth((2*displaymetrics.widthPixels)/3);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportMenuInflater().inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
toggle();
return true;
}
return super.onOptionsItemSelected(item);
}
}
*NOTE :*I had initially made MainActivity extend Activity and it was working fine there.
I had to move to SlidingActivity to implement Action Bar Sherlock
EDIT :I replaced SlidingActivity with SherlockActivity and deleted the setBehindContentView(..) and carried on.But no clue why 1st one did not work.
Have you tried using setBehindWidthRes rather than setBehindOffsetRes?
You can try setting the behind width with setBehindOffsetRes, and passing an id from a resource XML, like in this answer.

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);
}

Way to set Home Application Icon in ActionBarSherlock

I am confused the way to set home icon on ActionbarSherlock and of course am new to this ActionBarSherlock. Have checked many sources, but unable to get how to set the home icon. Below is my class that sets the ActionbarSherlock.
public abstract class BaseActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem miPrefs = menu.add("Login");
miPrefs.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
miPrefs.setOnMenuItemClickListener(new OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Intent loginIntent = new Intent(BaseActivity.this, LoginForm.class);
startActivity(loginIntent);
return true;
}
});
return true;
}
}
Of course I know how to set application icon as home icon in the normal action bar. The following is the way I usually set the normal actionbar.
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem menu1 = menu.add(0, 0, 0, "Login");
menu1.setIcon(R.drawable.image1);
menu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
In the onCreate(), we have to get the actionbar by getActionBar() and then with actionbar.setDisplayHomeAsEnabled(true), it is possible to set the application icon as home icon. By setting the following we can listen to the clicks of home icon.
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
// Here we can keep the code to get to the mainactivity.
return true;
}
}
Also, when I just try to get the actionbar by ActionBar actionbar = getSupportActionBar(); in oncreate(), I get this error,
Type mismatch: cannot convert from com.actionbarsherlock.app.ActionBar to android.app.ActionBar
I'm confused about how to set the application icon as home icon based on the above code of ActionbarSherlock and listen for the clicks. How can I get that done?
Enabling the App Icon to be clickable in the ActionBar (using ABS)
#Override
public void onCreate() {
super.onCreate();
getSupportActionBar().setHomeButtonEnabled(true);
}
ABS is a library so when you want to access it's features, you must use it's own methods/classes, not to be confused with the default Android methods/classes (such as getActionBar() and getSupportActionBar()). A great place for sample code is https://github.com/JakeWharton/ActionBarSherlock/tree/master/samples/demos.
Listening to clicks
The same as what you have above.

How to use Sliding Menu with ActionBar Sherlock?

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:

Categories

Resources