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. I am ... • engineer at Zaim Inc. • DroidKaigi

    2015 & 2016 staff • DroidKaigi 2015 & 2016 speaker 2 DroidKaigi 2016
  2. 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
  3. 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
  4. Why does it need? In order to implement the function

    across different API level. 10 DroidKaigi 2016
  5. 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
  6. 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
  7. 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
  8. Frequently used class • Fragment • ViewPager • NotificationCompat •

    DrawerLayout • SwipeRefreshLayout • Loader (AsyncTaskLoader / CursorLoader) 16 DroidKaigi 2016
  9. Demo: DrawerLayout • Including v7-appcompat and design support library •

    ActionBarDrawerToggle • NavigationView 17 DroidKaigi 2016
  10. 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
  11. The Class that I want you to remember NestedScrollView >>

    There is a case to be required in Design Support Library. 20 DroidKaigi 2016
  12. 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
  13. Example: NotificationCompat Helper for accessing features in Notification introduced after

    API level 4 in a backwards compatible fashion. — NotificationCompat - Class overview 22 DroidKaigi 2016
  14. // 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
  15. v7 Support Library • Android 2.1 (API level 7) and

    higher • You can be included independently. • With resouces 24 DroidKaigi 2016
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. Other v7: cardview • Support for the CardView widget. dependencies

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

    { compile 'com.android.support:gridlayout-v7:23.1.1' } 35 DroidKaigi 2016
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. v14 Preference Support Library • Support for PreferenceFragment using "android.app.Fragment".

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

    devices dependencies { compile 'com.android.support:preference-leanback-v17:23.1.1' } 43 DroidKaigi 2016
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. Annotations Support Library • Support for annotation metadata. • With

    resouces dependencies { compile 'com.android.support:support-annotations:23.1.1' } 58 DroidKaigi 2016
  42. e.g. @NotNull private void something() { setMessage(null); // Out warning

    setMessage("message"); // Not out warning } private void setMessage(@NotNull String message) { ... } 59 DroidKaigi 2016
  43. 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
  44. Design Support Library • Support for material design components and

    patterns. • With resouces dependencies { compile 'com.android.support:design:23.1.1' } 61 DroidKaigi 2016
  45. Structure of the layout file <android.support.design.widget.CoordinatorLayout ...> <!-- ActionBar -->

    <android.support.design.widget.AppBarLayout ...> <android.support.v7.widget.Toolbar ... app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 66 DroidKaigi 2016
  46. Structure of the layout file <android.support.design.widget.CoordinatorLayout ...> <!-- ActionBar -->

    <android.support.design.widget.AppBarLayout ...> <android.support.v7.widget.Toolbar ... app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout> <!-- FAB --> <android.support.design.widget.FloatingActionButton ... android:layout_gravity="bottom|end"/> </android.support.design.widget.CoordinatorLayout> 67 DroidKaigi 2016
  47. Structure of the layout file <android.support.design.widget.CoordinatorLayout ...> <!-- ActionBar -->

    <android.support.design.widget.AppBarLayout ...> <android.support.v7.widget.Toolbar ... app:layout_scrollFlags="scroll|enterAlways"/> </android.support.design.widget.AppBarLayout> <!-- Body --> <android.support.v4.widget.NestedScrollView .../> <!-- FAB --> <android.support.design.widget.FloatingActionButton ... android:layout_gravity="bottom|end"/> </android.support.design.widget.CoordinatorLayout> 68 DroidKaigi 2016
  48. 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
  49. Custom Tabs Support Library • Support for managing CustomTabs. •

    With resouces dependencies { compile 'com.android.support:customtabs:23.1.1' } 70 DroidKaigi 2016
  50. 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
  51. 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
  52. Percent Support Library • Support for managing percentage based dimmentions.

    • With resouces dependencies { compile 'com.android.support:percent:23.1.1' } 75 DroidKaigi 2016
  53. 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
  54. 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
  55. Summary - First, let's use v4-support and v7-appcompat library. -

    The speciality series will use if neccessary. 81 DroidKaigi 2016