My app creates an ActionBar (androidx.appcompat.app.ActionBar) like so:
mActionBar = getSupportActionBar();
I need to detect touch events in the ActionBar and use dispatchTouchEvent() to do so:
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
super.dispatchTouchEvent(ev);
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
// Do something, if and only if the ActionBar is tapped.
}
return false;
}
This detects touch events occuring anywhere in the app. I want to consider only those events coming from mActionBar. TIA!
I forgot to mention that the activity in question consists only of a WebView and a FloatingActionButton:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/webv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/browser_page"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/user_view"
android:background="@null"
android:layout_margin="10dp"
android:visibility="invisible"
android:listSelector="@android:color/darker_gray"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_comment"
android:visibility="visible"
app:fabSize="normal"
app:srcCompat="@drawable/plus_circle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="100dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="10dp"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
I changed the dispatchTouchEvent() as follows:
public boolean dispatchTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
super.dispatchTouchEvent(ev);
if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getY() <= mActionBar.getHeight() + actionbar_start) {
// Do something, if and only if the ActionBar is tapped.
return false;
}
return true;
}
STATUS_BAR_HEIGHT is 24dp and
DisplayMetrics metrics = getResources().getDisplayMetrics();
actionbar_start = Math.round(STATUS_BAR_HEIGHT * metrics.density);
.