set View Focus Changed - Android User Interface

Android examples for User Interface:Touch Event

Description

set View Focus Changed

Demo Code


import android.graphics.ColorMatrixColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.TouchDelegate;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;

public class Main{
    public final static OnFocusChangeListener buttonOnFocusChangeListener = new OnFocusChangeListener() {

        @Override/*  ww w. j  a v a  2  s .  co  m*/
        public void onFocusChange(View v, boolean hasFocus) {
            if (!v.isEnabled()) {
                return;
            }
            if (hasFocus) {
                if (v.getBackground() != null) {
                    v.getBackground().setColorFilter(
                            new ColorMatrixColorFilter(BT_SELECTED));
                    v.setBackgroundDrawable(v.getBackground());
                } else {
                    setClickStyle(true, v);
                }
            } else {
                if (v.getBackground() != null) {
                    v.getBackground().setColorFilter(
                            new ColorMatrixColorFilter(BT_NOT_SELECTED));
                    v.setBackgroundDrawable(v.getBackground());
                } else {
                    setClickStyle(false, v);
                }

            }
        }
    };
    public final static OnTouchListener buttonOnTouchListener = new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (!v.isEnabled()) {
                return false;
            }
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (v.getBackground() != null) {
                    v.getBackground().setColorFilter(
                            new ColorMatrixColorFilter(BT_SELECTED));
                    v.setBackgroundDrawable(v.getBackground());
                } else {
                    setClickStyle(true, v);
                }
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

            } else {
                if (v.getBackground() != null) {
                    v.getBackground().setColorFilter(
                            new ColorMatrixColorFilter(BT_NOT_SELECTED));
                    v.setBackgroundDrawable(v.getBackground());
                } else {
                    setClickStyle(false, v);
                }
            }
            return false;
        }
    };
    
    public final static void setViewFocusChanged(View inView) {
        inView.setOnTouchListener(buttonOnTouchListener);
        inView.setOnFocusChangeListener(buttonOnFocusChangeListener);
    }
}

Related Tutorials