Invizzble
Invizzble

Reputation: 55

android navigation drawer doesn't want to get clicked

I have this weird bug that my navigation drawer doesn't seem to realise that it is being clicked. I have searched through a lot of things and I can't seem to find an answer.

 package com.BlackSheepApps.Eventpost.activities;

    import android.app.ActionBar;
    import android.app.Activity;
    import android.content.Intent;
    import android.content.res.Configuration;
    import android.content.res.Resources;
    import android.graphics.drawable.ColorDrawable;
    import android.os.Bundle;
    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.widget.DrawerLayout;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.ListView;

    import com.BlackSheepApps.Eventpost.R;
    import com.BlackSheepApps.Eventpost.adapters.AdapterNavigationDrawer;

public class ActivityBasic extends Activity{

    private ActionBar actionbar;
    private Resources resources;
    private String[] drawerStrings;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private ListView drawerList;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        setContentView(R.layout.drawer_layout);


        //sets the customized action bar
        actionbar = getActionBar();
        resources = getResources();
        actionbar.setBackgroundDrawable(new ColorDrawable(resources.getColor(R.color.opaque_blue)));

        //make the drawer
        drawerStrings = resources.getStringArray(R.array.drawer_categories);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        drawerList = (ListView)findViewById(R.id.left_drawer);

        drawerList.setAdapter(new AdapterNavigationDrawer(this, -1, drawerStrings));
        drawerList.setOnItemClickListener(new DrawerItemClickListener());

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.app_name, R.string.app_name) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        drawerLayout.setDrawerListener(drawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        registerViews();

    }

    /**
     * This method is used for registering the views in the layout, BUT First you'll have to start using a self created method : registerLayout(int layout_id) to register the right layout in the right 
     * place.
     * 
     */
    protected void registerViews(){

    }

    protected void ActionBarItemRemover(){

    }

    protected void registerLayout(int layout_id){
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View childView = inflater.inflate(layout_id, (ViewGroup)findViewById(layout_id));
        drawerLayout.addView(childView);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Log.d("Eventpost", "clicked");
            selectItem(arg2);

        }

    }

    private void selectItem(int position){
        switch(position){
        case 0:
            startActivity(new Intent(this, ActivityHomeScreen.class));
            break;
        case 4:
            startActivity(new Intent(this, ActivityProperties.class));
            break;
        }
    }


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

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        drawerToggle.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 (drawerToggle.onOptionsItemSelected(item)) {
          return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

adapter:

package com.BlackSheepApps.Eventpost.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.BlackSheepApps.Eventpost.R;

public class AdapterNavigationDrawer extends ArrayAdapter<String>{

    String[] button_names;
    Resources resources;

    public AdapterNavigationDrawer(Context context, int resource,
            String[] objects) {
        super(context, resource, objects);
        button_names = objects;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent){
        RelativeLayout row = (RelativeLayout)convertView;

        if(null == row){
            //No recycled View, we have to inflate one.
            LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = (RelativeLayout)inflater.inflate(R.layout.drawer_row, null);
        }

        TextView text = (TextView) row.findViewById(R.id.drawer_text);
        //ImageView image =(ImageView) row.findViewById(R.id.drawer_image);
        text.setText(button_names[pos]);

        return row;
    }



}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

        <TextView
            android:id="@+id/drawer_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="23sp" 
            android:textColor="#FDFFED"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="20dp"/>

</RelativeLayout>

When I try to click on a item in my drawer, the debug code doesn't show in the console, so it doesn't know that it's clicked. And for the record, I did register the activities inside my manifest....

Upvotes: 0

Views: 425

Answers (1)

Atul O Holic
Atul O Holic

Reputation: 6792

On drawer Toggle you are calling, invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() which clearly states that it will call the onPrepareOptionsMenu() method. However it is not implemented and hence your Navigation Drawer is not responding to the clicks.

Implement it as,

/***
 * Called when invalidateOptionsMenu() is triggered
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // if nav drawer is opened, hide the action items
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
    menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
    return super.onPrepareOptionsMenu(menu);
}

source here.

Customize the above as per you code and requirement and it shall work. You also check the link above for a step by step tutorial on this topic. Have fun and happy coding. :)

EDIT:

Do like this, create a boolean variable isDrawerOpen and set it as below,

       public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);

            isDrawerOpen = false;
            // supportInvalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            isDrawerOpen = true;
            // supportInvalidateOptionsMenu(); // creates call to
            // onPrepareOptionsMenu()
        }

then on your item click of Views in the drawer call closeDrawer(); method which is like below,

public void closeDrawer() {
    if (isDrawerOpen) {
        mDrawerLayout
                .closeDrawer((LinearLayout) findViewById(R.id.left_drawer));
    }
}

Upvotes: 1

Related Questions