In my android app, i have 3 full screen ImageButtons ( horizontally inside LinearLayout) and i have hidden the status bar (full screen app)
The problem is that when i swipe down to show the status bar, the ImageButton on top, gets clicked, i tried detecting the clicks so i disable button's functionality if it is a swipe not a click like this
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN :
y1 = event.getY();
i=0;
break;
case MotionEvent.ACTION_UP:
y2 = event.getY();
float deltaY = y2-y1;
if(deltaY < 10 && deltaY > -10)
{
flag=true;
}
else
{
flag=false;
}
break;
}
return super.onTouchEvent(event);
}
But it doesn't seem to work.
Any help would be very appreciated, Thank you in advance.
Edit 1 :
XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="3"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#567"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
style="?android:attr/buttonStyleSmall"
android:background="?android:attr/selectableItemBackground"
android:onClick="speech"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#456"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
style="?android:attr/buttonStyleSmall"
android:onClick="pics"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#345"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
style="?android:attr/buttonStyleSmall"
android:id="@+id/vids"
/>
</LinearLayout>
</LinearLayout>
Java :
public class BirthdayActivity extends AppCompatActivity {
private float x1,x2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_birthday);
}
public void speech(View v)
{
finish();
Intent intent = new Intent(BirthdayActivity.this , SpeechActivity.class);
BirthdayActivity.this.startActivity(intent);
}
public void pics(View v)
{
finish();
Intent intent = new Intent(BirthdayActivity.this , PicsActivity.class);
BirthdayActivity.this.startActivity(intent);
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN :
x1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getY();
float Delta = x2-x1;
//if on actionUp, the distance is big == swipe
if(Delta < 5 && Delta(-5))
{
}
else
{
}
break;
}
return super.onTouchEvent(event);
}
}
The return of
onTouchEvent
istrue
if you consumed the action andfalse
if you haven't.
So, you should to this -
@Override
public boolean onTouchEvent(MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN :{
//something
return true;
}
case MotionEvent.ACTION_UP: {
//something else
return true;
}
}
//some other event, let system handle it
return super.onTouchEvent(event);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.