[JAVA] Android Easily give a "press" to a button

How to easily give the feeling of pressing an Image button or button on Android

image3.jpg

  1. Create the following two Java classes and add them to your project.

PushButton.java


package *****package name*****;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

/***Effect button to push***/
public class PushButton extends Button {
    public PushButton(Context context) {
        super(context);
    }

    public PushButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setPressed(boolean pressed) {
        if(pressed){
            this.setScaleY(0.92f);
            this.setScaleX(0.96f);
        }else{
            this.setScaleY(1.0f);
            this.setScaleX(1.0f);
        }
        super.setPressed(pressed);
    }

}

AlphaButton.java


package *****package name*****;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

/***Semi-transparent effect button***/
public class AlphaButton extends Button {
    public AlphaButton(Context context) {
        super(context);
    }

    public AlphaButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void setPressed(boolean pressed) {
        if(pressed){
            this.setAlpha(0.75f);
        }else{
            this.setAlpha(1.0f);
        }
        super.setPressed(pressed);
    }

}
  1. After that, place a custom button instead of "Button" in the layout XML ・ Layout example

layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainframe"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">

    <!---Button that shrinks when pressed-->
    <*****package name*****.PushButton
        android:id="@+id/button_sample01"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:background="@drawable/button_sample"
        />

    <!---Semi-transparent button when pressed-->
    <*****package name*****.AlphaButton
        android:id="@+id/button_sample02"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:background="@drawable/button_sample"
        />
</LinearLayout>

that's all

Recommended Posts

Android Easily give a "press" to a button
[Android] How to detect volume change (= volume button press)
[Android / Java] Set up a button to return to Fragment
Android Development-Try to display a dialog-
A note about adding Junit 4 to Android Studio
[Android] Inherit ImageView to create a new class
[Android] Implement a function to display passwords quickly
How to easily create a pull-down in Rails
[Android] Two ways to get a Bluetooth Adapter
[Rails] How to create a Twitter share button
[Introduction to Android application development] Let's make a counter
How to perform a specific process when the back button is pressed in Android Fragment
Introduction to Android Layout
How to take a screenshot with the Android Studio emulator
How to easily implement in-app purchase using itemstore <Implementation: Android>
I tried adding a separator line to TabLayout on Android
I'm glad to have a method to blink characters on Android
I want to give a class name to the select attribute
Add a shadow to the Swift Button (and also the circle)