natsumiyu
natsumiyu

Reputation: 3257

How to open the Sliding Navigation Drawer when ImageView is clicked?

I'm trying to implement the Navigation Drawer Activity on my Dashboard Activity. In my application, I have created a Theme where the 'Toolbar/ActionBar' is removed and just put an ImageView for my burger menu on a custom layout and include it on my dashboard layout. What I wanted is that when this ImageView is clicked the sliding drawer will display.

What I tried to do is add an Navigation Drawer Activity by com.project.projectname -> New -> activity -> 'Navigation Drawer Activity' and automatically it adds NavigationActivity class , activity_navigation.xml app_bar_navigation.xml, etc.

But I was wondering how can I open the drawer? In my Dashboard Activity I added this

 public class DashboardActivity extends AppCompatActivity {

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

   View side_tab = findViewById(R.id.side_tab);
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);

  expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // open drawer here 
        }
    });

Also, in NavigationActivity it uses ActionBarDrawerToggle, I must not use this because I don't apply an ActionBar. But what alternative can I use?

Here is my NavigationActivity

public class NavigationActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    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);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.navigation, 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.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Intent goToSreen;

    if (id == R.id.nav_dashboard) {
        goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class);
        startActivity(goToSreen);
    } else if (id == R.id.nav_others) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

Thank you for your kind help.


Update

So in my Dashboard Activity I added the following

NavigationActivity navigationActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {

 super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);

    View side_tab = findViewById(R.id.side_tab);
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);

 expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            navigationActivity = new NavigationActivity() ;
            navigationActivity.drawer.openDrawer(GravityCompat.START);
        }
    });

And in my Navigation Activity I add a global variable

public class NavigationActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DrawerLayout drawer;


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

I have managed to call the drawer in my Dashboard Activity but the sliding menu still doesn't display when I clicked my burger menu. The app crashes and displays java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference

Upvotes: 2

Views: 3696

Answers (3)

natsumiyu
natsumiyu

Reputation: 3257

I extend my NavigationActivity to my DashboardActivity like below

public class DashboardActivity extends NavigationActivity {

    side_tab = findViewById(R.id.side_tab);
    expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);


  expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            side_tab.setVisibility(View.INVISIBLE);
            drawer.openDrawer(GravityCompat.START);
        }
    });

and then follow the said answered by @Hassan Usman and @tahsinRupam

after that I added the DrawerLayout in my dashboard.xml

Upvotes: 1

HassanUsman
HassanUsman

Reputation: 1973

Add this in your NavigationActivity

public class NavigationActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

//Add this line
    public static DrawerLayout drawer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation);

    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
.......

And next change this

expanded_menu.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                // open drawer here 
                NavigationActivity. drawer.openDrawer(GravityCompat.START);
        }
   });

Try this hope it will solve your problrm.

Upvotes: 2

tahsinRupam
tahsinRupam

Reputation: 6405

Add the following code in NavigationActivity:

DrawerLayout mDrawerLayout;    // Declare globally

In onCreate() :

mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
 expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            // open drawer here 
            mDrawerLayout.openDrawer(Gravity.LEFT);
        }
    });

If the drawer is open, close it onBackPressed():

@Override
public void onBackPressed() {
    if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) {
        mDrawerLayout.closeDrawer(Gravity.LEFT);
        return;
    }
}

Hope this helps.

Upvotes: 1

Related Questions