0

I defined a layout view in a .xml file called menu_list_slide_lateral.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_menu_slide"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnSliBebe"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/bebe" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/foto_diario" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/consejos" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/ajustes" />

</LinearLayout>

Im creating the SlidingMenu from code:

setBehindContentView(R.layout.menu_list_slide_lateral);
setSlidingActionBarEnabled(true);       
slideMenu = getSlidingMenu();
slideMenu.setMode(SlidingMenu.LEFT);
slideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
slideMenu.setShadowWidthRes(R.dimen.shadow_width);
slideMenu.setBehindOffset(100);
slideMenu.setFadeDegree(0.35f);

And mi activity extends from SlidingFragmentActivity:

public class TimelineActivity extends SlidingFragmentActivity

It shows perfectly the menu, but i want to do some actions when the user chooses an option from the menu:

sliding menu options

For example, i want to open another activity when i choose the "Bebe" option. I tried to set a onClick event to that button but it doesn't seem to work, it makes nothing:

inflater = getLayoutInflater();
item = inflater.inflate(R.layout.menu_list_slide_lateral, null);


btnSliBebe = (Button) item.findViewById(R.id.btnSliBebe);
btnSliBebe.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {

        Log.e(TAG, "boton bebe");

    }   
});

How can i access that buttons and asign them a event?

Thanks!

alxsimo
  • 567
  • 1
  • 6
  • 19

1 Answers1

0

What you should to is to create separate fragment for slide menu which will contain your layout R.layout.menu_list_slide_lateral and will handle actions. This is easy to do. Now when you have this fragment you need to insert it into your slide activity.

setBehindContentView which you used is placeholder for menu. So create simple layout which will hold your menu fragment. E.g. call it R.layout.menu_frame and it should be like this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Then in your sliding activity set R.layout.menu_frame as behindContentView and add your menu fragment into this view.

public class BaseSlidingActivity extends SlidingFragmentActivity {

protected MenuFragment mSlidingMenuFragment;
private SlidingMenu mSlidingMenu;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set the Behind View
    setBehindContentView(R.layout.menu_frame);

    //if we create new menu - create new MenuFragment and insert it into         
            //menu_frame
    if (savedInstanceState == null) {
        FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
        mSlidingMenuFragment = new MenuFragment();
        t.replace(R.id.menu_frame, mSlidingMenuFragment);
        t.commit();
    } 
            //if activity was restored(e.g. on orientation change) find it in fragment 
            //manager
            else {
        mSlidingMenuFragment = (MenuFragment) this.getSupportFragmentManager().findFragmentById(R.id.menu_frame);
    }

    // customize the SlidingMenu
    mSlidingMenu = getSlidingMenu();
    mSlidingMenu.setShadowWidthRes(R.dimen.shadow_width);
    mSlidingMenu.setShadowDrawable(R.drawable.shadow);
    mSlidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    mSlidingMenu.setFadeDegree(0.35f);
    mSlidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);


}

I hope it helps. You can browse through source code of example at github slidinfmenu example

Martin Vandzura
  • 3,047
  • 1
  • 31
  • 63