I am creating an app which requires same Navigation Drawer for all activities. To do so, I have created a class which extends Activity
(need for child classes) and written the code for Navigation Drawer 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 am trying to extend it in some other classes eg-public class MyActivity extends NavigationDrawerClass
but the navigation drawer is not working. Neither clicking on the drawer icon or sliding has any affect. If I try to run the NavigationDrawerClass
as a stand alone class then it runs perfectly. What additional steps I have to take to make the navigation drawer available for all classes.
Per the comments, in the extended Activity, we're simply finding the base class's DrawerLayout's content View, and inflating the extended Activity's layout into 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);
...
}
}
You can use fragments in your activity. This is better solution.
But if you want to extend this activity you should set layout before call super.onCreate() method. And in your layout for every extended activity must add navigation drawer.
Let' example : In NavigationDrawerClass add method getLayout() and call in 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With