0

Sorry I know it had been asked a lot of times, but I don't find a good tutorial for Slidingmenu.

new attempt (3)

I tried it with an (simple) ListView:
1. attempt like Google
new attempt (1)
new attempt (2)
Google Style & this code (3)
done with: this code

problems

->It worked since I've put it into the external class like Link 3.
->Also to open the menu with the AppIcon desn't work any more
->I just would change the Activity (maybe: Fragments (?))

ActivityMain & the others look like so:

private static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    context=this;
}

@Override
public void onStart(){
    super.onStart();

    final String[] values = getResources().getStringArray(R.array.nav_drawer_items);

    ((ListView) findViewById(R.id.left_drawer)).setAdapter(new ArrayAdapter<String>(this, R.layout.menu_list_item, values));

    NavigationDrawerSetup nds=new NavigationDrawerSetup((ListView) findViewById(R.id.left_drawer), (DrawerLayout) findViewById(R.id.drawer_layout), values, getActionBar(), context);

    nds.confiugreDrawer();
}

NavigationDrawerSetup

public class NavigationDrawerSetup extends Activity{

protected ListView drawerView;
protected String[] drawerList;
protected DrawerLayout drawerLayout;
protected ActionBar actionBar;
protected Activity currentActivity;

protected ActionBarDrawerToggle mDrawerToggle;

public NavigationDrawerSetup(ListView mDrawerView, DrawerLayout mDrawerLayout, String[] mDrawerList, ActionBar actionBar, Context currentActivity) {
    drawerView=mDrawerView;
    drawerLayout=mDrawerLayout;
    drawerList=mDrawerList;
    this.actionBar=actionBar;
    this.currentActivity=(Activity) currentActivity;
}

public void confiugreDrawer() {
    //Android doesn't accept the ListView.setAdapter here
    drawerView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
            case 0:
                Intent menuToMain = new Intent(currentActivity, ActivityMain.class);
                menuToMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuToMain);
                break;
            case 1:
                Intent menuToInfo = new Intent(currentActivity, ActivityInfo.class);
                menuToInfo.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuToInfo);
                break;
            case 2:
                Intent menuToTest = new Intent(currentActivity, ActivitySwitchTest.class);
                menuToTest.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(menuToTest);
                break;
            default:
                new RalaAlertToast(currentActivity, "default");
                break;
            }
        }
    });

    mDrawerToggle = new ActionBarDrawerToggle(currentActivity, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
            actionBar.setTitle(R.string.app_name);
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            actionBar.setTitle(R.string.app_name);
        }
    };

    drawerLayout.setDrawerListener(mDrawerToggle);

    actionBar.setDisplayHomeAsUpEnabled(true);
    actionAPI14();
}

@TargetApi(14)
private void actionAPI14() {
    actionBar.setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The main content view -->

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/popuptestview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:text="@string/view" />

    <EditText
        android:id="@+id/et_subnetTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/popuptestview"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:gravity="right"
        android:hint="@string/app_name"
        android:inputType="textEmailAddress" >

        <requestFocus />
    </EditText>
</RelativeLayout>

<!-- The navigation drawer -->

<ListView
    android:id="@+id/left_drawer"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:fadingEdge="vertical" />

    </android.support.v4.widget.DrawerLayout>

menu_list_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/listitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="@dimen/activity_vertical_margin"
android:textSize="@dimen/menu" />

Log:

03-30 17:33:50.645: E/AndroidRuntime(3840): FATAL EXCEPTION: main
03-30 17:33:50.645: E/AndroidRuntime(3840): java.lang.NullPointerException
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivityForResult(Activity.java:3401)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivityForResult(Activity.java:3362)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivity(Activity.java:3598)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.Activity.startActivity(Activity.java:3566)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at rala.testing.NavigationDrawerSetup$1.onItemClick(NavigationDrawerSetup.java:59)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AbsListView.performItemClick(AbsListView.java:1102)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2790)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.widget.AbsListView$1.run(AbsListView.java:3465)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.os.Handler.handleCallback(Handler.java:730)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.os.Looper.loop(Looper.java:137)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at android.app.ActivityThread.main(ActivityThread.java:5303)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at java.lang.reflect.Method.invokeNative(Native Method)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at java.lang.reflect.Method.invoke(Method.java:525)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
03-30 17:33:50.645: E/AndroidRuntime(3840):     at dalvik.system.NativeStart.main(Native Method)
Community
  • 1
  • 1
rala
  • 895
  • 2
  • 18
  • 43
  • why aren't you using navigationDrawer – A.S. Mar 26 '14 at 12:45
  • where do I get it? - Stock from Google? - I don't understand the demo... – rala Mar 26 '14 at 12:47
  • 1
    yes stock from google. https://developer.android.com/design/patterns/navigation-drawer.html – A.S. Mar 26 '14 at 12:47
  • 1
    https://developer.android.com/training/implementing-navigation/nav-drawer.html – A.S. Mar 26 '14 at 12:48
  • I've tried like this, but I don't know something abojt the most elements & it didn't work... – rala Mar 26 '14 at 12:50
  • so, now I get a menu :D - I tried Google's way again :D; now I want to export it to the external class like the link in _attempt 3_ & and to change the Activity (but like Google - I don't want to open a new 'Window') – rala Mar 29 '14 at 15:04
  • oh and to open the mneu with the app icon doesn't work since the external class – rala Mar 29 '14 at 15:07

1 Answers1

1

Try it like this,

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

activity_menu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00FFFF"
    tools:context=".MainActivity" >

</RelativeLayout>

and here is your activitymain.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    tools:context=".MainActivity" >
</RelativeLayout>

after doing this just swipe left to your screen and you will get another activity.

InnocentKiller
  • 5,234
  • 7
  • 36
  • 84
  • and a menu like the demo app like switching colors? – rala Mar 26 '14 at 12:53
  • Yes, what else you want, you can customize your activity_menu as you want. – InnocentKiller Mar 26 '14 at 12:54
  • sure - and in activity_menu I can style my menu? – rala Mar 26 '14 at 12:57
  • look above, new attempt – rala Mar 27 '14 at 08:03
  • the line 52 is setAdapter() it was working... now I'm trying to give it a layout – rala Mar 27 '14 at 09:14
  • no; it was working before I tried the layout; sorry I forgot a ";" between the last comment – rala Mar 27 '14 at 09:31
  • What you are trying to do with your code, seriously i am not getting any of your point. But if you want to make this app running then please divide your task in some pieces, first of all try to only implement slider if it works then create a new project and try to implement listview and then try to merge your both project. – InnocentKiller Mar 28 '14 at 09:27
  • I'm trying to get a menu like Google Plus to change activities. It works with the main activity, but not with an ListView; I tried it in different ways... - I try both in one while I disable the other one – rala Mar 28 '14 at 09:33
  • Post your all activitie's code as well as it's XML. – InnocentKiller Mar 28 '14 at 09:35
  • done; I've no listerner yet - he doesn't want the adapter, but it's like the tutorial – rala Mar 28 '14 at 09:43
  • like Disa or others with overlay to switch the activity (without icons, only text) I think it's nicer than a simply menu with "option key" – rala Mar 28 '14 at 09:52