1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.view; 18 19 import com.example.android.apis.R; 20 21 import android.app.Activity; 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.os.Bundle; 25 import android.util.AttributeSet; 26 import android.util.Log; 27 import android.view.LayoutInflater; 28 import android.view.MotionEvent; 29 import android.view.View; 30 import android.view.ViewGroup; 31 import android.widget.CheckBox; 32 import android.widget.CompoundButton; 33 import android.widget.LinearLayout; 34 import android.widget.TextView; 35 36 import java.util.ArrayList; 37 38 39 /** 40 * Demonstrates how to use {@link View#onHoverEvent}, {@link ViewGroup#onInterceptHoverEvent}, 41 * and {@link View#setOnHoverListener}. 42 * 43 * This activity displays a few buttons and text fields and entices the user 44 * to hover over them using a mouse or touch pad. It displays feedback reporting 45 * the position of the pointing device and the label of the view being hovered. 46 * 47 * A button changes from dark green to bright yellow when hovered. 48 * This effect is achieved by using a state-list drawable to select among different 49 * background shapes and colors based on the hover state of the button. 50 * 51 * A {@link View#OnHoverEventListener} is used to listen for hover events within the 52 * container. The container will re 53 * 54 * A checkbox is used to control whether a special view, the Interceptor, will intercept 55 * events before they are sent to its child (a button). When the Interceptor 56 * is intercepting events, the button will not change state as the pointer hovers 57 * over it because the interceptor itself will grab the events. 58 */ 59 public class Hover extends Activity { 60 private TextView mMessageTextView; 61 private CheckBox mInterceptCheckBox; 62 private HoverInterceptorView mInterceptor; 63 64 @Override 65 protected void onCreate(Bundle savedInstanceState) { 66 super.onCreate(savedInstanceState); 67 68 setContentView(R.layout.hover); 69 70 mMessageTextView = (TextView) findViewById(R.id.message); 71 mInterceptCheckBox = (CheckBox) findViewById(R.id.intercept_checkbox); 72 mInterceptor = (HoverInterceptorView) findViewById(R.id.interceptor); 73 74 View container = findViewById(R.id.container); 75 container.setOnHoverListener(new View.OnHoverListener() { 76 @Override 77 public boolean onHover(View v, MotionEvent event) { 78 switch (event.getAction()) { 79 case MotionEvent.ACTION_HOVER_ENTER: 80 mMessageTextView.setText(Hover.this.getResources().getString( 81 R.string.hover_message_entered_at, 82 event.getX(), event.getY())); 83 break; 84 case MotionEvent.ACTION_HOVER_MOVE: 85 mMessageTextView.setText(Hover.this.getResources().getString( 86 R.string.hover_message_moved_at, 87 event.getX(), event.getY())); 88 break; 89 case MotionEvent.ACTION_HOVER_EXIT: 90 mMessageTextView.setText(Hover.this.getResources().getString( 91 R.string.hover_message_exited_at, 92 event.getX(), event.getY())); 93 break; 94 } 95 return false; 96 } 97 }); 98 99 mInterceptCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 100 @Override 101 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 102 mInterceptor.setInterceptHover(isChecked); 103 } 104 }); 105 } 106 } 107