Add selection support to RecyclerView:recyclerview-selection:28.0.0

recyclerview-selection provides item selection support for RecyclerView.It support for creating, modifying, inspecting, and monitoring changes to items in a RecyclerView list.It enables users to select items in RecyclerView list using touch or mouse input.

To add selection support to a RecyclerView instance, follow these steps:

1.Add Dependency


Add following depedency to your app’s build.gradle file.28.0.0-alpha1 is a pre-release version to support the Android P developer preview.

2.Implement ItemKeyProvider


Developers must decide on the key type used to identify selected items. There are three key types: Parcelable,String, and Long.

3.Implement ItemDetails


ItemDetails implementation provides the selection library with access to information about a specific RecyclerView item. This class is a key component in controling the behaviors of the selection library in the context of a specific activity.

4.Implement ItemDetailsLookup


ItemDetailsLookup enables the selection library to access information about RecyclerView items given a MotionEvent. It is effectively a factory for ItemDetails instances that are backed up by aRecyclerView.ViewHolder instance.

The Selection library calls getItemDetails() when it needs access to information about the area. Your implementation must negotiate ViewHolder lookup with the corresponding RecyclerView instance and the subsequent conversion of the ViewHolder instance to an ItemDetailsLookup.ItemDetails instance.

5.Reflect Selected State


When the user selects an item the library will record that in SelectionTracker then notify RecyclerView that the state of the item has changed.The item must then be updated to reflect the new selection status. Without this, the user will not see that the item has been selected.

Update the styling of the view to represent the activated status with a color state list.

item_list.xml

The selection library does not provide a default visual decoration for the selected items. You must provide this when you implement onBindViewHolder().

6.Add Contextual Actions


ActionModes used to provide alternative interaction modes and replace parts of the normal UI until finished. Action modes include text selection and contextual actions.

7.Build SelectionTracker


In order to build a SelectionTracker instance, your app must supply the same Adapter that you used to initialize RecyclerView to SelectionTracker.Builder.

Register a SelectionTracker.SelectionObserver to be notified when the selection changes. When a selection is first created, start ActionMode to represent this to the user, and provide selection specific actions.

8.Activity lifecycle events


In order to preserve state you must handling of Activity lifecycle events. your app must call the selection tracker’sonSaveInstanceState() and onRestoreInstanceState() methods from the activity’s onSaveInstanceState()and onRestoreInstanceState() methods respectively.

 

Download this Project from GitHub

 

 

Detect user’s activity using Activity Recognition Transition API

Transition API can tell if your users are walking, running, cycling, or in a vehicle and you can use it to improve your app experience.

Transition API combine various signals like location and sensor data to determine when the user has started or ended an activity like walking or driving.

1.Add Dependencies


Declare a dependency to the API, add a reference to the Google maven repository and add an implementation entry to com.google.android.gms:play-services-location:12.0.0 to the dependencies section of your app build.gradle file.

2.Add Permission


Specify the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission in the app manifest.

3.Register for activity transition updates


To start receiving notifications about activity transitions, you must implement, An ActivityTransitionRequest object that specifies the type of activity and transition.PendingIntent callback where your app receives notifications.

ActivityTransitionRequest

You must create a list of ActivityTransition objects, which represent the transition that you want to receive notifications about. An ActivityTransition object includes the 1.An activity type  IN_VEHICLE,ON_BICYCLE,RUNNING,STILL,WALKING

2.A transition type of ACTIVITY_TRANSITION_ENTER or ACTIVITY_TRANSITION_EXIT. For more information, refer to theActivityTransition class.

The following code shows how to create a list of ActivityTransition objects:

PendingIntent

When using the API, a common use case is that an application wants to monitor activity in the background and perform an action when a specific activity is detected.The application specifies a pending intent callback typically an intent service, which will be called with an intent when activities are detected.So there’s no need to keep the service always running in the background.

After successfully registering for activity transition updates, your app receives notifications in the registered PendingIntent.

4.Receive activity transition events


When the requested activity transition occurs, you app receives an Intent callback.The events are ordered in chronological order, for example, if an app requests for the IN_VEHICLE activity type on the ACTIVITY_TRANSITION_ENTER and ACTIVITY_TRANSITION_EXIT transitions, then it receives an ActivityTransitionEvent object when the user starts driving, and another one when the user transitions to any other activity.

Your application will receive callbacks with extras containing an activity recognition result.This contains a list of activities that the user may be doing at the particular time.

5.Deregister for activity transition updates


Deregister for activity transition updates by calling the removeActivityTransitionUpdates() method of the ActivityRecognitionClient and passing your PendingIntent object as a parameter.

 

Download this project from GitHub

 

 

Calling REST API from a Flutter App

Building Flutter applications whose content is static can be a bad idea. Instead, you should consider building applications that can fetch content from the Web server.

In this tutorial, I’m going to show you how to use the classes and methods available in the Flutter SDK to connect to remote web servers and interact with them using their REST APIs.

Calling REST API from a Flutter App

Creating an HTTP Connection


To create an HTTP Client we need to add an import.

The following code snippet shows you how to setup a connection with the GitHub API endpoint:

Note that the HTTP APIs use Dart Futures in the return values.Flutter recommend using the API calls with the async/await syntax.

 

Responsive UI


Network calls are slow. It doesn’t matter where you are on your phone. Sometimes, your server might be slow and you just don’t want to show a white page. So you want to show a progress bar. There is a great library called async_loader. Following code snippet show how to use it.

Usage

To use this plugin, add async_loader as a dependency in your pubspec.yaml file.

Create instance

You need to create an async loader in your build function.It has a few parameters.It has initState,renderLoad,renderError,and renderSuccess.

initState is basically as the widget is floating, what do you want to load?what data do you want to load?

renderLoad is as it’s being loaded, what do you want to show? So in renderLoad, I show a progress bar.

renderError is if something went crazy wrong, what do you want to do? So here for the sake of the demo, I just have new text error loading conversation.

renderSuccess is called with your data that you return in your initState. And then you can take that data, and then you can actually render your entire UI.

JSON Parsing


In Android, you can use GSON for JSON parsing.There’s nothing like this on Flutter, mainly because Flutter doesn’t have a reflection.There is a great library called json_serializable.

Setting up json_serializable

To include json_serializable, you need one regular and two dev dependencies. dev dependencies are dependencies that are not included in your app source code.

Check the latest versions.

pubspec.yaml

Click “Packages Get” in your editor to make these new dependencies available in your project.

Convert  User class to a json_serializable .

user.dart

When creating json_serializable classes the first time, you will get errors.Because the generated code for the model class does not exist yet. To resolve this, you must run the code generator that generates the serialization boilerplate for us.

By running (in command prompt)flutter packages pub run build_runner build in our project root, you can generate json serialization code for our models whenever needed.

Consuming json_serializable models

To deserialize a JSON string json_serializable way, we do not have actually to make any changes to our previous code.

Same goes for serialization. The calling API is the same as before.

 

Download Project from GitHub