Reputation: 347
I made a simple app which when I open it I see the wallpaper and the 2 buttons.First button opens my gallery and the second one opens a Calculator app. Now I want to be able to slide from left to right and bring up the Navigation Drawer Activity.
I did this: right clicked on my app -> New -> Activity -> Navigation Drawer Activity. It created 2 java classes ( NavigationActivity
and NavigationDrawerFragment
), some layouts (activity_navigation
fragment_navigation
and fragment_navigation_drawer
). Anyway, what I don't know is how to make the navigation drawer show up, how do I link it to my activity_main
?
This is the activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="@drawable/digital_blue_wallp">
<TextView
android:text="@string/welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#fff8fdff"
android:textSize="24sp"
android:id="@+id/welcome"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:background="@drawable/calcu"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/welcome"
android:layout_alignEnd="@+id/welcome"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:background="@drawable/gallery"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Upvotes: 0
Views: 2124
Reputation: 635
In you main activity layout, declare the navigation drawer like this:
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/left_drawer"
android:layout_width="180dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:paddingTop="10dp"
android:choiceMode="singleChoice"
android:numColumns="2"
android:verticalSpacing="15dp"
android:horizontalSpacing="5dp"
android:background="@color/black_transparent"/>
</android.support.v4.widget.DrawerLayout>
I chose to use a GridView but you can use whatever you want. Then in your activity instantiate the drawer and subview:
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mAppsGrid = (GridView)findViewById(R.id.left_drawer);
You should then create a custom drawer adapter, which is very similar to a grid adapter and simply controls the items within the view.
mDrawerAdapter = new DrawerAdapter(this);
mAppsGrid.setAdapter(mDrawerAdapter);
mAppsGrid.setOnItemClickListener(this));
I know this doesn't specifically tell you what you're doing wrong, but without seeing more of your code, it would just be guess what's going wrong on your app. Hope this helps!
Upvotes: 1