In case of an AppCompatActivity with title (set by setTitle
call), (how) can I detect touches on this title section:
In case this is not (easily) possible, I'm populating this activity with setContentView
.
Here is my simplified layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@ id/fl"/>
</LinearLayout>
fl
will be replaced by getSupportFragmentManager().beginTransaction().replace
with a PreferenceFragmentCompat
.
I wish to detect touches either on the titlebar, or underneath the preference items.
However I tried to call setOnTouchListener
on fl
(FrameLayout fl = findViewById(R.id.fl)
), and it's never called.
CodePudding user response:
set the clicklistener on toolbar since you are using the setTitle for setting the title.
Toolbar toolbar = findViewById(R.id.action_bar);
if(toolbar != null) {
for (int i = 0; i < toolbar.getChildCount(); i ){
View child = toolbar.getChildAt(i);
if(child instanceof TextView || child instanceof ImageView) {
child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Show some toast or log message
}
});
}
}
}
Let me know if this works