sliding drawer appear in all activities
Extending is the right way. Just override setContentView in the right way.
Here's the working example, but instead of drawer, I use a created a custom tabbar:
Define a layout with your drawer like this:
this is act_layout.xml
<LinearLayout
...
android:orientation="vertical"
>
<YourDrawer
...
/>
<FrameLayout
android:id="@+id/act_content"
...
>
// Here will be all activity content placed
</FrameLayout>
</LinearLayout>
This will be your base layout to contain all other layouts in the act_content frame.
Next, create a base activity class, and do the following:
public abstract class DrawerActivity extends Activity {
protected LinearLayout fullLayout;
protected FrameLayout actContent;
@Override
public void setContentView(final int layoutResID) {
// Your base layout here
fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.act_layout, null);
actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content);
// Setting the content of layout your provided to the act_content frame
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
// here you can get your drawer buttons and define how they
// should behave and what must they do, so you won't be
// needing to repeat it in every activity class
}
}
What we do, is basically intercept all calls to setContentView(int resId), inflate our layout for drawer from xml, inflate our layout for activity (by reId provided in method call), combine them as we need, and set as the contentView of the activity.
EDIT:
After you've created the stuff above, just proceed to write an app as usual, create layouts (without any mention of a drawer) create activities, but instead of extending simple activity, extend DrawerActivity, like so:
public abstract class SomeActivity extends DrawerActivity {
protected void onCreate(Bundle bundle) {
setContentView(R.layout.some_layout);
}
}
What happens, is that setContentView(R.layout.some_layout) is intercepted. Your DrawerActivity loads the layout you provided from xml, loads a standart layout for your drawer, combines them and then sets it as contentView for the activity.
How to Display Navigation Drawer in all activities?
in onCreate
of TutorialActivity
don't call setContentView
instead do this:
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.help, null, false);
mDrawer.addView(contentView, 0);
}
make mDrawer
in MainActivity
protected. and in R.layout.activity_main_drawer
just keep drawer tag and the element with gravity left(or right).
Same SlidingDrawer in all activities?
I think your best bet would be to create a parent class with all of the sliding drawer logic. The parent class would extend activity and make sure to setup / populate your sliding drawer, the data could possibly be a singleton also to avoid load duplication. Then all of your activities would extend your SlidingDrawer activity and do their thing.
Android: Navigation drawer on multiple activities
Figured out my issue. The problem was that by overriding setContentView it was called a second time (from HomeActivity OnCreate()) AFTER the ActionBarDrawerToggle had already been set. This means that I was basically overwriting all the work done in the DrawerActivity OnCreate().
Here is my modified code in the DrawerActivity
public class DrawerActivity extends Activity {
protected ActionBarDrawerToggle mDrawerToggle;
private String[] navOptions = new String[] {"Collect Offers", "Exclusive", "Glove Box",
"Servicing", "Dealer", "Settings"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_drawer); NO LONGER REQUIRED
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setContentView(final int layoutResID) {
DrawerLayout fullLayout= (DrawerLayout) getLayoutInflater()
.inflate(R.layout.activity_drawer, null);
LinearLayout actContent= (LinearLayout) fullLayout.findViewById(R.id.content);
DrawerLayout mDrawerLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) fullLayout.findViewById(R.id.nav_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, navOptions));
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
}
}
how to create sliding menu for all Activities
You can achieve this by implementing Navigation Drawer
.
Here is the example which will help you understand it better
Navigation Drawer - android and Android Sliding Menu using Navigation Drawer
Hope this will help you.
Related Topics
What Is the Benefit of Viewholder Pattern in Android
How to Scale/Resize Text to Fit a Textview
Calling Setcontentview() Multiple Times
Android Facebook 4.0 Sdk How to Get Email, Date of Birth and Gender of User
How to Create Socket Connection in Android
Android Adb Devices Unauthorized
Activenetworkinfo.Type Is Deprecated in API Level 28
Set Edittext Digits Programmatically
Programmatically Adding Tablerow to Tablelayout Not Working
Loading Youtube Video Through Iframe in Android Webview
Android Take Screen Shot Programmatically
Disable the Notification Panel from Being Pulled Down
Android SQLite Issue - Table ... Has No Column Named
Simulate Low Network Connectivity for Android
Redraw a Single Row in a Listview
How to Get the Current Page Number of a Viewpager for Android