AppCompat extension of Views DSL
Supported platforms: Android.
If you want to use this dependency without using one of the fun packs,
you can use Splitties.viewsDslAppcompat
, provided you have refreshVersions added to the project.
For reference, the maven coordinates of this module are com.louiscad.splitties:splitties-views-dsl-appcompat
.
When using an AppCompat theme, the LayoutInflater
replaces the platform
widgets like TextView
and Button
found in your xml layouts by a
compatibility version (i.e. AppCompatButton
, AppCompatTextView
, etc.).
If you're curious to see how it works, look for the method createView
in the
AppCompatViewInflater
class from the androidx.appcompat.app
package.
Since the LayoutInflater
only works on xml, if you use view(::TextView)
with Views DSL,
you get a TextView
instance, not an AppCompatTextView
instance. This means it
will not have AppCompat features and styling (e.g. auto size for TextView
).
However, if you use textView()
it will automatically delegate to
this split if in the dependencies, returning an AppCompatTextView
instance.
This works for all AppCompat widgets.
If you want to use a style defined in appcompat (like Widget_AppCompat_Button_Colored
),
just cache locally an AppCompatStyles
instance and use its properties and functions.
All AppCompat widgets are supported.
Here's the full list:
TextView
ImageView
Button
EditText
Spinner
ImageButton
CheckBox
RadioButton
CheckedTextView
AutoCompleteTextView
MultiAutoCompleteTextView
RatingBar
SeekBar
Just call the related method that is the camelCase version of the PascalCase constructor.
For example, you can call seekBar(…) { … }
and you'll receive an AppCompatSeekBar
instance.
Note that automatically doesn't mean magically. In fact, no reflection is involved (contrary to xml inflation).
You can also see the source of the function that maps to AppCompat widgets versions, and the Initializer that makes it zero initialization on your side.
There's also support for Toolbar
with the toolbar
function, and SwitchCompat
with
the switch
function.
Note that the returned Toolbar
handles config changes.
If your app needs to use AppCompat themed widgets in the non default process, you'll need to manually setup ViewFactory, so it uses AppCompat.
Here's how you need to do it:
- Copy and paste this Initializer into a package of an android library/app module of your project
- Create an internal subclass of
androidx.startup.InitializationProvider
- Declare that subclass in the
AndroidManifest.xml
of the module exactly like it is done here. To do so, copy and paste it, then fix the package of the class under theandroid:name
xml attribute of theprovider
tag, specify theandroid:process
value to the one of your non default process, and finally changed theandroid:name
xml attribute of themeta-data
tag to the initializer you copied in the first step.
Be sure to test it to make sure you have set it up properly.