How to reuse a same Navigation Drawer in different activities? - android

My drawer is created by android studio automatically and I realized that its structure is not as it was like when I created handly. API23 suggests developers to use assembled layout files. How can I use the same drawer in different activities with API23?

create xml of only drawer component and include it in every activity layout.
After that to write java code of drawer create CommonActivity class which extends activity and write here drawer now user CommonActivity rather than extending Activity class in every activity class
like
public class CommonActivity extends Activity
{
oncreate
{
//drawer code
}
}
public class YourActivity extends CommonActivity
{
//No need to write drawer code here again
}

Related

Main activity with a menu and map fragment (Android app)

My main activity extends AppCompatActivity where I create and inflate my menu. I also want my main activity to contain a map fragment. This requires that my main activity extends FragmentActivity, but I cannot extend from two super classes. How am I supposed to add a map fragment to my main activity?
AppCompatActivity extends FragmentActivity, so you get all of the functionality in FragmentActivity by extending AppCompatActivity.
Well, AppCompatActivity already extends FragmentActivity, so there should be no problem.

Android Navigation Drawer Implementation

Previously, I used ImageButtons to navigate through Activities. Now I am going to make a standard Left Navigation Drawer. But the Application need to Extend Activity where as the Default Navigation includes Extend NavigationDrawerActivity. How to Handle this?
I need something like:
public class Main extends Activity {
//Bla Bla Bla
}
And the Drawer Sample App already has this piece of cake:
public class Main extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
private NavigationDrawerFragment mNavigationDrawerFragment;
//This and that
}
The ActionBarActivity already extends the Activity class so if your activity extends ActionBarActivity nothing you have will break.
(You will need to have support library v7 in order for you to be able to extend ActionBarActivity)

Do i have to use fragments when using navigation drawer

I am currently building an app, and have decided to implement a navigation drawer for ease of use. currently, nearly all my classes extend activity, however, looking at the navigation drawer examples, they all use fragments. do I have to use fragments for this, or can i keep my classes as activities?
I am only worried, as I have nearly finished the app, and have been told that they do not use the same methods.
No. you can acheive with Activity. Not required to use Fragment.
http://developer.android.com/training/implementing-navigation/nav-drawer.html
Read this reference , this has detail about Drawer layout.
Create a Base class called DrawerBaseActivity which extends Activity. Add all the drawer code to DrawerBaseActivity and Make all your custom activity extends DrawerBaseActivity.
The link reference ask you to replace the fragment on drawerItemClick. But instead of fragment replace create intent to an activity and start your activity.
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
switch (position)
{ case 1 :
// start your respective activity
}
}
}
To keep the rich UI feature you would be better off using fragments.
If you have trouble calling methods you can always call
getActivity( ).TheMethod ( )
That will use the parent activity to call the relevant methods for you.

Access menu class from MainActivity class in android

I have two different classes. My main activity class and a separate menu.class. I want to be able to get the menu class to load from the mainactivity.class. The menu button does not work on my device or emulator when the mainActivity class is running. I am not sure how to call the menu.class from the mainActivity. I do not want to put the menu java code in the mainActivity.
I am asking for the way to add a snip of code to load a menu activity from one class to another, not to fix my code. I can not find any examples online to show how to load a menu class that is not also written in your main activity class.
public class lifeMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.life_menu);
}
That is a snip of my menu activity. My main activity is called MainActivity
You can make your MainActivity extend your MenuActvity that entends Activity. Hard to know since you did not provide a snip of your menu class.

How do I create common code for parts of Android activities?

In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?
The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}
Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers

Categories

Resources