Usage of support library views with material theme - android

When I include views which are available only in support library (FAB , coordinator layout) in my layout, it throws an exception and asks me to use an AppCompat theme. But as my apps min. Sdk is 21 , I am using a Material theme and don't want to change it to AppCompat. What to do in this situation ?

How to stop using AppCompat in Android Studio?
This should be the solution for your problem
But be aware that you can't use app:… in xml, you have to use android:… if possible
And you can't use Constraint layout I think.
Why do you want to avoid AppCombat?

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.

Theme.MaterialComponents.Light.NoActionBar theme affects rendering elements in constraint layout

I used Theme.MaterialComponents.Light.NoActionBar theme and it seems to be affecting constrained layout which is some of elements(such as buttons) can not see in the "Design and Blueprint". But they are appearing properly when the app is running.
I tried the following steps but no use.
1) Try to clean the project
2) Try to invalidate and restart Android Studio
3) Try Rebuilding the project
I tried adding Base key word but it seems to be wrong.
<style name="AppTheme" parent="Base.Theme.MaterialComponents.Light.NoActionBar">
I'm currently using the latest dependency which is 'com.google.android.material:material:1.1.0-alpha07'
It seems to be ok with early dependency such as 'com.google.android.material:material:1.0.0' but I'm wondering why it is not working with the latest releases.
I could use different theme which do not have material components and overcome this problem as well, but it is not what I'm expecting.
Your answers and comments are highly appreciated.
This issue is directly related to the version of material design. I used the latest version of android studio with latest version material design in the constraint layout. The same issue happened when I dropped a button from the palette to layout, it made the layout vanish and I changed the theme to the default, it also worked fine. But as u said, I should use material design. When I used latest material design version( com.google.android.material:material:1.7.0), it didn't work but when I used com.google.android.material:material:1.5.0, it worked fine. That's why u can change the version to find the one which is compatible and not buggy.

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

how to use android material button for android 4 and above?

I'm trying to add simple button on my layout, my min api is 14, The problem is when run the app on android < 5.0 the button doesn't have any animation , How can I fix that?
I think the animation you are talking about is Ripple Effect. That is a feature of material design. And remember the official material design theme is supported for android >= 5.0.
I guess that you are using default theme. So, when you run your app on android 5.0 or above it will using material theme (with ripple effect for button). However, when you run android < 5.0, the theme now is not material theme and button doesn't have ripple effect.
For solve your problem, you need using third party libraries to apply material design to your app. Here is list of awesome ui library and you can find some libraries that help you applying material design.
The list of awesome ui library: https://github.com/wasabeef/awesome-android-ui

Categories

Resources