Skip to content

Instantly share code, notes, and snippets.

@rajivnarayana
Created December 10, 2012 13:17
Show Gist options
  • Save rajivnarayana/4250508 to your computer and use it in GitHub Desktop.
Save rajivnarayana/4250508 to your computer and use it in GitHub Desktop.
Custom Activity to mimic "Show Overlay Touches" for devices < 4.0.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="25dp" />
<stroke android:color="#7F00007F" android:width="1dp"/>
<solid android:color="#7FFFFFFF" />
</shape>
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class TouchOverlayDemo extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ListView listView = new ListView(this);
listView.setAdapter(new MyAdapter());
final View touchOverlay = new View(this);
touchOverlay.setBackgroundResource(R.drawable.touch);
final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50,50);
RelativeLayout relativeLayout = new RelativeLayout(this) {
public boolean onInterceptTouchEvent(android.view.MotionEvent ev) {
final int action = ev.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
touchOverlay.setVisibility(View.VISIBLE);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
touchOverlay.setVisibility(View.INVISIBLE);
break;
}
return true;
};
@Override
public boolean onTouchEvent(MotionEvent event) {
params.setMargins((int)event.getX()- 25, (int)event.getY()-25, -50, -50);
touchOverlay.setLayoutParams(params);
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
touchOverlay.setVisibility(View.INVISIBLE);
break;
}
listView.dispatchTouchEvent(event);
return true;
}
};
relativeLayout.addView(listView);
relativeLayout.addView(touchOverlay, params);
touchOverlay.setVisibility(View.INVISIBLE);
setContentView(relativeLayout);
}
class MyAdapter extends ArrayAdapter<String> {
public MyAdapter() {
super(TouchOverlayDemo.this, android.R.layout.simple_list_item_1, android.R.id.text1);
}
@Override
public int getCount() {
return 100;
}
@Override
public String getItem(int position) {
return "Cell "+(position + 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment