Material Design, a new architecture emphasis that gives architecture guidelines for Android apps and apps on added platforms, was alien with the absolution of Android 5.0 Lollipop.
With it came new UI apparatus such as the ‘Floating Action Button’. Implementing these new apparatus while ensuring astern affinity was about a annoying process. Third affair libraries would usually be bare to accumulate the process.
At this year’s Google IO conference, Google alien the Android Architecture Abutment Library which brings a cardinal of important actual architecture apparatus to developers. The apparatus are backwards compatible, with abutment aback to Android 2.1 and implementing them is easier than previously. The library includes a aeronautics drawer view, amphibian labels for alteration text, a amphibian action button, snackbar, tabs and a motion and annal framework to tie them together. In this tutorial we’ll actualize an app that showcases these components.
You can acquisition the final action cipher on gitHub.
Before accepting started on the components, we’ll set up the action and set some styles. Application Android Studio, actualize a new Android project. Name it ‘Design Demo’ and leave the added settings as default, ensure the Minimum SDK adaptation is at API akin 15.
Add the afterward dependencies to the build.gradle (Module:app) file.
The aboriginal account adds the architecture abutment library and the added the CardView library, which we’ll use later. Accompany the action with the adapted Gradle files. This may absorb downloading some of the abutment libraries.
Create a adeptness book in the res/values binder alleged colors.xml. Adapt it as below:
Modify res/values/strings.xml as below. These are all the Strings we’ll charge for the project:
Modify res/values/styles.xml as below:
In the cipher above, we adapt the app’s Blush Palette by ambience the primary, primary-dark and emphasis colors as defined in the actual architecture guide.
Other affair settings that can be customized are apparent in the angel below.
Notice we do not accommodate the android: prefix afore the account name (e.g. android:colorPrimaryDark). This is for astern compatibility. The android: comment requires the minimum API akin set to 21. We use a affair with NoActionBar because we’ll be application a Toolbar as our Action Bar (or AppBar as it is now called).
To actualize the Toolbar, adapt res/layout/activity_main:
Above, we abolish the absence added on the RelativeLayout and add a Toolbar from the abutment library. If you are acknowledging accessories from API akin 21 and above, you would alone charge to use the absence Toolbar basic and not one from the abutment library.
In MainActivity.java adapt the onCreate(Bundle) method:
Download the action assets from gitHub which contains drawable folders that you can adhesive into the res agenda so that the aloft cipher doesn’t aftereffect in errors.
When prompted by Android Studio, ensure you acceptation the Toolbar from the abutment library.
In the aloft code, we get a advertence to the Toolbar and set it as the Action Bar. We afresh get a advertence of the Action Bar and set its Home figure to the abominable hamburger card icon.
Run the app and you should see the Toolbar set as the app’s AppBar.
The Aeronautics Drawer is a accepted basic in Android apps. It is one of the means to body a aeronautics bureaucracy on Android, the others actuality Tabs and Spinners. Implementing one was never a quick process, but now with the Architecture Abutment Library, its accomplishing is abundant simpler.
To get started, adapt activity_main.xml as shown.
In the aloft code, we add a NavigationView to the layout. A NavigationView is placed aural a DrawerLayout. Two important attributes to agenda are app:headerLayout which controls the (optional) blueprint acclimated for the attack and app:menu which is the card adeptness aggrandized for the aeronautics items (this can additionally be adapted at runtime).
Create a book labelled drawer_header.xml in the res/layout binder and adapt it as shown.
This creates a attack for the drawer that will accept a accomplishments blush of the app’s set primary blush (in our case blue), with a acme of 150dp and some text.
Next actualize a book alleged drawer.xml in the res/menu binder and adapt it as shown.
In the above, we actualize the drawer’s card items. The aboriginal allotment shows a accumulating of checkable card items. The arrested account will arise accent in the aeronautics drawer, ensuring the user knows which aeronautics account is currently selected. In the added part, we use a subheader to abstracted the added accumulation of items from the first.
In MainActivity.java add the afterward affiliate variable.
Then add the afterward to the basal of onCreate(Bundle).
Modify onOptionsItemSelected(MenuItem) as shown:
This will accompany the drawer assimilate the awning aback the Home button (the one with the hamburger card icon) is tapped. Run the app and you should see the following.
To abduction bang contest on the card items we charge to set an OnNavigationItemSelectedListener on the NavigationView. Abode the afterward at the basal of onCreate(Bundle).
The aloft cipher sets a adviser on the aeronautics appearance so that aback a drawer card account is selected, the card account is set to arrested (this will alone affect the card items apparent as checkable), The drawer is bankrupt and a Toast is apparent announcement the appellation of the alleged card item.
In a absolute app you would do article like cross to accession Fragment and not aloof appearance a Toast. If you accessible the drawer again, you can affirm that the alleged card account is now arrested by its highlight (only for the items apparent checkable).
A amphibian action button is a annular button cogent a primary action on your interface. The Architecture library’s FloatingActionButton gives a distinct constant implementation, by absence black application the colorAccent from your theme.
In accession to the accustomed admeasurement (56dp) amphibian action button, it supports the mini admeasurement (40dp) aback beheld chain with added elements is critical.
To add a FAB to the layout, adapt res/layout/activity_main.xml as shown.
In the above, we add the FloatingActionButton at the basal adapted bend of the layout. android:src sets the figure apparent aural the button. In code, you can set this with setImageDrawable().
Run the app, and you should see the FAB.
We’ll set an onClickListener on the FAB in the abutting section.
Traditionally, if you capital to present quick abrupt acknowledgment to the user, you would use a Toast. Now there is accession advantage – the Snackbar.
Snackbars are apparent at the basal of the awning and accommodate argument with an alternative distinct action. They automatically time out afterwards the accustomed time by activation off the screen. Users can additionally bash them abroad afore the timeout.
By including the adeptness to collaborate with the Snackbar through accomplishments and annexation to dismiss, they are appreciably added able than toasts, and the API is familiar.
In MainActivity.java add the afterward to the basal of onCreate(Bundle):
In the above, we set an onClickListener on the FAB so that aback it is tapped, a Snackbar will be shown. We actualize an Action on the Snackbar and set an onClickListener that displays a Toast aback tapped.
Note the use of a Appearance as the aboriginal constant to make(). Snackbar will attack to acquisition an adapted ancestor of the Snackbar’s appearance to ensure that it is anchored to the bottom. Run the app to analysis this.
Notice that the Snackbar overlaps the FAB. We’ll fix this in the breadth on CoordinatorLayout.
Using Tabs to about-face amid altered angle is not a new abstraction on Android. The Architecture library’s TabLayout simplifies the action of abacus tabs to your app. It accouterments both anchored tabs, breadth the view’s amplitude is disconnected appropriately amid all of the tabs, as able-bodied as scrollable tabs, breadth the tabs are not a accordant admeasurement and can annal horizontally.
To add a TabLayout to the app, adapt res/layout/activity_main.xml as shown.
In the above, we add a TabLayout as able-bodied as a ViewPager. The ViewPager will be acclimated to accredit accumbent paging amid tabs.
In MainActivity.java add the afterward subclasses.
Then at the basal of onCreate(Bundle) add the following.
In the above, we actualize a Fragment chic that creates a simple fragment with a distinct TextView as its content. We afresh actualize a FragmentStatePagerAdapter that will be acclimated as the ViewPager’s adapter. The getCount() action of the DesignDemoPagerAdapter chic allotment the cardinal of pages/tabs. getItem(int) allotment the Fragment that will be placed on the appearance folio and getPageTitle(int) allotment the appellation that will arise on a accurate tab. For this to work, we use setupWithViewPager() on the TabLayout. This ensures that tab alternative contest amend the ViewPager and folio changes amend the alleged tab.
Run the app and you should see the tabs. You can bash to change tabs or aloof tap on a tab to about-face to it.
The Architecture library introduces the CoordinatorLayout, a blueprint which provides an added akin of ascendancy over blow contest amid adolescent views, article which abounding of the apparatus in the Architecture library booty advantage of.
An archetype of this is aback you add a FloatingActionButton as a adolescent of your CoordinatorLayout and afresh canyon that CoordinatorLayout to your Snackbar.make() call.
Instead of the snackbar announcement over the amphibian action button, as apparent previously, the FloatingActionButton takes advantage of added callbacks provided by CoordinatorLayout to automatically move advancement as the snackbar animates in and allotment to its position aback the snackbar animates out. This is accurate on Android 3.0 and college devices.
To use the CoordinatorLayout in our layout, adapt res/layout/activity_main.xml as shown:
As apparent above, the FAB needs to be a adolescent of the CoordinatorLayout, so we alter the antecedent RelativeLayout with the CoordinatorLayout. Additionally agenda that we change the FAB’s position ambience to android:layout_gravity=”bottom|right”.
Next in MainActivity.java aback amalgam the Snackbar, canyon the CoordinatorLayout as the appearance parameter.
Run the app and now aback you tap on the FAB, the Snackbar will accelerate into view, but this time it won’t overlap the FAB, the FAB will accelerate up with it and aback the Snackbar is absolved either by timing out or with a swipe, the FAB will abatement aback into place.
The added capital use case for the CoordinatorLayout apropos the app bar and scrolling techniques. The architecture library provides the AppBarLayout which allows the Toolbar and added angle (such as tabs provided by TabLayout) to acknowledge to annal contest in a affinity appearance apparent with a ScrollingViewBehavior.
Before attractive at this, let’s aboriginal actualize article we can annal through. We’ll use a RecyclerView to actualize a account of items we can annal through.
Note: I spent about an hour aggravating to accomplish the CoordinatorLayout assignment with a ListView, but the scrolling behaviour was aloof not working. A simple chase led me to this post, this column and this post. If you analysis those links, it seems that the CoordinatorLayout doesn’t assignment able-bodied out-of-the-box with ListViews (and allegedly GridViews and ScrollViews). The aftermost two links appearance a workaround to authoritative it assignment with a ListView, but it doesn’t assume to be backwards compatible. I capital to let you apperceive incase you get ashore on the aforementioned issue. Abutment for ListViews will apparently be added in a approaching update.
I’ll go through the conception of the RecyclerView afterwards abundant account as this is out of the ambit of this tutorial and you can calmly acquisition advice online apropos RecyclerViews.
Place the afterward in your build.gradle (Module: app) book and accompany the gradle files..
Create a book in res/layout alleged fragment_list_view.xml. Adapt it as shown.
Create accession blueprint book in res/layout and name it list_row.xml. Alter its capacity with the agreeable below.
Create a java book alleged DesignDemoRecyclerAdapter.java and adhesive the following.
Modify DesignDemoFragment.onCreateView() in MainActivity.java as shown:
This inflates the ahead created blueprint file. Now the fragment’s appearance will accommodate a account of items. Run the app to affirm this.
With that completed, we can attending at the scrolling behaviour provided by the CoordinatorLayout.
Modify activity_main.xml as shown.
In the above, we backup the Toolbar and TabLayout in an AppBarLayout. The AppBarLayout allows the Toolbar and added angle (such as tabs provided by TabLayout) to acknowledge to annal contest in a affinity appearance apparent with a ScrollingViewBehavior. Aback the user scrolls through the RecyclerView, the AppBarLayout responds by application its children’s annal flags to ascendancy how they access (scroll on screen) and avenue (scroll off screen).
Note that all angle application the annal banderole charge be declared afore angle that do not use the flag. This ensures that all angle avenue from the top, abrogation the anchored elements behind.
In our app we use the annal and enterAlways flags on the Toolbar which will aftereffect in the Toolbar scrolling off awning aback scrolling upwards and arise aback on awning on the bottomward scroll. Notice the use of app:layout_behavior=”@string/appbar_scrolling_view_behavior” on the ViewPager. The abutment library contains a appropriate cord adeptness @string/appbar_scrolling_view_behavior that maps to AppBarLayout.ScrollingViewBehavior, which is acclimated to acquaint the AppBarLayout aback annal contest action on a accurate view. The behavior charge be accustomed on the appearance that triggers the event.
Run the app to see the aftereffect of scrolling on the AppBar.
The CollapsingToolbarLayour provides accession blazon of scrolling behaviour to the AppBar. To see it in action, we’ll aboriginal actualize accession action that has a Toolbar, but no TabLayout.
Create a Blank Action (File -> New -> Action -> Blank Activity) and name it SecondActivity.
Modify res/layout/activity_second.xml as shown.
In the above, we use the CollapsingToolbarLayout with the annal and exitUntilCollapsed flags which will accomplish its subviews annal off screen. For the Toolbar however, we set app:layout_collapseMode=”pin” which will ensure that the Toolbar itself charcoal affianced to the top of the awning while the appearance collapses. Accession aftereffect we’ll get is that the appellation in the CollapsingToolbarLayout will arise beyond aback the blueprint is absolutely visible, afresh alteration to its absence admeasurement as it is collapsed. We’ll set this appellation in code. The blow of the blueprint contains a NestedScrollView with several Cards as its children.
In SecondActivity.java adapt onCreate(Bundle) as shown.
Here we set up the Up caret on the Toolbar and afresh set a appellation for the CollapsingToolbarLayout.
For the Up caret to work, abode the afterward in the apparent book as a adolescent of the SecondActivity’s action tag.
In DesignDemoRecyclerAdapter.java adapt `onBindViewHolder() as shown.
In the above, we set an onClick Adviser to the TextView on anniversary row of the RecyclerView. This is not the best way of ambience a adviser on the RecyclerView items, because if you run the app, the listener’s blow ambition will alone awning the breadth with the row’s argument and not the accomplished row. I do this actuality because all I appetite is a way to alpha the new action and so I autonomous on a way to address the atomic bulk of code.
Run the app and you will see the afterward scrolling behaviour.
The aftermost basic of the Architecture Abutment Library that we’ll attending at is the bigger EditText. Normally aback accounting is started on an EditText, the placeholder adumbration that was on the acreage is hidden. Now you can blanket an EditText in a TextInputLayout causing the adumbration argument to become a amphibian characterization aloft the EditText, ensuring that users never lose ambience in what they are entering.
To see this, abode the afterward central one of the CardViews in activity_second.xml. You can abode it afterwards the added TextView of the Card.
Run the app and aback you alpha to blazon into the EditText, the adumbration argument will float aloft the field.
In accession to assuming hints, you can affectation an absurdity bulletin beneath the EditText by calling setError().
That brings us to the end of this tutorial. The completed action for the tutorial can be downloaded here. For added on the Android Architecture Abutment Library, apprehend the Android developers blog.
10 Design Android Cardview Background Color – android cardview background color
| Delightful to our website, on this moment I’m going to teach you concerning keyword. And after this, here is the initial graphic:
How about graphic preceding? will be of which wonderful???. if you feel consequently, I’l l teach you many image once more down below:
So, if you desire to acquire the great shots about (10 Design Android Cardview Background Color), click on save button to save the shots for your personal computer. These are ready for down load, if you love and want to own it, click save logo in the page, and it’ll be directly down loaded to your pc.} Finally in order to have unique and recent graphic related with (10 Design Android Cardview Background Color), please follow us on google plus or save the site, we try our best to offer you daily up grade with all new and fresh photos. We do hope you enjoy keeping here. For some up-dates and recent information about (10 Design Android Cardview Background Color) graphics, please kindly follow us on twitter, path, Instagram and google plus, or you mark this page on book mark area, We attempt to give you up-date regularly with all new and fresh shots, like your surfing, and find the best for you.
Here you are at our website, articleabove (10 Design Android Cardview Background Color) published . Today we are delighted to declare that we have found an incrediblyinteresting contentto be discussed, that is (10 Design Android Cardview Background Color) Lots of people looking for info about(10 Design Android Cardview Background Color) and of course one of them is you, is not it?