JFEinstien sliding menu unable to set width of the behind view - android

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.

Related

How can i move to any other activity with jfeinstein slider library

I have implemented jfeinstein10 slider menu library in my application. With this piece of code I am successfully able to implement slider in my app.
Now my question is how can I move to next activity using this slider? The following image shows how my slider looks like:
So basically I want to move to next activity or any other activity when I click on any of the options from slider. Like for example, when I click on Comment or Post or Chat or any other option I want to go to the relevant screen. Hope this is clear. Still if you need more explanation you can ask.
Following is my code snippet.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SlidingMenu menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.5f);
menu.attachToActivity(MainActivity.this, SlidingMenu.SLIDING_CONTENT);
menu.setMenu(R.layout.activity_menu);
Button mButton = (Button) findViewById(R.id.slidingMenu);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
menu.showMenu();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
You can define the onclick listenrs to the Menu layout that you have appended to the Main Activity like so:
menu.getMenu().findViewById(R.id.yourSideMenuOption).setOnClickLister(this);
Its advisable to use an abstract activity, so that you are not using the same code over and over in each of your activity.

ShareActionProvider Not Clickable

I want to call an activity after clicking on share icon in action bar. For this I
create a menu named 'flip' which contains an item named 'menu_share' for Android
2.2+. Flip menu is inflated in 1st activity named 'ShareActivity'. After this I want to call other activity after clicking on shareicon in action bar but icon not responding.
Code for menu xml file.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto=" >
<item android:id="#+id/menu_share"
android:title="share"
android:icon="#drawable/shar"
yourapp:showAsAction="ifRoom"
yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
</menu>
Code for 1st activity.
public class ShareActivity extends ActionBarActivity {
SocialAuthAdapter adapter;
EditText edit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
ActionBar ab=getSupportActionBar();
ab.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
ab.setDisplayHomeAsUpEnabled(true);
ab.setDisplayShowTitleEnabled(false);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.flip, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
case R.id.menu_share:
startActivity(new Intent(this,ShActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
You have set an ActionProvider with yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider". This is likely intercepting the event, thus preventing your Activity from getting the call to onOptionsItemSelected().
Since you want to start your own Activity instead of using Android's default share behavior, simply remove this line.

Sliding menu toggle option

I want to use the menu button on android to make the sliding menu toggle from left to right. The problem I face is that since I have used the sliding menu functionality in my main activity on create method, I do not how to use the same variable in the onPrepareOptionMenu method.
SlidingMenu menu;
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
menu.setShadowWidth(10);
menu.setBehindOffset(60);
menu.setFadeDegree(0.25f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setBehindWidth(400);
menu.setMenu(R.layout.menu_frame);
this is the code which I use to call the sliding menu, However, I want to enable the toggle button whenever the menu button is called along side the swipe gesture .
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
//try to enable the toggle here so that the sliding menu can appear/disappear
return true;
}
The problem is that unlike most cases, I do not extend my main class with the Sherlock activity since my main class is already extending some other activity. Hence I use the sliding menu in form of a constructor(look at my example). I am not sure how to integrate the toggle function. Thank for all the help
If you need override OptionsMenu methods and you want this methods public your Activity, then you first create Activity to Options menu and in you activity need extends CustomOptionMenuActivity.
Example:
1. Create CustomOptionMenuActivity:
public class CustomOptionMenuActivity extends Activity {
private Menu SlidingMenu;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
this.SlidingMenu = menu;
return true;
}
public boolean onPrepareOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_main, menu);
//try to enable the toggle here so that the sliding menu can appear/disappear
return true;
}
}
Then you can use menu in any activities, but you need extends this activity.
Example:
public class MainActivity extends CustomOptionMenuActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
}
}
Good luck!

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: Showing Action Bar menu items depending on ViewPager

I am having trouble getting the following piece of code to work out. I have a viewpager with 3 fragments, and I want a search icon to only show up on one. I started off trying to add the search function by the fragment, but the rendering of the menu item was slow when swiping to that page. I am now on the part to add the search icon to the activity, and then just hide or show depending on which viewpager page is active, but the following is not working:
public class MyApp extends FragmentActivity implements
FragmentTeams.FragmentNotification,ViewPager.OnPageChangeListener,
OnNavigationListener{
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
menuSearch = menu.findItem(R.id.menu_search);
mSearchView = new SearchView(this);
menuSearch.setActionView(mSearchView);
menuSearch.setVisible(false);
return true;
}
#Override
public void onPageSelected(int pageNum) {
if(pageNum== 1){
ActionBar actionBar = MyApp.this.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
menuSearch.setVisible(true);
invalidateOptionsMenu();
}else{
ActionBar actionBar = MyApp.this.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
menuSearch.setVisible(false);
invalidateOptionsMenu();
}
}
While the above does (appear to) create and hide the icon at onCreateOptionsMenu, it is not reenabled when moving to
pageNum ==1
Can anyone give me some insight as to why this may be happening?
invalidateOptionsMenu make the system calls the method onPrepareOptionsMenu, so you can override this method as follows:
public boolean onPrepareOptionsMenu(Menu menu) {
int pageNum = getCurrentPage();
if (pageNum == 1) {
menu.findItem(R.id.menu_search).setVisible(true);
}
else {
menu.findItem(R.id.menu_search).setVisible(false);
}
}
public void onPageSelected(int pageNum) {
invalidateOptionsMenu();
}
You can implement onCreateOptionsMenu() in your Fragment and set 'setHasOptionsMenu(true)' for the fragment
a possible solution for this problem would be inflating your custom menu inside the activity hosts your ViewPager and getting a menu reference as below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
customMenu = menu;
return super.onCreateOptionsMenu(menu);
}
after that you can easily hide/show the menu's items without any delay inside onPageSelected method as below:
#Override
public void onPageSelected(int position) {
switch (position) {
case 0: {
customMenu.getItem(0).setVisible(false);
break;
}
case 1: {
customMenu.getItem(0).setVisible(true);
break;
}
}
I used Nermeen's answer and managed to get it without any delay.
I don't inflate anything in onCreateOptionsMenu, but use it to get a reference to the menu:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
customMenu = menu;
return super.onCreateOptionsMenu(menu);
}
Then, in onPrepareOptionsMenu(), I call the viewpager getCurrentItem() (should be something like viewPager.getCurrentItem()), call invalidateOptionsMenu() and inflate whatever menu I want in that page using the customMenu reference I created in onCreateOptionsMenu().

Categories

Resources