Cosc 5/4730 Support design library
Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces – You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. – A note, as of this writing, v broke somethings in Navigation drawer view which worked in The example code has comments to explain it.
Support Design library Using it – Add to the build.gradle (module file) – In the dependencies compile 'com.android.support:design:23.0.0‘ Sync/compile and now it ready to use in your app.
SnackBar Similar to a toast shown on the bottom of the screen and contain text with an optional single action. – Like a toast, they automatically time out after the given time length by animating off the screen. In addition, users can swipe them away before the timeout.
SnackBar code Simple version Snackbar.make(myView, "Hi there?", Snackbar.LENGTH_LONG) //or SHORT.show(); With the response button Snackbar.make(myView, "Hi there?", Snackbar.LENGTH_LONG) //or SHORT.setAction("Undo?", SBonClickListener) //this line is optional..show(); Where SBonClickListener is a standard OnClicklistener View.OnClickListener SBonClickListener = new View.OnClickListener(){ public void onClick (View v){ Toast.makeText(getActivity(),"You clicked undo", Toast.LENGTH_LONG).show(); } }; myView is first layout for the screen.
floating action button The FAB is a round button, instead of the traditional square button. – It’s normally colored with the accent color (from Material design) and a symbol – It also has an elevation (shadow) as well, so it doesn’t look flat on the app.
floating action button code Xml <android.support.design.widget.FloatingActio nButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:fabSize="mini" /> Note app, needs xmlns:app=“ apk/res-auto” at the top. fabSize="normal" for bigger size. The java code is just like a normal widget: myView.findViewById( R.id.fab1).setOnClickListener (new View.OnClickListener() public void onClick(View v) { … } });
TextInputLayout it will display the hint even after the user starts typing and allows you to set an error message as well. The layout wraps around an editText. – Uses standard listener – In the onTextChanged Use setError(msg) – Display an for error setError(“”) – To clear the error.
TextInputLayout Xml <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="abc" android:singleLine="true" /> Java code mTextInputLayout = (TextInputLayout) findViewById(R.id.textinput02); et2 = (EditText) findViewById(R.id.edittext02); et2.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { if (!s.toString().equals("abc")) { set the error message that will display below the edittext mTextInputLayout.setError("Incorrect input."); } else { clear the error message. mTextInputLayout.setError(""); }
Motion with snackbars. The idea to move things out of the way, while a snackbar is displayed, so that it doesn’t obscure things. An example with the FAB – Which is to be placed at the buttom right. We wrap everything with a CoordinatorLayout
CoordinatorLayout Xml Normal layout stuff, that stays still … rest of the layout code. Stuff to move, in this case the FAB For the snackbar, where it wants the layout, we provide it the coorindatorlayout mCoordinatorLayout = (CoordinatorLayout) myView.findViewById(R.id.coordinatorlayout1); Now the layout uses the coordinatorlayout, so they coordinate the snackbar. Snackbar.make(mCoordinatorLayout, "Did the FAB move?", Snackbar.LENGTH_LONG).setAction("Yes?", SBonClickListener).show();
Support NavigationView For the DrawerLayout, you can use a “menu” to the drawer instead of creating a listview or fragment. <android.support.design.widget.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start"
NavigationView xml Xml <item android:checked="true" <item … Layout for the header is a linearlayout with a textview.
NavigationView Java code mNavigationView = (NavigationView) findViewById(R.id.navview); setup a listener, which acts very similar to how menus are handled. mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() public boolean onNavigationItemSelected(MenuItem menuItem) { we could just as easily call onOptionsItemSelected and how it deal with it. use return onOptionsItemSelected(menuItem); int id = menuItem.getItemId(); if (id == R.id.navigation_item_1) { … return true; //we dealt with it. mDrawerlayout.closeDrawers(); //close the drawer layout } Return false; //we did not deal with it. });
Example code All the examples previous slides are in supportdesignDemo – In the Material design directory of the UI repo.
Support.TabLayout The support TabLayout will give you tabs (see left) – Uses a tab selected listener, you can change the information, fragment, page in a viewpager. can be used in place of a pageTabStrip on viewpager Examples below use viewpager – Left uses a pagetabstrip and right a tablayout.
Collapsing toolbar. Using a listview or Recyclerview – Mostly done with xml with a coordinatorlayout and a AppBarLayout. Plus more. supportDesign3 has the code – And dling-Scrolls-with-CoordinatorLayout maybe helpful as well. dling-Scrolls-with-CoordinatorLayout this will be left for advanced Mobile Apps
References was used to create the animated pictures (gif) developers.blogspot.com/2015/05/android- design-support-library.html developers.blogspot.com/2015/05/android- design-support-library.html
Q A &