Step 1: create an xml file for main activity
<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:layou_height="match_parent"></android.support.v4.widget.drawerlayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"/>
<ListView
android:id="@+id/left_drawer"
android:layout_width="200dip"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/holo_green_dark"
android:dividerHeight="10dp"
android:background="@android:color/white"/>
step 2: creamt xml for navigation drawer
<TextView
android:id="@+id/drawer_itemName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
step 3: creamt xml for fragment which you will inflate
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:orientation="horizontal"
android:gravity="center" >
<TextView
android:id="@+id/frag1_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="You have liked me 10 times"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="18dip"/>
</LinearLayout>
Step 4: create an adpter class that will use to inflate the drawer itm to drawer list view
public class CustomAdapter extends ArrayAdapter<String>
{
Context context;
List<String> drawerItem;
int layoutResID;
public CustomAdapter(Context context,int layoutResID,List<String> drawerItem)
{
this.context = context;
this.drawerItem = drawerItem;
this.layoutResID=layoutResID;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResID, parent, false);
}
TextView ItemName = (TextView) view.findViewById(R.id.drawer_itemName);
ItemName.setText(dItem.getItemName());
return view;
}
Step 5: create an fragment class which will be called on each click of nvigation drawer items.
public class FragmentOne extends Fragment
{
TextView tvItemName;
String myText;
public FragmentOne(String myText)
{
this.myText=myText;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_layout_one, container,false);
tvItemName = (TextView) view.findViewById(R.id.frag1_text);
tvItemName.setText(myText);
return view;
}
}
Step 6: create an activity that will integrate all the above pages and this wii be called when you lauch your application
public class MainActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
CustomAdapter adapter;
FrameLayout frame;
List<String> dataList;
boolean in=false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing
dataList = new ArrayList<String>();
datalist={"Profile","Message","Details"};
getSupportActionBar().setTitle(":)");
mTitle = mDrawerTitle = "MyDrawer";
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
frame=(FrameLayout) findViewById(R.id.content_frame);
adapter = new CustomNavigationDrawerAdapter(this, R.layout.drawer_item,dataList);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
SelectItem(position);
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle=new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(":)");
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{ super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
protected void onPostCreate(Bundle savedInstanceState)
{ super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState(); //to sync the indicator to match the current state of the navigation drawer:
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
public void SelectItem(int position)
{
FragmentManager frgManager = getFragmentManager();
Fragment fragment = null;
Bundle args = new Bundle();
switch (position) {
case 0: fragment = new FragmentOne("This is the fragment called by Profile");
frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;
case 1: fragment = new FragmentOne("This is the fragment called by Messages");
frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;
case 2: fragment = new FragmentOne("This is the fragment called by Details");
frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
0 Comment(s)