Can I use AppCompat theme with Activity/FragmentActivity classes? - android

Given the following:
Android documentation says :
AppCompatActivity - Adds an application activity class that can be used as a base class for activities that use the Support Library action bar implementation.
I'm not considering adding action bar to my activity
I need some material design widgets, which I can control only through AppCompat or material theme, but the latest need API >= 21, which is not my case.
I tested Theme.AppCompat.Light.NoActionBar with Activity class and it works well.
--
Is there an issue with using Activity class with AppCompat theme in my case?

The AppCompat library is intended to make compatibility with olders API, so the Theme and all components may work well in older systems.
I think the only concern is to always use the AppCompat elements and not the regular ones.
Example, use AppCompatEditText, AppCompatTextView, etc... And always refers to they with the AppCompat (AppCompatEditText editText;)
I have used a lot the support library and not have others issues, considering the visual elements may be a little different when using an API minor than 21

No.There is not issues with AppcompactActivity & support libaray. You can refere this link
To gain more rich & amazing look go with support library,Try to do material design

Related

Is AppCompat possible to ignore on API 23+?

I am developing an Android Project from scratch that has min 23 API level and uses AndroidX. When I complete the XML layouts (for ex. simple activity_layout) there is options to pick like between TextView & androidx.appcompat.widget.AppCompatTextView.
Is TextView targeting AndroidX?
If yes, can I ignore using androidx.appcompat.widget.AppCompatTextView or other AppCompat component if I target min 23 API level?
Is TextView targeting AndroidX?
The question in not clear but the class androidx.appcompat.widget.AppCompatTextView extends android.widget.TextView.
You can check the official doc.
A TextView which supports compatible features on older versions of the platform.
Also:
This will automatically be used when you use TextView in your layouts and the top-level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.
Then.
If yes, can I ignore using androidx.appcompat.widget.AppCompatTextView or other AppCompat component if I target min 23 API level?
You can ignore this view if you are using the androidx.appcompat:appcompat:x.y.z library.
from the standard android documentation for AppCompatTextView:
This will automatically be used when you use TextView in your layouts
and the top-level activity / dialog is provided by appcompat. You
should only need to manually use this class when writing custom views.
taken from here : https://developer.android.com/reference/android/support/v7/widget/AppCompatTextView , so yes, you can just use TextView as long as your activity extends AppCompatActivity
As pointed out by #Gabriele Mariotti (thank you to him) you should check out the documentation here https://developer.android.com/reference/kotlin/androidx/appcompat/widget/AppCompatTextView for more information regarding androidx AppCompatTextView documentation specifically.
The androidX documentation states the following regarding an AppCompatTextView:
This will automatically be used when you use TextView in your layouts
and the top-level activity / dialog is provided by appcompat. You
should only need to manually use this class when writing custom views
an exact duplicate of the standard android documentation.
My initial answer only made use of this : https://developer.android.com/jetpack/androidx/releases/appcompat to check for any known limitations or history regarding androidx AppCompatTextView
I assumed that:
Like the Support Library, AndroidX ships separately from the Android
OS and provides backwards-compatibility across Android releases.
AndroidX fully replaces the Support Library by providing feature
parity and new libraries.
taken from https://developer.android.com/jetpack/androidx would be sufficient to make that assumption, +1 to him for the help :)

ActionBar does not work, Android

I have a problem with ActionBar.
I set minsdk in AndroidManifest to 14. Next I create Activity (BlankActivity), thats create MyACtivity class which extend ActionBarActivity and import android.support.v7.app.ActionBarActivity. I think that this library need only if you use sdk level 7 or lower.
This import correctly? Or i need use another extend class?
And I try change extend class to Activity, but its does not create ActionBar on Activity.
How add ActionBar on this Activity?
P.S. I was misled, because on my sdk function getActionBar must work, its NullPointer, because my app use this strange import. A can use getSupportActionBar, but its strange use support library for sdk 7 to create Application for sdk 14 or higher.
P.S.S. Thanks!!
If anybody have same problen, there is some links about toolbar:
http://www.101apps.co.za/index.php/articles/using-toolbars-in-your-apps.html
Why was ActionBarActivity deprecated
I have a problem with ActionBar.
Who doesn't ;)
I think that this library need only if you use sdk level 7 or lower.
The appcompat-v7 library used to backport API 14 Action Bar to platforms below that. APIs 7 through 13 used this reimplementation, APIs from 14 used native Action Bar.
Since Lollipop the appcompat-v7 always uses it's own implementation of Action Bar and backports Material theme from Lollipop.
This import correctly? Or i need use another extend class?
To have the Action Bar with appcompat-v7 your activity class must extend AppCompatActivity (previously ActionBarActivity) and it's theme must descend from Theme.AppCompat.* family.
And I try change extend class to Activity, but its does not create ActionBar on Activity.
Native activities on Lollipop don't have any Action Bar by default. You would supply it by having a Toolbar widget in your layout and calling setActionBar(Toolbar). Similar approach can also be used with appcompat-v7 (if you use a theme without default action bar) by calling setSupportActionBar(Toolbar).
How add ActionBar on this Activity?
[...] but its strange use support library for sdk 7 to create Application for sdk 14 or higher.
It's perfectly OK, the goal is to make the app look the same from API 7 to API 22. Appcompat-v7 now backports not only the Action Bar but Material theme as well.
It's better you start with a working example. Just check in you android sdk installation for the folder \samples\android-21\ui\ActionBarCompat-Basic.
The use of the support library is correct, just follow the ActionBar Developer guide here.

Theme.Compat, ActionBar, Fragments, and support libraries

I have an existing app with a minSdk of 14, now targeting 21, and I would like to use Theme.AppCompat to get the "Material look" throughout. This app also uses ActionBar tabs and ViewPager (FragmentPagerAdapter, etc.). I was previously using the native ActionBar and Fragment classes, but in order to use AppCompat from support.v7 it looks like I also have to use support.v7.app.ActionBar and related classes.
The issue I then ran into is that some support library ActionBar classes, such as the ActionBar.TabListener abstract interface, define methods that require the support library version of Fragments (support.v4.app.Fragment, et al). So now I'm faced with extensive modifications throughout the app to import the support library classes, change to getSupportFragmentManager, etc.
Is this the only route I can take for using the AppCompat themes for V14-21 -- switch entirely to the support library for both the action bar and fragments? Do some of these classes pass thru to the native classes at execution time?

Can't use Holo themes with ActionBarActivity child (correct API chosen)

I'm going through the tutorial at developers.android.com and I had problems with styling the action bar. I use the newest SDK (the bundle with Eclipse).
Say, that in values-v14/styles.xml I have
<style name="MessageTheme" parent="#android:style/Theme.Holo.Light.DarkActionBar">
I've tried all the variations of that that I could find. Tried without DarkActionBar in values-v11 as well.
It compiles fine but when I open activity styled as such, app crashes and logcat says
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Like I said, API is set correctly. The target one in project properties (API 19) and android:minSdkVersion="14" in the manifest (tried higher as well).
Now, my activity extends ActionBarActivity (that's how the file was generated). If I make it extend Activity instead, then Holo works fine. That's an answer I found, but I don't understand why that works. What exactly is the difference between ActionBarActivity and Activity that makes this works and is this some hack or is it supposed to be done this way?
Also, that works fine with my additional Activity. If I try to this with the main activity from the tutorial, it doesn't compile because 2 methods used there are undefined - getSupportActionBar and getSupportFragmentManager.
You are using a compatibility library, so to style a support actionBar you need your theme to be descendant of appCompat.
Try this:
<style name="Theme.whatever" parent="#style/Theme.AppCompat.Light.DarkActionBar">
If you are still a little lost, you can generate your theme with this tool: ActionBar style generator and take a look how it's done.
Edit:
Check this out, also: Styling the Action Bar
See "For Android 2.1 and higher"
About the difference between Activity and ActionBarActivity...
As far as I know, you extend ActionBarActiviy if you need to have an action bar while targeting lower than 3.0 android versions. That's why you are having troubles with actionBar or supportActionBar depending on what kind of activity you are coding.
So, to summarize, when working with Acivity call actionBar, but If you are extending ActionBarActivity you should call SupportActionBar. For instance: getSupportActionBar().
More info you could use: Support Library Features
Edit 2: Android is yelling at you because your are trying to use appCompat features. To avoid this in your particular instance, all you need to do is NOT extending ActionBarActivity, but coding regular Activities. Then use ActionBar features as normally you would do.
Edit 3 and probably last:
Let's guess you are using holo as theme, and you are coding a regular Acitivty for API 11 and above. In this case you are not extending ActionBarActiviy, so you don't have to do anything special. That's ok, right? but now, you want the same thing to work for API versions lower than 11 and here comes your real problem. To make that happen you must extend ActionBarActivity, but you didn't. So your only way out (as far as I know) is to have another activity that extends ActionBarActivity, and somehow detect with code, which version of android is running, in order to execute the right code (this is, which class you of the two you should take advantage of) so your app would be able to avoid crashing.
Thats why I think using only appComapt is a nice solution, assuming you don't really need to use holo. But, if you truly want to make things that way...
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
// Use a regular Activity class for API 11 and above.
}
else{
// Use an activity extending ActionBarActivity. Have in mind that here you would be calling a supportActionBar instead of a regular ActionBar.
}

Difference between extending LifecycleActivity,Activity,ActionbarActivity & AppCompactActivity?

In Android what is the main difference between extending Lifecycler Activity, Activity,ActionBarActivity & AppCompactActivity? How do these classes differ from each other in terms of usage?
extending ActionBarActivity gives you the ActionBars functionality on every API level >= 7
by extending Activity you can avoid adding additional projects/libraries to your project but you'll lack the ActionBar on api levels below 11
edit: More details:
ActionBarActivity is part of the Support Library. Support libraries are used to deliver newer features on older platforms. For example the ActionBar was introduced in API 11 and is part of the Activity by default (depending on the theme actually). In contrast there is no ActionBar on the older platforms. So the support library adds a child class of Activity (ActionBarActivity) that provides the ActionBar's functionality and ui
edit2: Update April 2015 - it looks like the ActionBarActivityis deprecated in revision 22.1.0 of the Support Library. AppCompatActivity should be used instead.
edit3: Update Aug 2017 - LifecycleActivity is a LifecycleOwner but:
"Since the Architecture Components are in alpha stage, Fragment and
AppCompatActivity classes cannot implement it (because we cannot add a
dependency from a stable component to an unstable API). Until
Lifecycle is stable, LifecycleActivity and LifecycleFragment classes
are provided for convenience. After the Lifecycles project is
released, support library fragments and activities will implement the
LifecycleOwner interface; LifecycleActivity and LifecycleFragment will
be deprecated at that time."
(copied from the Architecture Components guideline)
If you look carefully, you will see this
public class ActionBarActivity extends FragmentActivity
implements ActionBarDrawerToggle.DelegateProvider TaskStackBuilder.SupportParentable
Here you can read about FragmentActivity:
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
And differences between Activity and FragmentActivity:
Difference between Activity and FragmentActivity
Also, there are some new themes for styling actionBar...
https://developer.android.com/training/basics/actionbar/styling.html
Actionbar is introduced in API level 11. com.android.support:appcompat-v7:+ is a support library which allows you to have an ActionBar in your app for devices running on Android 3.0 or below. So, if you need actionbar below api level 11 your Activity needs to extend ActionBarActivity.
If you are targetting api level 11 and above then you don't need to extend ActionBarActivity and reference AppCompat. You can simply extend Activity and you will have actionabr by default.
Android Studio default project includes it automatically in dependencies and extends ActionbarActivity instead of Activity in order to use it.
The ActionBarActivity or the SupportActionBarActivity have additional methods and properties that are not in a generic Activity. for example methods for adding tabs are present in the ActionBarActivity and not in a generic Activity.
The major difference being you don't get an ActionBar on a generic Activity.
ActionBarActivity just has more support libraries and better usage of the newer themes available from api 11.
"In its most basic form, the action bar displays the title for the activity and the app icon on the left. Even in this simple form, the action bar is useful for all activities to inform users about where they are and to maintain a consistent identity for your app."
You are using Android support library When you come to to the Actionbaractivity . so the uses of Support library is your application can be suport for maximum number of devices. Support library gives to your application the power of Backward compatibilty. Actionbaractivity gives you the mulitiple feature like Actionbardrawer toggle etc.. there are more support libraries available. see this link .. and share to your friends...https://developer.android.com/tools/support-library/index.html

Categories

Resources