Unable To See Navigation Drawer
Solution 1:
seems that there should be problem in your layout file.
Remember that
DrawerLayout
allows maximum2 child views. Have a look at this Google Doc.
It should be like:
<android.support.v4.widget.DrawerLayoutxmlns: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 --><FrameLayoutandroid:id="@+id/content_frame"android:layout_width="match_parent"android:layout_height="match_parent" /><!-- The navigation drawer --><ListViewandroid:id="@+id/left_drawer"android:layout_width="240dp"android:layout_height="match_parent"android:layout_gravity="start"android:choiceMode="singleChoice"android:divider="@android:color/transparent"android:dividerHeight="0dp"android:background="#111"/></android.support.v4.widget.DrawerLayout>
Solution 2:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/header"
layout="@layout/menu"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="@+id/frame"
android:textSize="24sp"
android:gravity="center"
android:layout_marginTop="50dp"/>
<ListView
android:id="@+id/list_item"
android:layout_width="200dp"
android:layout_height="match_parent"
android:dividerHeight="1dp"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/></android.support.v4.widget.DrawerLayout>
MainActivity.java
publicclassMainActivityextendsActivity {
String[] names = {"android","java","spring","html"};
ListView list;
FrameLayout frame;
ActionBarDrawerToggle action;
DrawerLayout drawer;
Button but;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
frame = (FrameLayout) findViewById(R.id.frame);
list = (ListView) findViewById(R.id.list_item);
but = (Button) findViewById(R.id.menu);
ArrayAdapteradapter=newArrayAdapter(this, android.R.layout.simple_list_item_1, names);
list.setAdapter(adapter);
action = newActionBarDrawerToggle(this, drawer, R.string.drawer_open, R.string.drawer_close) {
/* Called when drawer is closed */publicvoidonDrawerClosed(View view) {
//Put your code here
}
/* Called when a drawer is opened */publicvoidonDrawerOpened(View drawerView) {
//Put your code here
}
};
drawer.setDrawerListener(action);
but.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
if (drawer != null) {
if (drawer.isDrawerOpen(list)) {
drawer.closeDrawer(list);
} else {
drawer.openDrawer(list);
process();
}
}
}
});
}
privatevoidprocess() {
list.setOnItemClickListener(newAdapterView.OnItemClickListener() {
@OverridepublicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentTransaction ft=getFragmentManager().beginTransaction();
switch (names[position])
{
case"android":
{
yourClass obj=newyourClass();
//ft.replace(R.id.frame,obj);break
}
case"java":
{
yourClass obj=newyourClass();
//ft.replace(R.id.frame,obj);break;
}
case"spring":
{
yourClass obj=newyourClass();
//ft.replace(R.id.frame,obj);break;
}
case"html":
{
yourClass obj=newyourClass();
//ft.replace(R.id.frame,obj);break;
}
default:
{
Toast.makeText(MainActivity.this,"you have to click another one",Toast.LENGTH_LONG).show();
}
}
ft.commitAllowingStateLoss();
list.setItemChecked(position, true);
drawer.closeDrawer(list);
}
});
}
}
menu.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"tools:context=".MainActivity"android:layout_height="wrap_content">
<Button
android:id="@+id/menu"
android:title="menu"
android:icon="@drawable/menu"
android:background="@drawable/menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#0a0a0a" /></LinearLayout>
Solution 3:
You need to toggle the open/close of the drawer. To do so, you can do the following :
1 - Implement the toggleDrawer method
public void toggleDrawer(boolean shouldBeVisible){
if(shouldBeVisible){
dlayout.openDrawer(dlayout);
} else {
dlayout.closeDrawers();
}
}
2 - Call it on onCreate (note : this is just for testing)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
toggleDrawer(View.VISIBLE);
}
3 - Like Daryl said, implement a button so you can actively toggle (open and close the drawer to your liking)
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(newOnClickListener() {
@OverridepublicvoidonClick(View v){
if(dlayout.getVisiblity() == View.VISIBLE) {
// drawer is visible -> closetoggleDrawer(false);
} else {
// drawer is gone/invisible -> opentoggleDrawer(true);
}
}
})
4 - (Make sure your DrawerLayout contains only 2 child views)
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
Note : Forgot to add this but is indeed an invaluable reference (thanks to @Meet, upvoted)
Post a Comment for "Unable To See Navigation Drawer"