I want to implement android bottom sheet in Onlongclick of my Recyclerview Adapter class, but am trying to press long click on my recyclerview item its got crashed.
public class AddAtendanceAdapter extends
RecyclerView.Adapter<AddAtendanceAdapter.AttendanceViewHolder> {
public List<Details> dAttendance = Collections.emptyList();
private LayoutInflater inflater;
private static Context context;
private View v;
public AddAtendanceAdapter(Context context, List<Details> dAttendance) {
this.dAttendance = dAttendance;
this.context = context;
inflater = LayoutInflater.from(context);
}
@Override
public AttendanceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_item_row,
parent, false);
AttendanceViewHolder pvh = new AttendanceViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(AttendanceViewHolder holder, int postions) {
Details details = dAttendance.get(postions);
holder.StudentsName.setText(details.getStudentName());
Glide.with(context)
.load(details.getStudentImage())
.into(holder.stdImg);
}
@Override
public int getItemCount() {
if (dAttendance != null) {
return dAttendance.size();
}
return 0;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
public static class AttendanceViewHolder extends RecyclerView.ViewHolder {
TextView StudentsName;
CircleImageView stdImg;
private Activity activity;
AttendanceViewHolder(View itemView) {
super(itemView);
StudentsName = (TextView) itemView.findViewById(R.id.txtStdName);
stdImg = (CircleImageView) itemView.findViewById(R.id.stdImg);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Log.d("juu","hhh");
openBottomSheet(v);
return false;
}
});
}
private void openBottomSheet(View v) {
//View view = activity.getLayoutInflater ().inflate (R.layout.bottom_sheet, null);
// View view = inflater.inflate( R.layout.bottom_sheet, null );
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate (R.layout.bottom_sheet, null);
TextView txtBackup = (TextView)view.findViewById(R.id.txt_backup);
TextView txtDetail = (TextView)view.findViewById(R.id.txt_detail);
TextView txtOpen = (TextView)view.findViewById(R.id.txt_open);
final TextView txtUninstall = (TextView)view.findViewById( R.id.txt_uninstall);
final Dialog mBottomSheetDialog = new Dialog (context, R.style.MaterialDialogSheet);
mBottomSheetDialog.setContentView (view);
mBottomSheetDialog.setCancelable (true);
mBottomSheetDialog.getWindow ().setLayout (LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
mBottomSheetDialog.getWindow ().setGravity (Gravity.BOTTOM);
mBottomSheetDialog.show ();
txtBackup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Clicked Backup",Toast.LENGTH_SHORT).show();
mBottomSheetDialog.dismiss();
}
});
txtDetail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Clicked Detail",Toast.LENGTH_SHORT).show();
mBottomSheetDialog.dismiss();
}
});
txtOpen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Clicked Open",Toast.LENGTH_SHORT).show();
mBottomSheetDialog.dismiss();
}
});
txtUninstall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context,"Clicked Uninstall",Toast.LENGTH_SHORT).show();
mBottomSheetDialog.dismiss();
}
});
}
}
}
my log cat is
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application at android.view.ViewRootImpl.setView(ViewRootImpl.java:583) at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310) at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) at android.app.Dialog.show(Dialog.java:319) at in.edsys.teacherapp.adapters.AddAtendanceAdapter$AttendanceViewHolder.openBottomSheet(AddAtendanceAdapter.java:119) at in.edsys.teacherapp.adapters.AddAtendanceAdapter$AttendanceViewHolder.access$000(AddAtendanceAdapter.java:80) at in.edsys.teacherapp.adapters.AddAtendanceAdapter$AttendanceViewHolder$1.onLongClick(AddAtendanceAdapter.java:95)