Unable to get Android HamBurger in actionBar - android

This is what my code looks like, my activity extends Activity. I am having trouble getting the Hamburger menu icon as i get back arrow menu icon. which i dont want.
code in mainactivity:
ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.action_bar);
actionBar.setDisplayShowCustomEnabled(true);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
menu_list = new ArrayAdapter<String>(this, R.layout.drawer_list,
getResources().getStringArray(R.array.menu_options));
mDrawerList.setAdapter(menu_list);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.drawable.ic_drawer ,R.string.app_name, R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
code in action_bar.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#29ABE2" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_vertical"
android:textColor="#FFF"
android:textSize="25dp"
android:text="DataVision" />
</LinearLayout>
additional methods:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle
// If it returns true, then it has handled
// the nav drawer indicator touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

You need to wrap your existing LinearLayout in a DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<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">
then your LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="vertical"
android:background="#29ABE2" >
<TextView
android:id="#+id/textView1"
etc.
...
</LinearLayout>
then the drawer:
<ListView android: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>
see also: https://developer.android.com/training/implementing-navigation/nav-drawer.html

Related

How to use Butterknife in Combination with a navigation header?

I bind some Views with Butterknife but it doesn't work somehow.
The Exception i got is
java.lang.IllegalStateException: Required view 'full_name_nav'
which basically says that he can't find the view. So i think i am binding at the wrong position, but i don't know where i should do it correctly. I am including the Navigation Header Layout in the NavigationView and binding these views in my oncreate method.
My Code:
XML Layout of the Activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main" <!-- Including the Navigation Header here-->
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
XML Layout of the Navigation Header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
app:srcCompat="#android:drawable/sym_def_app_icon" />
<TextView
android:id="#+id/user_name_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:textAppearance="#style/TextAppearance.AppCompat.Body1" />
<TextView
android:id="#+id/full_name_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/motorcycle_model_nav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
My Main Activity where i actually bind the views:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#BindView(R.id.full_name_nav)
TextView navigationName;
#BindView(R.id.motorcycle_model_nav)
TextView navigaitonMotorcycle;
#BindView(R.id.user_name_nav)
TextView navigationUsername;
SharedPreferences prefs;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prefs = this.getSharedPreferences(
"package", Context.MODE_PRIVATE);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this); // Calling ButterKnife here
setNavInfo();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
I implemented the Viewbinding in the OnCreateOptionsMenu, now it works.

Android Hamburger Icon Doing Nothing

I have been able to show the "hamburger" icon on my toolbar, but when I click on it, nothing is happening... The only way to bring my drawer is to slide from the left..
Here's my code:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrayerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrayerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<>(this, R.layout.drawer_list_item, test));
mDrawerList.setOnItemClickListener(new DrawerListClickListener());
mDrawerToggle = new ActionBarDrawerToggle(this, mDrayerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
public void onDrawerClosed(View view){
super.onDrawerClosed(view);
getSupportActionBar().setTitle("Close");
invalidateOptionsMenu();
mDrawerToggle.syncState();
}
public void onDrawerOpened(View drawerView){
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle("Open");
invalidateOptionsMenu();
mDrawerToggle.syncState();
}
};
mDrawerToggle.syncState();
mDrayerLayout.setDrawerListener(mDrawerToggle);
}
Here's my xml file. I don't think it will help but who knows.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar -->
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/my_toolbar">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android: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>
Thanks!
When using the four-parameter constructor for ActionBarDrawerToggle, you need to override the Activity's onOptionsItemSelected() method, and call the toggle's corresponding method to trigger the drawer opening and closing.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
...
return super.onOptionsItemSelected(item);
}

Android Tabs inside sliding drawer

I want to create a navigation drawer that contains tabs in android, but I could not do anything. I want to add tabs (categories, favorites) to the navigation drawer.Is it posible?
(eg. ios)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer,
R.string.app_name, R.string.app_name ){
public void onDrawerClosed(View view) {
// calling onPrepareOptionsMenu() to show action bar icons
// invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// calling onPrepareOptionsMenu() to hide action bar icons
//invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(getApplicationContext(), getSupportFragmentManager(), android.R.id.tabcontent );
tabHost.addTab(tabHost.newTabSpec("surec").setIndicator("Süreç listesi"), SurecListesiFragment.class, null);
tabHost.addTab(tabHost.newTabSpec("bolum").setIndicator("Bölüm listesi"), PhotosFragment.class, null);
I cannot switch between tabs. And I cannot use any widgets on tab content. (listview, button, etc.. )
(EDIT : solution)
(linearlayout : slider menu layout)
linearLayout.bringToFront();
linearLayout.requestLayout();
I did it just yesterday.
This is my layout for main activity with drawer:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Main layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_background"
android:orientation="vertical" >
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_location_spinners_campaigns" />
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/layout_campaignslistview" />
</LinearLayout>
<!-- Slider menu -->
<LinearLayout
android:id="#+id/preferencesDrawer"
android:layout_width="290dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#color/app_background"
android:orientation="vertical" >
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
The method to display the tabs:
FragmentTabHost tabhost;
void buildTabs() {
tabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabhost.addTab(tabhost.newTabSpec("locations").setIndicator(getString(R.string.locations)), FragmentPreferencesLocations.class, null);
tabhost.addTab(tabhost.newTabSpec("categories").setIndicator(getString(R.string.categories)),FragmentPreferencesCategories.class, null);
}
Besides that you need code to display the drawer and customize your tabs...
EDIT:
I initialize drawer with this method (invoke it at onCreate):
ActionBarDrawerToggle drawerToggle;
LinearLayout preferencesDrawer;
FragmentTabHost tabhost;
...
void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_launcher, R.string.app_name,
R.string.app_name) {
public void onDrawerClosed(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.app_name));
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
drawerClosed(drawerView);
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(getString(R.string.preferences));
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
// Invoked by action bar button
void clickFilterCategories() {
toggleDrawer(preferencesDrawer);
}
void toggleDrawer(View drawer) {
if (drawerLayout.isDrawerOpen(drawer)) {
closeDrawer(drawer);
} else {
openDrawer(drawer);
}
}
void openDrawer(View toOpen) {
drawerLayout.openDrawer(toOpen);
}
void closeDrawer(View toClose) {
drawerLayout.closeDrawer(toClose);
}

Open Navigation Drawer onCreate

I'm currently trying to implement a Navigation Drawer into my Application. For that i want the Drawer to be opened when the activity is started. This is my onCreate:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
isInLandscapeMode = getResources().getBoolean(R.bool.isInLandscape);
if (!isInLandscapeMode)
{
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.date, R.string.applicationName)
{
/**
* Called when a drawer has settled in a completely closed
* state.
*/
public void onDrawerClosed(View view)
{
super.onDrawerClosed(view);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView)
{
super.onDrawerOpened(drawerView);
drawerView.bringToFront();
drawerView.requestLayout();
}
};
// Set the drawer toggle as the DrawerListener
drawerLayout.setDrawerListener(drawerToggle);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
drawerLayout.openDrawer(Gravity.START);
}
The Drawer is opening as it should but if i try to click one of my list entries it closes. If I open it manually afterwards everything is working perfectly. Do I have to set the focus on my drawerLayout somehow? I have tried several things but nothing seems to work.
UPDATE
Finally found my error:
I had the following code in my layout xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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" >
<LinearLayout
android:id="#+id/list_container"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f1f1"
android:orientation="horizontal" >
</LinearLayout>
<FrameLayout
android:id="#+id/detail_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
The main content view(detail_container) must be the first child in the navigation drawer thought. So i changed it to this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<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" >
<FrameLayout
android:id="#+id/detail_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
</FrameLayout>
<LinearLayout
android:id="#+id/list_container"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#f1f1f1"
android:orientation="horizontal" >
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</FrameLayout>
And it worked.
Use drawer.openDrawer(Gravity.LEFT);
After you find your drawer layout and Data(list or expandable list) then put
drawerLayout.openDrawer("Name of Data Contained By Drawer");

Navigation Drawer's click event not working

This is my layout . I have a Navigation Drawer and a button in the activity buttons Click event is working but the Click event for Drawer is not working.
<android.support.v4.widget.Drawer Layout
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 -->
<Frame Layout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:background="#333"
android:paddingLeft="15sp"
android:paddingRight="15sp"
/>
<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="#e5e5e5"
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" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
The code i have implemented:
public class MainActivity extends Activity {
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
// 2.3 enable and show "up" arrow
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option
// drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}
}
In XML layout You have provided following attribute to Button tag
android:onClick="actionbtn"
This means method actionbtn will be invoked when user will click Button.
Create method called actionbtn under MainActivity like below with parameter as object of View class.
public void actionbtn(View view){
<< Put your code for drawer action here
drawerLayout.openDrawer(drawerListView)>>;
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
Please note that I am not sure about code to put in method, but creating above method in MainActivity class will solve your issue.
Update -
Please try using following XML layout
<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"
tools:context=".MainActivity" >
<!-- The main content view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#333"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
</android.support.v4.widget.DrawerLayout>
public void onDrawerOpened(View drawerView) {
drawerListView.bringToFront();}
Your java code is perfect. Problem here is with your xml file.
You need to remove this part from android.support.v4.widget.DrawerLayout of the xml code and try then it'll run like a champ.
<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="#e5e5e5"
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" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
Remember you cant include the layout items with navigation drawer items.
Try This
Xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/relative_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="0.5dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

Categories

Resources