Android 4.x Fragment not inheriting Activity Lollipop Theme - android

Simple question.
Given AppCompat theme with new MaterialDesign in mind in pre-lollipop device.
Given Activity extending ActionBar activity as it is suggested by Google
Given REAL (PostHoneycomb) Fragment on the Activity
Results are
1. Activity has correctly picked up theme with nice accent colors wherever they are needed
Included fragment (throw dynamic transaction adding, not from XML) does not inherit Material Design theme from Activity and has old Honeycomb blue seekbar buttons
The same fragment layout design in AndroidStudio shows correct AppCompat theme
Eplicit theme enforcing on Fragment onCreateView using ContextWrapper doesn't work either.
p.s. Can't post screenshots as I don't have reputation

This was an issue with AppCompat 22.1.0 - update to a newer version (either 22.1.1 which contains the fix or 22.2.0 - the current latest version).

Related

Warning : Use SwitchCompat from AppCompat or SwitchMaterial from Material library

What is the difference between these 2 objects (SwitchCompat and SwitchMaterial)? I have tried them and visually they are identical.
By the way, why did they remove the Switch class? Do you know which UI element is supposed to replace it in the future?
The SwitchMaterial:
is provided by the Material Components Library
extends the SwitchCompat
uses Widget.MaterialComponents.CompoundButton.Switch as default style, using the colors defined in the Theme.MaterialComponents (like colorSecondary, colorSurface and colorOnSurface) and applying the Elevation Overlays in dark mode.
The SwitchCompat:
is provided by the androidx appcompat library
uses Widget.AppCompat.CompoundButton.Switch as default style
By the way, why did they remove the Switch class?
The Switch class is not removed. It is provided by the android framework like other widgets as Button,TextView.. and the appcompat and material components libraries provide an updated version of them (like AppCompatButton, MaterialButton...).
There is a different with these widgets.
Using an AppCompat theme there is the AppCompatViewInflater that automatically replaces all usages of core Android widgets inflated from layout files by the AppCompat extensions of those widgets (for example a Button is replaced by AppCompatButton).
Using the Theme.MaterialComponents there is the MaterialComponentsViewInflater that replaces some framework widgets with Material Components ones at inflation time, provided a Material Components theme is in use (for example a Button is replaced by MaterialButton).
It is NOT true for the SwitchMaterial and the SwitchCompat.The reason for that is due to the AppCompat SwitchCompat not actually extending from the framework Switch class.
https://developer.android.com/reference/androidx/appcompat/widget/SwitchCompat
Switch have a different look for older versions of Android. we use SwitchCompat to have consistent look for all Android versions.
SwitchCompat is a complete backport of the core Switch widget that
brings the visuals and the functionality of that widget to older
versions of the platform. Unlike other widgets in this package,
SwitchCompat is not automatically used in layouts that use the
element. Instead, you need to explicitly use
<androidx.appcompat.widget.SwitchCompat> and the matching attributes
in your layouts.
SwitchMaterial is inherited from SwitchCompat. it is a class that creates a Material Themed Switch.
The switch view operates differently depending on which version of Android you are using. This might cause an issue if your app is being run in older versions or newer versions. To solve this, instead we can use SwitchCompat which operates the same on all versions currently.
To adjust this, go to xml code, instead of switch
// change from "Switch"
<androidx.appcompat.widget.SwitchCompat
android:id="#+id/main_activity_sw_simulate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="9dp"
android:fontFamily="#font/coda"
android:text="#string/switch_text"
android:textColor="#color/onyx"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck" />
SwitchCompat
SwitchCompat is an extended version of CompoundButton. SwitchCompat is a version of the old Switch widget which on devices back to API v7. It does not make any attempt to use the platform provided widget on those devices which it is available normally.
SwitchMaterial
It's an extended version of SwitchCompat. It creates a Material Themed Switch. This class uses attributes from the Material Theme to style a Switch.Because SwitchCompat does not extend Switch, you must explicitly declare SwitchMaterial in your layout XML.

AppCompat theme for Leanback

I opened my old Android TV app project and tried to update the dependencies. I then updated the appcompat libraries to 27.1.1. I opened the leanback fragments and Android Studio warned me the BrowseFragment is deprecated and I should use BaseSupportFragment instead. Same case for other leanback fragments. I then changed all the fragment to AppCompat version (except for LeanbackSettingsFragment as there was no AppCompat version provided) and then changed my Activities extend AppCompatActivity.
After I made these changes, I built the app but when I launched the app I saw this error in logcat:
Unable to start activity ComponentInfo{net.swiftzer.trainboard.dev/net.swiftzer.trainboard.main.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
The theme I used for the app and activities are all under Theme.Leanback. But I checked the theme is based on platform native theme.
Should I stick with the deprecated leanback fragment classes or the leanback library do provides AppCompat themes? The leanback library is like a half-baked one. I saw the leanback library do provides an AppCompat version of fragments last year but no sample projects was using them. Now they marked the fragment classes extended from android.app.Fragment as deprecated but they didn't provide proper way to migrate.
You can't use Theme.Leanback for Activities extending AppCompatActivity. For using support fragments you can't extend the activity with android.app.Activity either. For using support fragments extend your activity with android.support.v4.app.FragmentActivity.
You are not allowed to use AppCompat theme for Android TV with Leanback library.
You should extend your BaseActivity of the FragmentActivity to benefit from the Support Components.

Android: Expanding and collapsing toolbars without AppCompat library

In my application I'm using the theme #android:style/Theme.Material because I like the dark material design.
I would like to implement an expanding and collapsing toolbars like the one in "Build a Material Design App with the Android Design Support Library" from Codelab.
http://imgur.com/1JHP0cP.gif
The problem is that it's require the widget CoordinatorLayout that seems to be incompatible with this theme.
Is there any solution that doesn't require to change the theme ?
If not, is it possible to have a dark material look with AppCompat ?
I replaced #android:style/Theme.Material by Theme.AppCompat and add appcompat-v7 in my dependencies.
Also I replaced every extends Activity by extends AppCompatActivity in my java code.
Now my application can run without this error.

Can I use AppCompat theme with Activity/FragmentActivity classes?

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

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.
}

Categories

Resources