I have problem with setOnLongClickListener. When I use this method I want the dialog, which will open, to automatically close by itself (which the method's return false should do) when I take my finger off the screen. Is it possible to do this with this method, or are there other suitable methods?
Create Dialog
protected void showDialogLongClick(final int position){
final Dialog dialog = new Dialog(activity);
dialog.setCancelable(true);
View v = activity.getLayoutInflater().inflate(R.layout.onlongclick_card_mygift,null);
dialog.setContentView(v);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
nameLong = v.findViewById(R.id.name_longclick);
descriptionLong = v.findViewById(R.id.gift_description);
addressLong = v.findViewById(R.id.gift_address);
imageCategory = v.findViewById(R.id.small_category);
nameLong.setText(myGift.get(position).getName());
descriptionLong.setText(myGift.get(position).getDescription());
addressLong.setText(myGift.get(position).getAddress());
String cat = myGift.get(position).getCategory();
changeCategoryImage(cat, imageCategory);
dialog.show();
}
Listener
holder.card.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
showDialogLongClick(position);
return false;
}
});
XML card dialog
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_card_mygift"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name_longclick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:textSize="25sp"
android:layout_weight="1"
android:layout_marginEnd="10dp"/>
<ImageView
android:id="@+id/small_category"
android:layout_weight="0"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#F44336"
android:textSize="22sp"/>
<TextView
android:id="@+id/gift_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#F44336"
android:textSize="22sp"/>
<TextView
android:id="@+id/gift_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#FFFFFF"
android:textSize="20sp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Dismiss the dialog
}
return false;
}
});
view.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
//Create the dialog
return true;
}
});
Use setOnLongClickListener for creating the dialog and use setOnTouchListener for dismissing the dialog.
So you should add setOnTouchListener to the view. Call dialog.dismiss() when MotionEvent action is MotionEvent.ACTION_UP (user lifts his finger).
If the view is inside a scroll view the views onTouchListener does not get called. A simple solution is to add an onTouchListener to the scroll view and dismiss the dialog when user lifts his finger.
scrollView.setOnTouchListener( new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Dismiss the dialog
return true;
}
return false;
}
});