Skip to content

Instantly share code, notes, and snippets.

@pcevikogullari
Created March 9, 2017 14:48
Show Gist options
  • Save pcevikogullari/e6c1a9767b68f9ce8268356e411c5000 to your computer and use it in GitHub Desktop.
Save pcevikogullari/e6c1a9767b68f9ce8268356e411c5000 to your computer and use it in GitHub Desktop.
/**
* Created by Pamir on 09/03/2017.
*/
public class MyLinearLayout extends LinearLayout {
private final float SCALE_PRESSED = 0.9f;
private final float SCALE_NORMAL = 1.0f;
private OnMyClickListener myOnClickListener;
public MyLinearLayout(Context context) {
super(context);
init();
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
setFocusable(true);
setFocusableInTouchMode(true); // Needed to call onFocusChanged()
setBackgroundColor(Color.BLACK);
setClickable(true);
}
public void setOnMyClickListener(OnMyClickListener myOnClickListener) {
this.myOnClickListener = myOnClickListener;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
setScale(SCALE_PRESSED);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
setScale(SCALE_NORMAL);
}
gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
public void setScale(float scale) {
setScaleX(scale);
setScaleY(scale);
}
private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (myOnClickListener != null) {
myOnClickListener.onClick();
}
Log.v("MyButton", "TapConfirmed");
return super.onSingleTapConfirmed(e);
}
};
private GestureDetector gestureDetector = new GestureDetector(getContext(), gestureListener);
public interface OnMyClickListener {
void onClick();
}
}
@pcevikogullari
Copy link
Author

Scales down when takes focus, turn back to normal state when released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment