A basic Android navigation box for all activities

I am creating an application that requires the same navigation drawer for all actions. For this, I created a class that extends Activity

(need for child classes) and the code for the navigation box is written there.

public class NavigationDrawerClass extends Activity {
    String [] names = new String[]{"Rohan", "Muthu","Rishi"};
    private ActionBarDrawerToggle mDrawerToggle;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.navigation_drawer_class);
        DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        ListView list = (ListView) findViewById(R.id.left_drawer);
        list.setAdapter(new ArrayAdapter<String>(NavigationDrawerClass.this, android.R.layout.simple_list_item_1, names));
        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                NavigationDrawerClass.this,                  /* host Activity */
                drawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.open_drawer ,  /* "open drawer" description for accessibility */
                R.string.close_drawer  /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle("Drawer Closed");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Drawer Opened");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        drawerLayout.setDrawerListener(mDrawerToggle);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggle
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void setTitle(CharSequence title) {
        getActionBar().setTitle("Navigation Drawer Example");
    }
}

      

Then I try to extend it in some other classes, for example public class MyActivity extends NavigationDrawerClass

, but the navigation drawer doesn't work. Not a single click on the drawer icon or sliding does not affect. If I try to run NavigationDrawerClass

as a stand alone class then it works fine. What additional steps should I take to make the navigation drawer accessible to all classes.

+3


source to share


4 answers


In the comments in the extended Activity, we just find the base DrawerLayout content View class and inflate the extended layout of the activity in it.



public class MyActivity extends NavigationDrawerClass
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
        getLayoutInflater().inflate(R.layout.my_activity, content, true);   
        ...
    }
}

      

+6


source


You can use fragments in your activity. This is the best solution.

But if you want to extend this activity, you must set the layout before calling the super.onCreate () method. And in your layout, for each advanced action, you need to add a navigation drawer.

Example: In NavigationDrawerClass add getLayout () method and call onCreate ()



public class NavigationDrawerClass extend Activity {
      public void onCreate(Bundle saveInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(getLayout());
              // your code here
      }

      protected int getLayout() {
             return R.layout.navigation_drawer_class;
      }
}

      

In child activity use code like this

public class YourActivity extends NavigationDrawerClass{
    public void onCreate(Bundle saveInstanceState) {
         super.onCreate();
         // here do not call setContentView() method
         // all your other code
    }
    @Override
    protected int getLayout() {
         return R.layout.your_activity_xml_layout;
    }
}

      

+1


source


Clean solution for @MikeM's solution is to override setContentView

in base class and do the trick:

@Override
public void setContentView(int layoutResID)
{
    super.setContentView(R.layout.navigation_drawer_class);

    ViewGroup content = (ViewGroup) findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, content, true);
}

      

Then, in activities that continue, just call setContentView

normally:

setContentView(R.layout.other_class);

      

EDIT: Also, in the base class, initialization of the drawer, drawer switch, etc. needs to be done. According to the method onPostCreate

instead of this onCreate

, to avoid being called setContentView

twice (on base onCreate

and on extended class onCreate

):

@Override
protected void onPostCreate(Bundle savedInstanceState)
    super.onPostCreate(savedInstanceState);

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

    ListView list = (ListView) findViewById(R.id.left_drawer);
    list.setAdapter(new ArrayAdapter<String>(NavigationDrawerClass.this, android.R.layout.simple_list_item_1, names));
    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(
            NavigationDrawerClass.this,                  /* host Activity */
            drawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
            R.string.open_drawer ,  /* "open drawer" description for accessibility */
            R.string.close_drawer  /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("Drawer Closed");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("Drawer Opened");
            invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
        }
    };
    drawerLayout.setDrawerListener(mDrawerToggle);
}

      

+1


source


this work for me

public class MyDrawer extends AppCompatActivity {
ActionBarDrawerToggle toggle;
protected RelativeLayout fullLayout;
protected FrameLayout frameLayout;
@Override
public void setContentView(final int layoutResID) {
    fullLayout = (RelativeLayout) getLayoutInflater().inflate(R.layout.mydrawer, null);
    frameLayout = (FrameLayout) fullLayout.findViewById(R.id.drawer_frame);
    getLayoutInflater().inflate(layoutResID, frameLayout, true);
    super.setContentView(fullLayout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout3);
    toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            drawer.closeDrawers();
            int itemId = menuItem.getItemId();
            Toast.makeText(getApplicationContext(), menuItem.getTitle().toString(),
                    Toast.LENGTH_LONG).show();
            //navigationView.getMenu().findItem(R.id.drawer_5_reasons).setChecked(true);
            return true;
        }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
    if (toggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

      

}

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="0dp"
android:id="@+id/drawer_framelayout">
<FrameLayout
    android:id="@+id/drawer_frame2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
<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_layout3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<FrameLayout
    android:id="@+id/drawer_frame"
    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_main2"
app:menu="@menu/activity_main_drawer"
android:background="#fefefd" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

      

for use:

public class yourclass extends MyDrawer {

      

main problam.setOnItemClickListener

<android.support.v4.widget.DrawerLayout>
<FrameLayout>
    your main content stuff here
</android.support.v4.widget.DrawerLayout>
<FrameLayout>
    your main content stuff here(.setOnItemClickListener)

      

0


source







All Articles