I'm developing an app that has the same action bar for all Activities. The only thing that changes is the menu, which can be easily built dinamically.
At the moment, I have a xml file for each toolbar of each Activity, but there are plenty of code repetition thoughtout the code. How should I deal with it?
I've thought of making a single xml file and including it in the Activities layout. However, there are plenty of stuff that are made programatically, such as definition of the button, title and menu. These methods are located in the Activities onCreate. How is it done to reutilize this code?
Im not sure If I understand you correctly, but maybe make class which extends after Activity (or if you use AppCompatActivity etc.) for example class MyCustomActivity extends AppCompatActivity implement there your actionbar, and then in every new Activity class where you need that ActionBar just extend MyCustomActivity. In this class you can ofcourse implement some public method to set menu in subclasses etc.
Related
There are two principal ways of creating an app bar for an activity in API 21+ using the Toolbar.
Create an activity which extends AppCompatActivity and then follow the instructions here
Create a standalone Toolbarwhich acts as an app bar (define the Toolbar in xml using android.support.v7.widget.Toolbar) and then inflate a menu into it like this: ` toolbar.inflateMenu(R.menu.homeview_menu_common);
My question is: what are the benefits and drawbacks of doing one over the other?`
A related question to this topic can also be found here (How can an activity use a Toolbar without extending AppCompatActivity)
Short answer: No you should make your activity extend AppCompatActivty
You can create a toolbar without AppCompatActivty but besides an app bar the AppCompat also brings with it the support libraries that allow you to add material design to your app going as far back as API level 7 of Android.
Unless there is a specific reason for not using AppCompat all your Activites should extend AppCompatActivty to model a Material app.
You need to use an AppCompatActivity extended Activity because, when you set up the Toolbar as the ActionBar with setSupportActionBar(Toolbar) you get the ability to reference it through Context.getSupportActionBar() from nearly anywhere in your code i.e Fragment. But, if you don't extend AppCompatActivity you can't easily get a reference to the Toolbar from anywhere else other than the Activity in which it was defined.
Say someone wants an Activity which both has an action bar and a preference, the first idea in mind is probably
public class MyActivity extends ActionBarActivity, PreferenceActivity
But Java doesn't allow this. I know API 11+ Activities has actionbar builtin. It's just an example of wondering how to use multiple features from multiple base classes.
EDIT: Based on the feedback it seems we have to hack in this case. IMHO it could be as simple as putting all activity utilities as fields in class Activity and implement getter/setter to use those utilities. Well, in reality, it isn't.
No you cannot extend from two classes in Java. Typically in Android to add the ActionBar to the older PreferenceActivity there are a couple of hacks you can do or libraries that also do the same thing. However, recently with the new AppCompat library they introduced the Toolbar widget which can be used to add an Actionbar to your PreferenceActivity in this case. For more information, checkout this post I recently wrote on how to add a Toolbar to your legacy SettingsActivity.
simple solution:
Firstly you can't extend multiple classes..java does not support multiple inheritance see here
Secondly using action bar sherlock library here, this gives you action bar functionality without extending the actionbaractivity plus its backwards compatiable.
Or...you can implement a custom action bar go here
As mentioned in the other answers, Java doesn't allow multiple inheritance.
If you want an ActionBar as well as something such as Preference functionality, consider using a PreferenceFragment
It's not quite the same as multiple inheritance but Fragments allow adding extra functionality to Activities.
You can create a subclass of the PreferenceActivity, called AppCompatPreferenceActivity (or whatever you would like), to use an AppCompatDelegate to provide the SupportActionBar functionality. You can then subclass the new AppCompatPreferenceActivity for your MyActivity class like so:
public class MyActivity extends AppCompatPreferenceActivity
For how to do this, check out the AppCompatPreferenceActivity sample code from the Chromium project.
I have a ListActivity and I would like to add a Cast button to the menu of this activity. In order to do this, the activity has to extend ActionBarActivity (instead of ListActivity) and use a different style in the AndroidManifest.xml (Them.AppCompat as opposed to AppTheme) which make the list look very different.
Also, my menu had several items, but now only the Cast button is showing up.
Any suggestions on how to maintain the ListActivity properties, but still add a Cast button to the menu?
Thank You,
Gary
A couple of ways, depending on how much flexibility you have and how much work you are willing to do:
See if you can change your activity to inherit from FragmantActivity or ActionBarActivity; then you can still have your list there but as a, say, fragment (e.g. ListFragment). If you can make such a change, Cast button can be added very easily.
If you cannot change that, then you need to have a fully custom solution; you need to add the cast icon/menu and then manage that yourself, you might want to look at this sample project.
As for the theme, in the first option above, you probably want to have your theme inherit from the AppCompat and then customize that accordingly.
I am trying to do inheritance between activities in my application.
I want to create "general activity" style for my all application.
For example, this "general activity" includes toolbar and statusbar. I want to show both in each activity in my program.
I wrote the BaseActivity layout like that:
<include layout="#layout/titlebar" />
<!-- Some control that can contain layout -->
<include layout="#layout/statusbar" />
And MainActivity that extends BaseActivity.
I want to insert in code the layout of the MainActivity to the specific space on BaseActivity.
Is it possible to do that? If yes, how can I set the layout of my inheritance activity in specific spase on this BaseActivity?
There is any control that can contain layout?
This can be possible from java side (Note that I didn't tried yet), here is an example Reusing layout XML and the code behind.
This is can also be done via layouts.
You have to make a separate layout (xml) for you toolbar or status bar, and then you have to include in required layout.
Re-using Layouts will be helpful link and Android Layout Trick #2: Include to Reuse
Sure, its possible.
You'll do it like you do any Java inheritance.
You'll want to create a BaseActivity which will extend Activity.
Your other Activities, will extend BaseActivity.
Not always, but generally, you'll want your BaseActivity to be abstract class. You don't want to create instances of it.
So, you should call setContentView from the EXTENDING Activity. That way each Activity will have its own layout.
I have created a tab fragments in android 2.2 , with a android compatibility support library , now in my application i have few activities some of them are extends Activity class and some of them extends ListActivity.
so how can i convert the existing Activity or ListActivity into Fragments so that i can take the advantage of Fragment features ?
As to create a fragment , one has to extends Fragment class but if an activity is deriving ListActivity then what to do to convert it in a fragment?
You need to review the Fragment documentation and samples on the Android Developers website. This will explain what a Fragment is able to do, and what you should be doing inside of your fragment.
In essence, its a very simple transition over to using Fragments once you have looked over the examples. You will need an Activity to contain the Fragments still.
To make this a lot simpler, I would advise you look into the ActionBarSherlock library, which will allow you to use the ActionBar and SupportLibrary back to 2.1.
To get you started, you will want to use the Fragment and ListFragment classes, which will be very similar to a standard activity, but the life cycles are a little different with a few naming changes.
You could try deriving it from ListFragment