ActionBarCompat and MapActivity - android

I've successfully converted most of my app to use ActionBarCompat by extending ActionBarActivity on most activities, however... I have a MapActivity class and need this to work here as well.
How would I go about creating a new class based on ActionBarActivity and having it extend MapActivity and would that even work?
The sample that Google provides in the ../samples/android-17/ActionBarCompat directory has a lot of these java files (ActionBarActivity.java, etc.) but also tons of required layouts, styles, drawables, etc., etc. and I wasn't sure if adding all of those files to my project is the only way to get this one map screen to work. Seems like there has to be a better way right?

You can't create an activity that inherits from both. You'll have to create a MapFragment.

Related

Extending a class with more than one activity with interface in android

Basically, I'm new with android and java and I know this question was asked for too many times, but I couldn't understand something.
I'm reading a guide that says I need to extend AppCompatActivity, so I could use the toolbar benefits from this class. Then I read another guide that says I need to extend another class, such as ExpandableListActivity. Both those classes weren't created by me. How can I create such an interface that can include both the classes, and implement it?
The thing you are trying to achieve isn't supported by java and you should make peace with it before embracing Android. The best thing you can do is implement the list functionality by yourself inside the same activity extending AppCompatActivity.

How to extend multiple Android activities

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.

Add Action Bar without Subclassing ActionBarActivity

Is it possible to add an Action Bar to an android application:
1) without subclassing ActionBarActivity
2) support for gingerbread and newer
I've searched google and SO, no results.
The reason I ask this is because I have an activity that already subclasses from another library, and I can't make the ActionBarActivity the root subclass.
Normally no, multiple inheritance isn't part of Java.
Of course, the real question is if ActionBarActivity will actually be useful on Gingerbread. It depends on what specific functionality you need from it.
What you can try to do:
Make your own "ActionBar" via layout.
If the library you're using is open source, modify it so its Activities extend ActionBarActivity instead.
If not, both ActionBarCompat is open source - you can download the source and incorporate the functionality into your Activity. ActionBarActivity does extend FragmentActivity, so you may need to work with the raw support-library source as well.
I know the answer for Q2 is YES. You use support library v7 to support Action Bars on devices running GingerBread (Eclairs and Froyos as well).
For Q1, i believe the answer is an YES. You just use the Window.requestFeature() to add Action Bars. But i am not very sure about this.
HTH.

Using DialogFragment

I want to throw a Dialog to the user of my app to confirm some action. From the developers API guides I learnt that they prefer using DialogFragments instead of dialog class.The activity which should show the dialog is a ListActivity and already has been coded.
My question is, if I need to use the DialogFragment then my activity may still extend ListActivity? Or I need to extend FragmentActivity (my app has min sdk 2.3.6) as I know android does not support multiple inheritence?
I dont want to use ListFragments
Thanks for any help.
I think you need to use the android-support-v4 library and then create a class that extends FragmentActivity but import this line android.support.v4.app.FragmentActivity; to support fragments from v2.3.6 and up. Change your ListActivity class to then extend ListFragment and invoke the class from your FragmentActivity. You can then use the DialogFragment with other fragments. This will change the whole structure of your project but it will force you to use better up to date api code.
Seeing that you don't want to use ListFragment I wouldn't go through all this trouble of converting all your classes into Fragments just to use DialogFragment you can just use normal Dialogs, they aren't a deprecated api so I wouldn't say there's anything wrong with them.
I think the reason DialogFragments are preferred is because they want you to use Fragments when developing your app, due to versatility for Tablet apps ect.
Hope this helps

Using multiple inheritence of activity

Is there way to inherit two different activities in android. I have to display the Map on my activity which inherit from some other activity. i want to display a map on that activity but i can't display the map without inheriting MapActivity. Is there any other way to display the map without using MapActivity.
No, android is java based. And java do not support multiple inheritance.
Java supports multiple interfaces.
Maybe using fragments will help. Fragments could simulate multiple activities. However, all "activities" must be available as fragment. I am not sure whether there exist one for maps
java does not support Multiple Inheritance, although you can come up with a clever design that will let you use functionality of multiple objects in your Activity.
read following article
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html
I modified the pattern listed by Mayank to assume that one base Activity doesn't change. I also made some tweak to show how arguments would work, considering activities will need access to base activity. In the following link, assume map activity would be BaseActivityAlpha. Here is my posting: http://www.anotherandroidblog.com/2013/01/03/extending-from-two-activities

Categories

Resources