Difference between an AppCompat view and a normal Android view - android

What is the difference between an AppCompat view component and a standard/default view component?
For example, the difference between an AppCompatEditText, and an EditText, or between an AppCompatButton and a Button.
Looking at the developer docs for android.support.v7.widget, the AppCompat view components are described as "tint aware", but is this the only difference, and what exactly does this do?

When you are using a Button or an EditText you are actually using AppCompatButton and AppCompatEditText. From the official documentation of the AppCompatEditText.
A tint aware EditText. This will automatically be used when you use EditText in your layouts. You should only need to manually use this class when writing custom views

What is the difference between an AppCompat view component and a
standard/default view component?
AppCompat View Component supports compatible features on older version of the platform.
the AppCompat view components are described as "tint aware", but is
this the only difference, and what exactly does this do?
Although most of the AppCompatView only difference is it allows dynamic tint and background tint. Tint aware is not the only difference, each AppCompatView has its own differences, for example.
AppCompatEditText vs EditText
Allows textAllCaps style attribute up to Gingerbread.
AppCompatSpinner vs Spinner
Setting the popup theme using popupTheme.
You can dig down each view difference in Android docs.
However, as Sid / Docs says, you don't have to specify this on your layouts since it will automatically converted to AppCompat views. But, if you want to create custom view, you should use AppCompat Views, or else this bug will happens.

well one of differences that i have noticed , in order to change the background of a normal Button , you have to modify the XML ( to NoActionBar..
android:theme="#style/Theme.AppCompat.Light.NoActionBar" to
android:theme="#style/Theme.MaterialComponents.DayNight.NoActionBar"
whatever all i want to say is it difficult to work with buttons than the AppCompat version ).
so the use of androidx.appcompat.widget.AppCompatButton is somehow considered good because you will avoid a lot of little problems using the androidx library.

Related

Android Exposed Dropdown causes exception

I need to add a drop down to my app, which is running on the AppCompat theme, since we are not using the Material design components, but rather have our own design for buttons, etc.
For a simple feature, I need to use a dropdown and here I think the Exposed drop down from the Material design theme would work nicely.
However, if I'm not specifically using the MaterialComponents theme in my styles, the app will crash whenever I try to use the style on the TextInputLayout.
Is there a way of using the nice Exposed dropdown from MaterialComponents without using the entire theme (as this overrides all colors for buttons and such)? Or maybe just a simple way of getting a nice and simple dropdown otherwise. Spinners seem kinda hard to work with..
Cheers.

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.

Android TV: VerticalGridFragment shadow dimension and color

I'm working on an Android TV application but I'm not quite happy with the standard shadow rendered by the VerticalGridFragment, I'd like to have it smaller and a bit less darker.
I've searched through the code but I didn't find any full working solution.
In my VerticalGridPresenter subclass, the only method I can override is createShadowOverlayOptions but I can't get the result I want.
The only workaround I came up with is to define the following dimensions, so that the ones declared in the Support Library are overridden:
<dimen name="lb_material_shadow_details_z">3dp</dimen>
<dimen name="lb_material_shadow_focused_z">4dp</dimen>
<dimen name="lb_material_shadow_normal_z">3dp</dimen>
But it's more an hack rather than a proper solution.
This is the standard shadow:
This is the result I get with my current hack:
As you can I see, it's smaller but I cannot change the color.
Is there a proper way to set shadow color and dimension for VerticalGridFragment and RowsFragment classes?
I'm using latest Leanback version:
compile 'com.android.support:leanback-v17:25.2.0'
My minSdkVersion is 17 because of a custom Android TV player, but it's fine to have it working starting from 21.
I've actually asked the Leanback team about this before and they said the recommended way of customizing their components is through overriding the styles and dimens. So that might help assuage some of your worry.
However, we also had to customize the shadows on our views. We did this by creating our own views and our own view presenters (instead of using their ImageCardView). With that we were able to set our own shadows at the presentation level.
If you look at the documentation for ListRowPresenter the docs say:
ListRowPresenter applies a default shadow to each child view. Call setShadowEnabled(boolean) to disable shadows. A subclass may override and return false in isUsingDefaultShadow() and replace with its own shadow implementation.
To see more of how they handle shadows, please look into the source code of ListRowPresenter and also check out the ShadowHelper and ShadowHelperApi21 classes to see how they've implemented adding shadows to their list items. We actually just copied over those two classes since they are package-local.
You can also override ShadowOverlayHelper.Options createShadowOverlayOptions() in ListRowPresenter which gives you some ability to change corner radius and focused and unfocused z.

Material design for subclass of EditText

In API 21, we can use colorAccent to change the color of widgets like EditText, RadioButton etc. But it doesn't apply to the subclasses of those widgets. I've tried this but it's just like colorAccent, it applies only to original widgets.
Is there any solution to this? other than having to create a custom drawable then implements it as a style..
For applying colorAccent just extend your custom EditText class from
android.support.v7.widget.AppCompatEditText
Actually as of Android support 22.1 you should subclass AppCompatEditText.
Since, you did not post any source code I'm guessing you're not inflating your view from an XML layout but rater calling new MyCustomEditText.
As the developers site stated: The material theme design can only be applied when loading views using a layout inflater.
This is because the new material design backport hooks into the layout inflation process.
Another reason could be do to the fact that the appcompat v7 library only loads the material design when it finds an EditText in the XML layout. And it does not recognize custom view components.
Google plans to release the Material backport widgets in to the public, they stated that these are currently still in development. You may want to override these special backport widgets it could possibly solve your problem. But since they are not public yet... you can't.

How to activate control tinting on EditBox using new android support library v7

It seems that there is no way to activate color tinting on standard controls (EditBox and CheckBox) on KitKat with new android support v7 library (appcompact). I have tried everything.
I have followed instructions from offical android blog post to the letter:
http://android-developers.blogspot.com/2014/10/appcompat-v21-material-design-for-pre.html
And I got everything working in short time without problems, except color tinting for EditBox and CheckBox.
I am not using some custom edit control (only standard android widgets), and I'm inflating all my layouts using LayoutInflater in fragments. I can only see effects of tinting with new 'SwitchCompat' widget. Oh, just to mention that I've set colorAccent in my theme.
Any ideas, what can I do to make it happen ?
After long struggle :) I got it to work !
Here is how:
All of your Activities must extend from ActionBarActivity, not from FragmentActivity, regardless if you are using actionbar or not, this is a must for tinting in support library.
Second, if you are using adapter for listview LayoutInflater for adapter must be created using themed activity as context, and for that matter any LayoutInflater that you are using must be provided by properly themed activity.

Categories

Resources