Touch screen
package app.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
class TrueButton extends BooleanButton {
protected boolean myValue() {
return true;
}
public TrueButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
abstract class BooleanButton extends Button {
protected boolean myValue() {
return false;
}
public BooleanButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
String myTag = this.getTag().toString();
Log.v(myTag, Test.describeEvent(this, event));
Log.v(myTag, "super onTouchEvent() returns " + super.onTouchEvent(event));
Log.v(myTag, "and I'm returning " + myValue());
return (myValue());
}
}
class FalseButton extends BooleanButton {
public FalseButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
public class Test extends Activity implements OnTouchListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.layout1);
layout1.setOnTouchListener(this);
Button trueBtn1 = (Button) findViewById(R.id.trueBtn1);
trueBtn1.setOnTouchListener(this);
Button falseBtn1 = (Button) findViewById(R.id.falseBtn1);
falseBtn1.setOnTouchListener(this);
RelativeLayout layout2 = (RelativeLayout) findViewById(R.id.layout2);
layout2.setOnTouchListener(this);
Button trueBtn2 = (Button) findViewById(R.id.trueBtn2);
trueBtn2.setOnTouchListener(this);
Button falseBtn2 = (Button) findViewById(R.id.falseBtn2);
falseBtn2.setOnTouchListener(this);
}
public boolean onTouch(View v, MotionEvent event) {
String myTag = v.getTag().toString();
Log.v(myTag, describeEvent(v, event));
if ("true".equals(myTag.substring(0, 4))) {
return true;
} else {
return false;
}
}
protected static String describeEvent(View view, MotionEvent event) {
StringBuilder result = new StringBuilder(300);
result.append("Action: ").append(event.getAction()).append("\n");
result.append("Location: ").append(event.getX()).append(" x ")
.append(event.getY()).append("\n");
if (event.getX() < 0 || event.getX() > view.getWidth()
|| event.getY() < 0 || event.getY() > view.getHeight()) {
result.append(">>> Touch has left the view <<<\n");
}
result.append("Edge flags: ").append(event.getEdgeFlags()).append("\n");
result.append("Pressure: ").append(event.getPressure()).append(" ");
result.append("Size: ").append(event.getSize()).append("\n");
result.append("Down time: ").append(event.getDownTime()).append("ms\n");
result.append("Event time: ").append(event.getEventTime()).append("ms");
result.append(" Elapsed: ").append(
event.getEventTime() - event.getDownTime());
result.append(" ms\n");
return result.toString();
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout android:id="@+id/layout1"
android:tag="trueLayoutTop" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" >
<com.androidbook.touch.demo1.TrueButton android:text="returns true"
android:id="@+id/trueBtn1" android:tag="trueBtnTop"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<com.androidbook.touch.demo1.FalseButton android:text="returns false"
android:id="@+id/falseBtn1" android:tag="falseBtnTop"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/trueBtn1" />
</RelativeLayout>
<RelativeLayout android:id="@+id/layout2"
android:tag="falseLayoutBottom" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="1" android:background="#FF00FF" >
<com.androidbook.touch.demo1.TrueButton android:text="returns true"
android:id="@+id/trueBtn2" android:tag="trueBtnBottom"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<com.androidbook.touch.demo1.FalseButton android:text="returns false"
android:id="@+id/falseBtn2" android:tag="falseBtnBottom"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_below="@id/trueBtn2" />
</RelativeLayout>
</LinearLayout>
Related examples in the same category