Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open a popup window from an adapter class

Tags:

android

popup

I created a class for showing employee details in a CardView inside a RecyclerView . New requirement is to show a popup window of card while clicking. So I added setOnClickListener and it works fine(I got card name in Log). But how do I open a popup window from this click? Actually am a newbie in android

public class EmployeeCardAdapter extends RecyclerView.Adapter<EmployeeCardAdapter.ViewHolder> {

private Context context;
private EmployeeBean employeeBean;
private ArrayList<EmployeeBean> employeeList;



public EmployeeCardAdapter(Context context, ArrayList<EmployeeBean> employeeList){
    this.context = context;
    this.employeeList = employeeList;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

   View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_card, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {

    EmployeeBean employeeBean = employeeList.get(position);
    viewHolder.employeeCardTitle.setText(employeeBean.getName());
    viewHolder.employeeName.setText(employeeBean.getName());
    viewHolder.employeeQualification.setText(employeeBean.getQualification());
    viewHolder.employeeExperiance.setText(employeeBean.getExperiance());

}
@Override
public int getItemCount() {
    return employeeList.size();
}


public static class  ViewHolder extends RecyclerView.ViewHolder{

    public TextView employeeCardTitle;
    public TextView employeeName;
    public TextView employeeQualification;
    public TextView employeeExperiance;

    public View view;
    public ClipData.Item currentItem;
    public ViewHolder(final View itemView) {
        super(itemView);
        employeeCardTitle = (TextView)itemView.findViewById(R.id.employee_card_title);
        employeeName = (TextView)itemView.findViewById(R.id.employee_name);
        employeeQualification = (TextView)itemView.findViewById(R.id.employee_Qualification);
        employeeExperiance = (TextView)itemView.findViewById(R.id.employee_experiance);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {



            }
        });
    }
}

}

I searched a lot of tutorials but every tutorials are opening the popup window from Activity class. But here I am using fragment. I designed a popup window layout as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/employee_popup_id"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/light_blue_tranparency">

    <TextView
        android:id="@+id/employee_card_title"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:background="@color/employee_card"
        android:text="contact det"
        android:gravity="center"
        android:textColor="@color/text_shadow_white"
        android:textStyle="bold"
        android:textSize="18dp"/>

        <ImageView
            android:id="@+id/employee_image"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:src="@drawable/ic_profile"
            android:layout_marginTop="4dp"
            android:gravity="center_horizontal"
            android:layout_below="@id/employee_card_title"
           />

    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/card_name_text_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/employee_image">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="Name"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:text=":"
            android:gravity="center_horizontal"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:id="@+id/employee_name"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/card_qualification_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_name_text_layout_id">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="qualification"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:text=":"
            android:gravity="center_horizontal"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:id="@+id/employee_Qualification"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:id="@+id/card_experiance_layout_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/card_qualification_layout_id">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.4"
            android:layout_height="wrap_content"
            android:text="Experiance"
            android:textSize="15dp"
            android:textColor="@color/text_dark_black"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:layout_width="0dp"
            android:layout_weight="0.1"
            android:layout_height="wrap_content"
            android:text=":"
            android:gravity="center_horizontal"
            android:textColor="@color/text_dark_black"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="5dp"/>
        <TextView
            android:id="@+id/employee_experiance"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:layout_marginTop="10dp"
            android:textColor="@color/text_dark_black"
            android:layout_marginLeft="5dp"/>
    </LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/card_experiance_layout_id"
    android:layout_marginBottom="4dp">

    <Button
        android:id="@+id/listen_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="button"/>

</LinearLayout>

Can I open this layout as a popup window from above adapter class ??

like image 464
Vishnu KR Avatar asked Dec 20 '15 17:12

Vishnu KR


2 Answers

Once try this if you wants to open popup window at particular position than you need give position in showasdropdown.

public void showPopup(View view) {
        View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        Button btnDismiss = (Button) popupView.findViewById(R.id.btn_dismiss);
        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(popupView, 0, 0);
    }
like image 139
user1140237 Avatar answered Nov 15 '22 04:11

user1140237


LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View layout = inflater.inflate(R.layout.popup_compare,null);
AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getRootView().getContext());
alertbox.setMessage("No Internet Connection");
alertbox.setView(layout);
alertbox.setNeutralButton("OK",new DialogInterface.OnClickListener() {public void onClick(DialogInterface arg0,int arg1) {
//to do
}});
alertbox.show();
like image 34
Chandan Yadav Avatar answered Nov 15 '22 03:11

Chandan Yadav