Issue
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.
Solution
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);
...
}
}
Answered By - Mike M.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.