[JAVA] Android ripple effect (Custom View)


Purpose
Make it clear where you are tapping when you tap a button or something.


Method
To be honest, there are various things. Proposal 1. Define the behavior when tapped in XML.    https://qiita.com/ujikawa1026/items/a79d418b70d88d1d69ba Proposal 2. Use an API called "material ripple". Another site: https://github.com/balysv/material-ripple


Premise
I'm designing colors, fonts and sizes with logic. XML (plan 1) is not fixed, but I want to set it freely (plan 2). Rather, it is better than XML because it has its own settings such as background color, color, font, and size.


Difficulties

    1. If the target view is Invisibility or Gone, The ripple effect (view) remains.
  1. I want to easily define it in a method ... → Call the View searched by findViewByID every time ...! ?? (Troublesome)
    Solution
    ・ Custom view ...!

yourCustomView.java


    //Difficulties 1. Solution
    @Override
    public void setVisibility(int visibility){
        //This view is disabled when tapped ([invisibility]or[GONE])
        //Nevertheless, it prevents it from being set.
        super.setVisibility(visibility);
        if(getParent() instanceof MaterialRippleLayout){
            ((MaterialRippleLayout) getParent()).setVisibility(visibility);
        }
    }

    //Difficulty 2. Solution
    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);
        switch (visibility){
            //Dynamically change when the view is visible
            case VISIBLE:MaterialRippleLayout.on(changedView)
                            .rippleColor(Color.GREEN)
                            .rippleAlpha(0.5f)
                            .rippleOverlay(true)
                            .rippleHover(true)
                            .rippleDuration(100)
                            .create();
                break;
            //do nothing.
            case INVISIBLE:
                break;
        }
    }

Thank you for your cooperation.

Recommended Posts

Android ripple effect (Custom View)
Android view
[Kotlin / Android] Create a custom view
Customize list view on Android
Implement ripple representation on images on Android