All the support you need. Support libs in Android
-
Upload
vitali-pekelis -
Category
Software
-
view
133 -
download
2
Transcript of All the support you need. Support libs in Android
About me
Shahar Barsheshet
shahar2k5
The beginning
The Android support library was first release at 2011, bringing
support to thousands of devices, old as new.
The Goal: new API for old Android version
Starting with the famous v4.
Support Libraries
What’s inside?
palette
cardview
gridlayout
leanback
recycleview
mediarouter
appcompat
support-v4
multidex renderscript annotations
What’s inside?
ComponentsCompatibility
RecyclerViewCardViewViewPager
etc.
FragmentLoaders
AppCompatetc.
The Present● New features
● Fast Releases
● Bug fixes
● Respond to developers needs
# Design# Leanback# Wear# Percent# Preferences# UI# Utils
How it works?
Intent myServiceIntent = new Intent();ContextCompat.startForegroundService(this, myServiceIntent);
package android.support.v4.content;
public static void startForegroundService(Context context, Intent intent) {if (Build.VERSION.SDK_INT >= 26) {
context.startForegroundService(intent);} else {
// Pre-O behavior.context.startService(intent);
}}
How it works?
Fragments
IllegalStateException: Can not perform this
action after onSaveInstanceState
Calling FragmentTransaction#commit() after Activity’s onSaveInstanceState()
How it works?
Fragments
● Extra cautious when committing transaction in Activity’s lifecycle callbacks!
● Avoid committing in Asynchronous callbacks!
● Use commitAllowingStateLoss() only as a last resort!
Revision 24.0.0 - Added Fragment.commitNow() for synchronous commit.
Revision 25.1.1 - FragmentTransaction.setAllowOptimization(true)
Revision 25.4.0 - Transactions are not allowed during state change.
Revision 26.0.0 - setAllowOptimization(true):deprecated , isStateSaved():added.
Versioning…?
Remove a fragment from backstack and start a new Activity
Ref: https://issuetracker.google.com/issues/37129908
AppCompat
Support library gives us the option to use the same theme for devices with support for Material Design even below API 21.
This means that we can customize one theme to match almost all API levels.
In addition, we get to use some nice components that were added in API 21, like the Floating Action Button, Toolbar, DrawerLayout and more, out-of-the-box.
AppCompat
<resources>
<color name="primary">#3F51B5</color>
<color name="primary_dark">#303F9F</color>
<color name="accent">#FF4081</color>
</resources>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">@color/primary</item>
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
</style>
</resources>
style.xml
colors.xml
Material Design
<item name="colorPrimary">@color/primary</item>
<item name="android:colorPrimary">@color/primary</item>
!=
minSdkVersion="21"
Whats new?
public class CustomFontTextView extends TextView {
private void applyCustomFont(Context context) {Typeface customFont = FontCache.getTypeface("SourceSansPro-Regular.ttf", context);setTypeface(customFont);
}}
Whats new?
Downloadable fonts
● Shrink APK size
● Google fonts catalog - over 800 free fonts
● Android Studio integration
● Font caching across multiple apps
Whats new?
public class ResizableTextView extends TextView {
private void setText(String text) {// measure width and height// calculate text size to fit in view
}}
Whats new?
EmojiCompat
● Downloadable fonts
● Bundled fonts in the project
dependencies {…compile "com.android.support:support-emoji:26.0.1"
}
Whats new?
EmojiCompat
<android.support.text.emoji.widget.EmojiTextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"/>
<android.support.text.emoji.widget.EmojiEditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"/>
<android.support.text.emoji.widget.EmojiButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"/>
Versioning…?
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:support-v4:26.0.2'
compile 'com.android.support:cardview-v7:26.0.2'
compile 'com.android.support:design:26.0.2'
Versioning…?
groupId: com.android.support
artifactId: support-v4
version: 26.0.2
Gradle Dependencies
compile 'com.android.support:support-v4:26.0.2'
Maven
Versioning…?
compile 'com.android.support:support-v4:26.0.2'
Minimum supported API level
compile 'com.android.support:support-v4:26.0.2'
The library version - codebase wise
Versioning…?
android {
compileSdkVersion 26
}
dependencies {
compile "com.android.support:appcompat-v7:26.0.1"}
Support Library revision should be the same as the compiled sdk version
Versioning…?
compile 'com.android.support:appcompat-v7:25.+'DON’T!
compile 'com.android.support:appcompat-v7:26.0.1'compile 'com.android.support:design:25.0.1'
compile 'com.android.support:design:25.3.1'
DON’T!
WARNING!
Versioning…?
ext {
supportLibVersion = '26.0.2'
}
dependencies {
compile "com.android.support:appcompat-v7:${supportLibVersion}"
compile "com.android.support:design:${supportLibVersion}"
compile "com.android.support:support-annotations:${supportLibVersion}"
compile "com.android.support:support-vector-drawable:${supportLibVersion}"
compile "com.android.support:animated-vector-drawable:${supportLibVersion}"
}
TIP! Make your life easier, use a variable for the version
Versioning…?
Revision 26 -> min sdk v14
allprojects {repositories {
maven { url "https://maven.google.com" }}
}
When?
When?1. Targeting latest version but supporting older
versions with the same API’s.
2. Some components are only exists in the Support Libraries.
Android, support me!
To sum it all up
● Use the support libraries!
● Do it right!
● Don’t re-invent the wheel
● Understand what you are doing (or at least try to)
● Bugs - don’t afraid to open!