Monday, February 20, 2017

android - Ripple effect example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f7faf6">
    <com.andexert.library.RippleView
        android:id="@+id/ripple_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        >
        <Button
            android:id="@+id/btn_do"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sample Button"
            android:layout_margin="25dp"
            android:padding="40dp"
            android:background="#5278bf"
            />
    </com.andexert.library.RippleView>
    <com.andexert.library.RippleView
        android:id="@+id/ripple_view_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        >
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sample TextView"
            android:layout_margin="25dp"
            android:padding="40dp"
            android:background="#278025"
            />
    </com.andexert.library.RippleView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


import com.andexert.library.RippleView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private static final String TAG = MainActivity.class.getSimpleName();

    private CoordinatorLayout mCLayout;
    private Button mButtonDo;
    private RippleView mRippleView;
    private RippleView mRippleViewTV;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButtonDo = (Button) findViewById(R.id.btn_do);
        mRippleView = (RippleView) findViewById(R.id.ripple_view);
        mRippleViewTV = (RippleView) findViewById(R.id.ripple_view_tv);

        // Set a ripple effect/animation complete listener
        mRippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView rippleView) {
                // Do something when ripple animation complete
                Snackbar.make(mCLayout,"Done",Snackbar.LENGTH_LONG).show();
            }
        });

        // Set a click listener for second ripple view
        mRippleViewTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do something
                Snackbar.make(mCLayout,"Clicked.",Snackbar.LENGTH_LONG).show();
            }
        });

        // Set a click listener for button widget
        mButtonDo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Do something
            }
        });
    }
}
build.gradle(app) dependencies

compile 'com.github.traex.rippleeffect:library:1.3'