0

Hello I want to create a click event with Navigation Drawer Activity in Android Studio. When I click Gallery in left menu I redirected it to Gallery activity with intent. It is okey it works but there is no left menu in Gallery activity. I want all redirected activities have left menu. How can I do this?

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        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.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.
        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();


        if (id == R.id.nav_camera) {

            // Handle the camera action
        } else if (id == R.id.nav_gallery) {
            Intent intent = new Intent(MainActivity.this, GalleryActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_slideshow) {
 Intent intent = new Intent(MainActivity.this, Slideshow.class);
            startActivity(intent);
        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

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

I only changed this with adding intent line.Everything else is default code when I chose NavigationDrawerActivity.

public class GalleryActivity extends AppCompatActivity {

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

Gallery layout

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gamze.leftmenu05.GalleryActivity">


<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName"
    android:text="Name" />

<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

</LinearLayout>
Allen.G
  • 15
  • 6

2 Answers2

1

Yes you can do this with activities also. As activities are little bit hard or heavy in case of loading view and staring screen. that's why fragments are mostly preferred way with navigation drawer.

You can do

  • Create Base activity which have drawer and extend that activity in rest of activities. you can check implementation here and here
  • 2nd option can be create main activity with navigation drawer and in content part of that load all fragments from the menu. For that you can check here
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
  • In this way you are inflating each time a new drawer menu, which is very inefficient IMHO. – Fabio Jul 28 '17 at 14:35
  • @Fabio no that's not going to happen as base activity will keep in memory so it won't load again and view won't get recreated. – Wasim K. Memon Jul 28 '17 at 14:38
  • ah ok, perfect. I always use fragments in such cases, but good to know :) – Fabio Jul 28 '17 at 14:40
  • @androidnoobdev No I could not make it work. I tried first option but not work. – Allen.G Jul 28 '17 at 16:22
  • When I click an activity and if that activity is starting activity then it works. But if it is not starting activity, then application stops – Allen.G Jul 28 '17 at 17:32
0

Because your GalleryActivity has a different layout.

If you want to preserve everything but the content of the main window i suggest you to use a fragment for the gallery instead of an activity

Fabio
  • 782
  • 4
  • 8
  • 25
  • I want to use activity. Is it possible? I want to apply this my application and it has activities. – Allen.G Jul 28 '17 at 14:23