Android

Android TutorialAdding a FuseView to an Android ProjectAndroid 9-Patch ImagesAndroid Accessing SQLite databases using the ContentValues classAndroid Accounts and AccountManagerAndroid ACRAAndroid ActivityAndroid Activity RecognitionAndroid adb (Android Debug Bridge)Android adb shellAndroid AdMobAndroid AIDLAndroid AlarmManagerAndroid Android-x86 in VirtualBoxAndroid Animated AlertDialog BoxAndroid AnimatorsAndroid Annotation ProcessorAndroid Architecture ComponentsAndroid AsyncTaskAndroid AudioManagerAndroid AudioTrackAndroid AuthenticatorAndroid AutoCompleteTextViewAndroid Autosizing TextViewsAndroid Barcode and QR code readingAndroid Bitmap CacheAndroid Bluetooth and Bluetooth LE APIAndroid Bluetooth Low EnergyAndroid Bottom SheetsAndroid BottomNavigationViewAndroid BroadcastReceiverAndroid Building Backwards Compatible AppsAndroid ButterKnifeAndroid ButtonAndroid Callback URLAndroid Camera 2 APIAndroid Camera and GalleryAndroid Canvas drawing using SurfaceViewAndroid Capturing ScreenshotsAndroid CardViewAndroid Check Data ConnectionAndroid Check Internet ConnectivityAndroid CleverTapAndroid ColorsAndroid ConstraintLayoutAndroid ConstraintSetAndroid ContentProviderAndroid ContextAndroid CoordinatorLayout and BehaviorsAndroid Count Down TimerAndroid Crash Reporting ToolsAndroid Create Singleton Class for Toast MessageAndroid Creating Custom ViewsAndroid Creating Overlay (always-on-top) WindowsAndroid Creating Splash screenAndroid Custom FontsAndroid Dagger 2Android Data Binding LibraryAndroid Data Encryption/DecryptionAndroid Data Synchronization with Sync AdapterAndroid Date and Time PickersAndroid DayNight Theme (AppCompat v23.2 / API 14+)Android Define step value (increment) for custom RangeSeekBarAndroid Design PatternsAndroid Device Display MetricsAndroid DialogAndroid Displaying Google AdsAndroid Doze ModeAndroid DrawablesAndroid EditTextAndroid Email ValidationAndroid EmulatorAndroid Enhancing Alert DialogsAndroid ExceptionsAndroid ExoPlayerAndroid FastjsonAndroid fastlaneAndroid FileProviderAndroid FirebaseAndroid Firebase App IndexingAndroid Firebase Cloud MessagingAndroid Firebase Crash ReportingAndroid Firebase Realtime DataBaseAndroid FloatingActionButtonAndroid Formatting phone numbers with pattern.Android Formatting StringsAndroid FragmentsAndroid FrescoAndroid game developmentAndroid Gesture Detection



Android CoordinatorLayout and Behaviors

From WikiOD

The CoordinatorLayout is a super-powered FrameLayout and goal of this ViewGroup is to coordinate the views that are inside it.

The main appeal of the CoordinatorLayout is its ability to coordinate the animations and transitions of the views within the XML file itself.

CoordinatorLayout is intended for two primary use cases:

As a top-level application decor or chrome layout
As a container for a specific interaction with one or more child views

Remarks[edit | edit source]

The CoordinatorLayout is a container that extends the FrameLayout.

By attaching a CoordinatorLayout.Behavior to a direct child of CoordinatorLayout, you’ll be able to intercept touch events, window insets, measurement, layout, and nested scrolling.

By specifying Behaviors for child views of a CoordinatorLayout you can provide many different interactions within a single parent and those views can also interact with one another. View classes can specify a default behavior when used as a child of a CoordinatorLayout using the DefaultBehavior annotation.

Creating a simple Behavior[edit | edit source]

To create a Behavior just extend the CoordinatorLayout.Behavior class.

Extend the CoordinatorLayout.Behavior[edit | edit source]

Example:

public class MyBehavior<V extends View> extends CoordinatorLayout.Behavior<V> {

      /**
       * Default constructor.
       */
      public MyBehavior() {
      }

      /**
       * Default constructor for inflating a MyBehavior from layout.
       *
       * @param context The {@link Context}.
       * @param attrs The {@link AttributeSet}.
       */
      public MyBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
}

This behavior need to be attached to a child View of a CoordinatorLayout to be called.

Attach a Behavior programmatically[edit | edit source]

MyBehavior myBehavior = new MyBehavior();
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) view.getLayoutParams();
params.setBehavior(myBehavior);

Attach a Behavior in XML[edit | edit source]

You can use the layout_behavior attribute to attach the behavior in XML:

<View
  android:layout_height="...."
  android:layout_width="...."
  app:layout_behavior=".MyBehavior" />

Attach a Behavior automatically[edit | edit source]

If you are working with a custom view you can attach the behavior using the @CoordinatorLayout.DefaultBehavior annotation:

@CoordinatorLayout.DefaultBehavior(MyBehavior.class)
public class MyView extends ..... {

}

Using the SwipeDismissBehavior[edit | edit source]

The SwipeDismissBehavior works on any View and implements the functionality of swipe to dismiss in our layouts with a CoordinatorLayout.

Just use:

        final SwipeDismissBehavior<MyView> swipe = new SwipeDismissBehavior();

        //Sets the swipe direction for this behavior.
        swipe.setSwipeDirection(
            SwipeDismissBehavior.SWIPE_DIRECTION_ANY);

        //Set the listener to be used when a dismiss event occurs
        swipe.setListener(
            new SwipeDismissBehavior.OnDismissListener() {
            @Override public void onDismiss(View view) {
                //......
            }

            @Override 
            public void onDragStateChanged(int state) {
                //......
            }
        });

        //Attach the SwipeDismissBehavior to a view
        LayoutParams coordinatorParams = 
            (LayoutParams) mView.getLayoutParams();    
        coordinatorParams.setBehavior(swipe);

Create dependencies between Views[edit | edit source]

You can use the CoordinatorLayout.Behavior to create dependencies between views. You can anchor a View to another View by:

For example, in order to create a Behavior for moving an ImageView when another one is moved (example Toolbar), perform the following steps:

Create the custom Behavior:

public class MyBehavior extends CoordinatorLayout.Behavior<ImageView> {...}

Override the layoutDependsOn method returning true. This method is called every time a change occurs to the layout:

@Override
public boolean layoutDependsOn(CoordinatorLayout parent, 
        ImageView child, View dependency) {
    // Returns true to add a dependency.
    return dependency instanceof Toolbar;
}

Whenever the method layoutDependsOn returns true the method onDependentViewChanged is called:

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, ImageView child, View dependency) {
    // Implement here animations, translations, or movements; always related to the provided dependency.
    float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight()); 
    child.setTranslationY(translationY);
}

Credit:Stack_Overflow_Documentation