In an activity
by using
public boolean dispatchTouchEvent(MotionEvent ev)
how can I know an user is touching left Y-axis
of 20% edge area?
It should be generic and orientation handled. I don't want to create any hidden view on that.
In an activity
by using
public boolean dispatchTouchEvent(MotionEvent ev)
how can I know an user is touching left Y-axis
of 20% edge area?
It should be generic and orientation handled. I don't want to create any hidden view on that.
event.getX()
event.getX()
is inside device with 20%example :
public class YourActivity extends Activity {
private LinearLayout background;
private float xValue, yValue, leftPersentage;
private int height,width;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
height = displaymetrics.heightPixels;
width = displaymetrics.widthPixels;
leftPersentage = (width)*20/100;
background = (LinearLayout) findViewById(R.id.background);
background.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
xValue = event.getX();
yValue = event.getY();
Log.d("Values Clicked" , xValue +"and" + yValue);
if(xValue <= leftPersentage){
// perform your task
Log.d("Values Clicked onLeft" , xValue +"and" + yValue);
}
return false;
}
});
}
}
Get the display size then get the point from touch event. Compare it mathematically to know if its left 20% or not.
get display size
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
get the touch points.
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
float touchedX = ev.getX();
float touchedY = ev.getY();
return super.dispatchTouchEvent(ev);
}