Adding 2 sliding menus to an activity - android

I have an activity, where using the Sliding Menu library, i try to create 2 sliding menus.
This is the code i tried:
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
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.tutorial_layout);
rightSlide = new HelpFragment();
t.replace(R.id.slidingList2, rightSlide);
t.commit();
menu.setSecondaryMenu(R.layout.log_history);
leftSlide = new LogHistory();
t.replace(R.id.loghistorycon, leftSlide);
t.commit();
Now i get a ANR error, and Logcat says, that the FragmentTransaction t, has already been commited.
I looked at the example from: github.com/jfeinstein10/SlidingMenu and it allows him to do 2 commit's:
setContentView(R.layout.content_frame);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, new SampleListFragment())
.commit();
getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.menu_frame_two, new SampleListFragment())
.commit();
What am I doing wrong? i just can't see the difference

Change your above code as below
FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
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.tutorial_layout);
rightSlide = new HelpFragment();
t.replace(R.id.slidingList2, rightSlide);
t.commit();
t = this.getSupportFragmentManager().beginTransaction();
menu.setSecondaryMenu(R.layout.log_history);
leftSlide = new LogHistory();
t.replace(R.id.loghistorycon, leftSlide);
t.commit();
For a FragmentTransaction, you can have only one commit. In your code you created a FragmentTransaction object and called commit once for rightSlide. So t is not usable for transactions anymore. So you have create another FragmentTransaction as I have done in the above code. I hope this will work for you.

#LLL
Following code is working Properly to me.i hope,it help u more...
SlidingMenu slidingMenu = getSlidingMenu();slidingMenu.setMode(SlidingMenu.LEFT_RIGHT);
slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
slidingMenu.setFadeDegree(0.35f);
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
slidingMenu.setMenu(R.layout.profile);
slidingMenu.setSecondaryMenu(R.layout.nextactivity);
Button csButton=(Button)findViewById(R.id.txtx);
inside csButton onclick listener just need to call slidingMenu.showSecondaryMenu();
and, Button csButton1=(Button)findViewById(R.id.button1);
inside csButton1 onclick listener just need to call slidingMenu.showMenu();

Related

how to execute new SlidingMenu(this) only once to implement SlidingMenu?

I follow SlidingMenu Example to create a sliding menu:
public class MyActivity extends FragmentActivity {
public void onCreate(Bundle savedInstanceState) {
......
SlidingMenu menu = new SlidingMenu(this);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.activity_fragment);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentContainer, new MenuFragment())
.commit();
}
}
It works.
But this will execute new SlidingMenu(this) everytime when navigation to this activity.
I want to create SlidingMenu only once for this activtiy, so
The first modify try:
private static SlidingMenu sSlidingMenu;
......
if (sSlidingMenu == null) {
sSlidingMenu = new SlidingMenu(this);
}
sSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
sSlidingMenu.setMenu(R.layout.activity_fragment);
......
It works when executed first time, but it crash second time:
java.lang.IllegalStateException: This SlidingMenu appears to already be attached
I google this error tip, and found these similar questions:
Q1 StackoverFlow: This SlidingMenu appears to already be attached Androd
Q2 Github: Fix for changing content view after SlidingMenu is attached to the Activity. #324
then try
The Second modify try:
......
if (sSlidingMenu == null) {
sSlidingMenu = new SlidingMenu(this);
sSlidingMenu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
}
sSlidingMenu.setMenu(R.layout.activity_fragment);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragmentContainer, new MenuFragment())
.commit();
It also crash when execute second time:
java.lang.IllegalArgumentException: No view found for id 0x7f090035 (:id/fragmentContainer) for fragment MenuFragment
A similar question
I have no idea how to execute new SlidingMenu(this) only once to implement SlidingMenu. Please help, thanks.

jfeinstein10/SlidingMenu attaching a Fragment

I'm trying to implement the jfeinsteins sliding menu, however I'm having a problem getting a fragment to attach. The issue is that the layout is shown on the screen twice. I assume that this is because I setMenu and then do the replace (adding it again).
Could anyone provide some pointers please.
public void configureSlidingMenu()
{
// configure the SlidingMenu
menu = new SlidingMenu(this);
menu.setMode(SlidingMenu.RIGHT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menu.setFadeDegree(0.35f);
menu.setBehindOffset(120);
menu.setMenu(R.layout.fragment_slideoutmenu);
getFragmentManager()
.beginTransaction()
.replace(R.id.slideOutMenu, new SlideOutMenu())
.commit();
}
I did this in my application...
Created a class Util like this:
public class Util {
public static void atachLeftMenu(final Activity pActivity){
final SlidingMenu menu = new SlidingMenu(pActivity);
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(pActivity, SlidingMenu.SLIDING_WINDOW);
menu.setMenu(R.layout.side_menu);
( (ListView)(menu.findViewById(R.id.side_menu_list)) ).setAdapter(new leftMenuAdapter(pActivity));
( (ListView)(menu.findViewById(R.id.side_menu_list)) ).setOnItemClickListener( new DrawerItemClickListener(pActivity));
//Set menu options and values
}
}
And in every Activity i just call the Util's "atachLeftMenu" method.
Util.atachLeftMenu(this);
If you want to do it within a fragment you'll still have to pass it the activity so you could do something like this:
Util.atachLeftMenu(getActivity());

Fragment slide animation is replacing first fragment before animating

I've followed instructions from dmanargias answer here: Android Fragments and animation
The animations themselves work, however the initial animation when adding a fragment is doing something strange. The initial fragment appears to be replaced with the new fragment before the animation is started.
e.g. One would expect an animation of
A <- B (B sliding from right to cover A)
However as soon as the action starts A instantly becomes B and you get an animation of
B <- B.
When popping the stack you get a correct animation of A -> B (B sliding away revealing A)
This is the code that adds a fragment:
CategoryFragment newFragment = CategoryFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
fragmentTransaction.replace(R.id.fragment, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
Any ideas why this would happen and if there's a way to fix it?
Try this:
final CategoryFragment newFragment = CategoryFragment.newInstance();
final View container = findViewById(R.id.fragment);
container.postDelayed(new Runnable() {
#Override
public void run() {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
transaction.replace(container.getId(), newFragment).commit();
currentFragment = cardFragment;
}
}, 0);
I was having exactly the same problem, but with different animations.
Check that android:shareInterpolator is not set to true in your xml animations.

BackStack fragment get touch or click , what is wrong with my code?

Following code am using while am adding new fragment and adding old fragment to backstack but still old fragment in backstack get click ,what is wrong with my code?
getFragmentManager()
.beginTransaction()
.add(R.id.content_frame, new XyzFragment())
.addToBackStack(null)
.commit();
use this, It is worked for me by using always "replace method" instead of "add method". I never used "add"
Fragment fragment = new YourFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.addToBackStack("Frag");
fragment.setArguments(null);
ft.replace(R.id.content_frame, fragment);
ft.commit();

How to start a Fragment/Activity within Fragment?

I have a button in Fragment. When Button is clicked it has to Open new Fragment/Activity within Fragment. I have written code using Intent,
Intent i = new Intent();
i.setClass(getActivity(), UpdateProfile.class);
startActivity(i);
but its opening in new activity like in below image.
My requirement is in Picture 1. Can someone suggest me how to do it?
EDIT: As suggested by rai and ADK, its working fine but new fragment overlays on old fragment. See the below image. "Change Password"(TextView) is New Fragment which overlays on existing fragment.
Try:
getFragmentManager()
.beginTransaction()
.replace(containerViewId, newFragment)
.addToBackStack(null) // enables back key
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE) // if you need transition
.commit();
You should to use FragmentTransaction enter link description here.
Like this
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.yourFragment, YourFragmentWithImageClass.getInstance());
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.commit();
in your Activity

Categories

Resources