Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Support Library 総復習

Support Library 総復習

DroidKaigi 2016 Day2で発表した資料です。
This is a reference that was presented at DroidKaigi 2016 Day 2.

Takahiro Shimokawa

February 19, 2016
Tweet

More Decks by Takahiro Shimokawa

Other Decks in Programming

Transcript

  1. Support Library
    total review
    Takahiro Shimokawa ( @androhi )
    1 DroidKaigi 2016

    View Slide

  2. I am ...
    • engineer at Zaim Inc.
    • DroidKaigi 2015 & 2016 staff
    • DroidKaigi 2015 & 2016 speaker
    2 DroidKaigi 2016

    View Slide

  3. Previous DroidKaigi on April 25, 2015
    3 DroidKaigi 2016

    View Slide

  4. Previous DroidKaigi on April 25, 2015
    4 DroidKaigi 2016

    View Slide

  5. Support Library?
    The Android Support Library package is a set of code
    libraries that provide backward-compatible versions of
    Android framework APIs as well as features that are
    only available through the library APIs.
    — Android Developers
    5 DroidKaigi 2016

    View Slide

  6. Chronicle-1
    The release date of android
    OS and the number of
    Support Library1
    1 Reference: Android developers - API Levels and Android version history.
    6 DroidKaigi 2016

    View Slide

  7. 7 DroidKaigi 2016

    View Slide

  8. Chronicle-2
    Interval of updates2
    2 Reference: Android Developers - Support Library Revisions.
    8 DroidKaigi 2016

    View Slide

  9. 9 DroidKaigi 2016

    View Slide

  10. Why does it need?
    In order to implement the
    function across different API
    level.
    10 DroidKaigi 2016

    View Slide

  11. 11 DroidKaigi 2016

    View Slide

  12. What can we do?
    1. We can better implementation. (Not the best)
    • e.g. Use of RxJava
    2. We can easily implement a user interface that
    Google is recommended.
    • e.g. Implementation of Material design
    12 DroidKaigi 2016

    View Slide

  13. Sorry, exclude these today.
    • Android Testing Support Library
    • Data Binding Library
    13 DroidKaigi 2016

    View Slide

  14. V series
    • v4 Support Library
    • v7 Support Library
    • v8 Support Library
    • v13 Support Library
    • v14 Preference Support Library
    • v17 Preference Support Library
    • v17 Leanback Library
    14 DroidKaigi 2016

    View Slide

  15. v4 Support Library
    • Android 1.6 (API level 4) and higher
    • With the biggest API set in Support Library
    • Without resouces
    dependencies {
    compile 'com.android.support:support-v4:23.1.1'
    }
    15 DroidKaigi 2016

    View Slide

  16. Frequently used class
    • Fragment
    • ViewPager
    • NotificationCompat
    • DrawerLayout
    • SwipeRefreshLayout
    • Loader (AsyncTaskLoader / CursorLoader)
    16 DroidKaigi 2016

    View Slide

  17. Demo: DrawerLayout
    • Including v7-appcompat and
    design support library
    • ActionBarDrawerToggle
    • NavigationView
    17 DroidKaigi 2016

    View Slide

  18. AndroidStudio Templete: Navigation Drawer
    Activity
    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
    this, drawer, toolbar, R.string.xx, R.string.xx);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    ...
    18 DroidKaigi 2016

    View Slide

  19. Demo: SwipeRefreshLayout
    • UI customize
    • setColorScheme(int...
    colors)
    19 DroidKaigi 2016

    View Slide

  20. The Class that I want you to remember
    NestedScrollView
    >> There is a case to be required in Design Support
    Library.
    20 DroidKaigi 2016

    View Slide

  21. Helpful class
    Many class named ʙCompat
    Helper for accessing features in [name of class]
    introduced after API level 4 in a backwards compatible
    fashion.
    21 DroidKaigi 2016

    View Slide

  22. Example: NotificationCompat
    Helper for accessing features in Notification introduced
    after API level 4 in a backwards compatible fashion.
    — NotificationCompat - Class overview
    22 DroidKaigi 2016

    View Slide

  23. // v4 Support Library
    Notification notification = new NotificationCompat.Builder();
    .setContentTitle("Title")
    .setContentText("message")
    .setSmallIcon(R.drawable.new_mail)
    .build();
    // android.app
    Notivication notification = new Notification.Builder(context);
    .setContentTitle("Title")
    .setContentText("message")
    .setSmallIcon(R.drawable.new_mail)
    .build();
    23 DroidKaigi 2016

    View Slide

  24. v7 Support Library
    • Android 2.1 (API level 7) and higher
    • You can be included independently.
    • With resouces
    24 DroidKaigi 2016

    View Slide

  25. Each v7 library
    1. v7-appcompat
    2. v7-cardview
    3. v7-gridlayout
    4. v7-mediarouter
    5. v7-palette
    6. v7-recyclerview
    7. v7-preference
    25 DroidKaigi 2016

    View Slide

  26. Frequently used library
    • v7-appcompat
    • v7-recyclerview
    26 DroidKaigi 2016

    View Slide

  27. v7-appcompat
    • Depend on v4-support.
    • Support for material design user interface
    implementations.
    dependencies {
    compile 'com.android.support:appcompat-v7:23.1.1'
    }
    27 DroidKaigi 2016

    View Slide

  28. i.e. depend on v4-support
    $ gradle -q dependencies app:dependencies
    ------------------------------------------------------------
    Root project
    ------------------------------------------------------------
    No configurations
    ------------------------------------------------------------
    Project :app - Injects the build id used by the Fabric SDK.
    ------------------------------------------------------------
    _debugCompile - ## Internal use, do not manually configure ##
    +--- com.android.support:appcompat-v7:23.0.1 (*)
    | +--- com.android.support:support-v4:23.0.1 (*)
    ...
    28 DroidKaigi 2016

    View Slide

  29. i.e support for material design implementations
    e.g. AlertDialog
    // v7-appcompat
    new android.support.v7.app.AlertDialog.Builder(context)
    .setTitle("Hi!")
    .setMessage("Welcome to DroidKaigi 2016.")
    .setNegativeButton("close", null)
    .show();
    // android.app
    new AlertDialog.Builder(context)
    .setTitle("Hi!")
    .setMessage("Welcome to DroidKaigi 2016.")
    .setNegativeButton("close", null)
    .show();
    29 DroidKaigi 2016

    View Slide

  30. 30 DroidKaigi 2016

    View Slide

  31. v7-recyclerview
    • There is also a case to replace the ListView of
    ViewHolder pattern.
    • It increases the degree of freedom in the display of
    the List format.
    dependencies {
    compile 'com.android.support:recyclerview-v7:23.1.1'
    }
    31 DroidKaigi 2016

    View Slide

  32. e.g. Calendar view with v7-
    recyclerview
    • Customized LayoutManager
    • Customized ItemDecoration
    32 DroidKaigi 2016

    View Slide

  33. Create calendar view
    RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.calendar_recyclerview);
    // appearance of the cell
    CalendarLayoutManager layoutManager = new CalendarLayoutManager(getActivity(), 7);
    recyclerView.setLayoutManager(layoutManager);
    // draw the separator
    recyclerView.addItemDecoration(new CalendarDividerItemDecoration(getActivity()));
    mAdapter = new CalendarAdapter(getActivity(), mCurrentYear, mCurrentMonth);
    mAdapter.setOnActionListener(this);
    recyclerView.setAdapter(mAdapter);
    33 DroidKaigi 2016

    View Slide

  34. Other v7: cardview
    • Support for the CardView widget.
    dependencies {
    compile 'com.android.support:cardview-v7:23.1.1'
    }
    34 DroidKaigi 2016

    View Slide

  35. Other v7: gridlayout
    • Support for the GridLayout class.
    dependencies {
    compile 'com.android.support:gridlayout-v7:23.1.1'
    }
    35 DroidKaigi 2016

    View Slide

  36. Other v7: palette
    • Add the Palette class which lets you extract
    prominent colors from an image.
    dependencies {
    compile 'com.android.support:palette-v7:23.1.1'
    }
    36 DroidKaigi 2016

    View Slide

  37. Other v7: preference
    • Add preference object, such as ChexboxPreference
    and ListPreference.
    • Add v4-support based PreferenceFragmentCompat
    class.
    dependencies {
    compile 'com.android.support:preference-v7:23.1.1'
    }
    37 DroidKaigi 2016

    View Slide

  38. Other v7: mediarouter
    • Provide MediaRouter and MediaRouteProvider class.
    • Google recommend using the library only in
    connection with Google Cast.
    dependencies {
    compile 'com.android.support:mediarouter-v7:23.1.1'
    }
    38 DroidKaigi 2016

    View Slide

  39. v8 Support Library
    • Android 2.2 (API level 8) and higher
    • Support for the RenderScript computation
    framework.
    defaultConfig {
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true
    }
    39 DroidKaigi 2016

    View Slide

  40. What is the RenderScript?3
    • High-speed drawing processing using GPU.
    • Not tied to a machine-architectures like AndroidNDK.
    3 Reference: Android Developers - RenderScript.
    40 DroidKaigi 2016

    View Slide

  41. v13 Support Library
    • Android 3.2 (API level 13) and higher
    • Support for using the ViewPager with
    "android.app.Fragment".
    dependencies {
    compile 'com.android.support:support-v13:23.1.1'
    }
    41 DroidKaigi 2016

    View Slide

  42. v14 Preference Support
    Library
    • Support for PreferenceFragment using
    "android.app.Fragment".
    dependencies {
    compile 'com.android.support:preference-v14:23.1.1'
    }
    42 DroidKaigi 2016

    View Slide

  43. v17 Preference Support
    Library
    • Provide preference interfaces on TV devices
    dependencies {
    compile 'com.android.support:preference-leanback-v17:23.1.1'
    }
    43 DroidKaigi 2016

    View Slide

  44. Reference image
    44 DroidKaigi 2016

    View Slide

  45. v17 Leanback Library
    • Support for use the UI widgets for TV apps.
    • Provide the theme of Theme.Leanback.
    • Depend on v4-support and v7-recyclerview.
    dependencies {
    compile 'com.android.support:leanback-v17:23.1.1'
    }
    45 DroidKaigi 2016

    View Slide

  46. Frequently used class
    • BrawseFragment
    • DetailFragmentFragment
    46 DroidKaigi 2016

    View Slide

  47. Demo: androidtv-leanbackGitHub
    For me all access to local network and internet do not
    work with android tv emulator sdk 22 and 23.
    — Playback/Streaming does not work. #50
    GitHub https:/
    /github.com/googlesamples/androidtv-Leanback
    47 DroidKaigi 2016

    View Slide

  48. e.g. androidtv project
    48 DroidKaigi 2016

    View Slide

  49. BrawseFragment
    Have a dedicated listener
    setOnSearchClickedListener(View.OnClickListener);
    setOnItemViewClickedListener(OnItemViewClickedListener);
    setOnItemViewSelectedListener(OnItemViewSelectedListener);
    49 DroidKaigi 2016

    View Slide

  50. Speciality series
    • Multidex Support Library
    • Annotations Support Library
    • Design Support Library
    • Custom Tabs Support Library
    • Percent Support Library
    • Recommendation Support Library for TV
    50 DroidKaigi 2016

    View Slide

  51. Multidex Support Library
    • Building apps with multiple Dalvik Executable(DEX)
    files.
    • Without resouces
    51 DroidKaigi 2016

    View Slide

  52. What case does multidex need?
    If you get in such a build error...
    Conversion to Dalvik format failed:
    Unable to excute dex: method ID not in [0, 0xffff]: 65536
    52 DroidKaigi 2016

    View Slide

  53. What case does multidex need?
    Or that's recently...
    trouble writing output:
    Too many field references: 131000; max is 65536.
    You may try using --multi-dex option.
    53 DroidKaigi 2016

    View Slide

  54. How to create the multidex app?
    android {
    compileSdkVersion 21
    buildToolsVersion "21.1.0"
    defaultConfig {
    ...
    minSdkVersion 14
    targetSdkVersion 21
    ...
    // Enabling multidex support.
    multiDexEnabled true
    }
    ...
    }
    dependencies {
    compile 'com.android.support:multidex:1.0.0'
    }
    54 DroidKaigi 2016

    View Slide

  55. Optimizing multidex development
    builds
    For the development flavor, set a minimum SDK version
    of 21. This setting generates multidex output much
    faster using the ART-supported format. For the release
    flavor, set a minimum SDK version which matches your
    actual minimum support level.
    — from Android Developers
    55 DroidKaigi 2016

    View Slide

  56. Build configuration sample
    android {
    productFlavors {
    // Define separate dev and prod product flavors.
    dev {
    // dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
    // to pre-dex each module and produce an APK that can be tested on
    // Android Lollipop without time consuming dex merging processes.
    minSdkVersion 21
    }
    prod {
    // The actual minSdkVersion for the application.
    minSdkVersion 14
    }
    }
    ...
    buildTypes {
    release {
    runProguard true
    proguardFiles getDefaultProguardFile('proguard-android.txt'),
    'proguard-rules.pro'
    }
    }
    }
    dependencies {
    compile 'com.android.support:multidex:1.0.0'
    }
    56 DroidKaigi 2016

    View Slide

  57. What happens when you create the
    multidex app?4
    $ unzip app-debug.apk
    extracting: ...
    inflating: ...
    $ ls
    AndroidManifest.xml app-normal-debug.apk classes.dex lib res
    META-INF assets classes2.dex org resources.arsc
    $ ls *.dex
    classes.dex classes2.dex
    4 Reference to Building Apps with Over 65K Methods
    57 DroidKaigi 2016

    View Slide

  58. Annotations Support Library
    • Support for annotation metadata.
    • With resouces
    dependencies {
    compile 'com.android.support:support-annotations:23.1.1'
    }
    58 DroidKaigi 2016

    View Slide

  59. e.g. @NotNull
    private void something() {
    setMessage(null); // Out warning
    setMessage("message"); // Not out warning
    }
    private void setMessage(@NotNull String message) {
    ...
    }
    59 DroidKaigi 2016

    View Slide

  60. Various annotation
    Check whether resouce id
    -> @StringRes / @LayoutRes / @DrawableRes
    Using like enum
    -> @IntDef / @StringDef
    Check the executing thread
    -> @MainThread / @WorkerThread
    and more...
    60 DroidKaigi 2016

    View Slide

  61. Design Support Library
    • Support for material design components and
    patterns.
    • With resouces
    dependencies {
    compile 'com.android.support:design:23.1.1'
    }
    61 DroidKaigi 2016

    View Slide

  62. For new components
    • FloatingActionButton
    • NavigationView
    • Snackbar
    62 DroidKaigi 2016

    View Slide

  63. For design pattern
    • AppBarLayout
    • CollapsingToolbarLayout
    • CoordinatorLayout
    • TabLayout
    • TextInputLayout
    63 DroidKaigi 2016

    View Slide

  64. Demo:
    CoordinatorLayout /
    AppBarLayout /
    FloatingActionButton
    • Each component move with
    together.
    64 DroidKaigi 2016

    View Slide

  65. Structure of the layout file


    65 DroidKaigi 2016

    View Slide

  66. Structure of the layout file



    app:layout_scrollFlags="scroll|enterAlways"/>


    66 DroidKaigi 2016

    View Slide

  67. Structure of the layout file



    app:layout_scrollFlags="scroll|enterAlways"/>


    android:layout_gravity="bottom|end"/>

    67 DroidKaigi 2016

    View Slide

  68. Structure of the layout file



    app:layout_scrollFlags="scroll|enterAlways"/>




    android:layout_gravity="bottom|end"/>

    68 DroidKaigi 2016

    View Slide

  69. Animation of FAB
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    CoordinatorLayout.LayoutParams params =
    (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    // Set custom behavior class
    params.setBehavior(new ScrollingFabBehavior());
    69 DroidKaigi 2016

    View Slide

  70. Custom Tabs Support Library
    • Support for managing CustomTabs.
    • With resouces
    dependencies {
    compile 'com.android.support:customtabs:23.1.1'
    }
    70 DroidKaigi 2016

    View Slide

  71. Custom Tabs?5
    Chrome Custom Tabs allow an app to customize how
    Chrome looks and feels.
    — from official site
    5 CustomTabs's oficial site: Chrome Custom Tabs
    71 DroidKaigi 2016

    View Slide

  72. Demo: Custom Tabs
    • UI customization
    • Performance optimization
    • Need pre-fetch
    72 DroidKaigi 2016

    View Slide

  73. 73 DroidKaigi 2016

    View Slide

  74. How to open the page with CustomTabs
    CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
    // customize toolbar's color
    .setToolbarColor(getResources().getColor(R.color.colorPrimary))
    .build();
    // use shared project
    String packageName = CustomTabsHelper.getPackageNameToUse(MainActivity.this);
    customTabsIntent.intent.setPackage(packageName);
    customTabsIntent.launchUrl(MainActivity.this, Uri.parse("http://zaim.net/"));
    74 DroidKaigi 2016

    View Slide

  75. Percent Support Library
    • Support for managing percentage based
    dimmentions.
    • With resouces
    dependencies {
    compile 'com.android.support:percent:23.1.1'
    }
    75 DroidKaigi 2016

    View Slide

  76. PercentLayout
    • FrameLayout -> PercentFrameLayout
    • RelativeLayout -> PercentRelativeLayout
    76 DroidKaigi 2016

    View Slide

  77. Sample layout file
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    app:layout_widthPercent="50%"
    app:layout_heightPercent="50%"
    app:layout_marginTopPercent="25%"
    app:layout_marginLeftPercent="25%"/>

    77 DroidKaigi 2016

    View Slide

  78. Recommendation Support
    Library for TV
    • Content catalog in the TV home screen.
    • With resouces
    dependencies {
    compile 'com.android.support:recommendation:23.1.1'
    }
    78 DroidKaigi 2016

    View Slide

  79. e.g. from Android Developers
    79 DroidKaigi 2016

    View Slide

  80. Build recommendations6
    public class RecommendationBuilder {
    ...
    public RecommendationBuilder setTitle(String title) {
    mTitle = title;
    return this;
    }
    public RecommendationBuilder setDescription(String description) {
    mDescription = description;
    return this;
    }
    public RecommendationBuilder setImage(String uri) {
    mImageUri = uri;
    return this;
    }
    public RecommendationBuilder setBackground(String uri) {
    mBackgroundUri = uri;
    return this;
    }
    ...
    6 Reference: Android Developers - Recommending TV Content.
    80 DroidKaigi 2016

    View Slide

  81. Summary
    - First, let's use v4-support and v7-appcompat
    library.
    - The speciality series will use if neccessary.
    81 DroidKaigi 2016

    View Slide

  82. Thank you
    for listening
    82 DroidKaigi 2016

    View Slide