-1

I am trying to alert multi selection dropdown check box in alert dialog like spinner but with check boxes in my android application ,can any one help here?

camelCaseCoder
  • 1,447
  • 19
  • 32
Dilip
  • 66
  • 10
  • Do you mean you want a spinner that shows another drop down list over the alert dialog? That sounds like a really strange UI. – Xiaoyu Yu Nov 02 '15 at 05:10
  • No I meant spinner like feature but with multi selection check boxes. – Dilip Nov 02 '15 at 05:50

5 Answers5

1

It may not work properly with respect to touch events. I guess that you will have to clone it and develop something like MultiSelectSpinner. You might wanna consult this answer for further detail.

Community
  • 1
  • 1
Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17
0

Use customlayout in AlertDialog class using api, dialog.setconteView() method. You can add whatever widgets you wish in your custom layout

NIPHIN
  • 1,071
  • 1
  • 8
  • 16
  • I couldn't create a custom layout with drop down feature that holds check boxes. – Dilip Nov 02 '15 at 06:04
  • use this answer, the author has conveniently written a multi-select spinner, you could make use of it. http://stackoverflow.com/questions/5015686/android-spinner-with-multiple-choice – NIPHIN Nov 02 '15 at 06:20
  • I used the same method to get what I needed. Thanx. – Dilip Nov 02 '15 at 10:14
0

Use below code:-

AlertDialog.Builder builder = new AlertDialog.Builder(
                context);
        builder.setTitle("Title");
        builder.setMultiChoiceItems(list, null,
                new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog,
                            int indexSelected, boolean isChecked)
                    {


                    }
                })
                .setPositiveButton("OK",
                        new DialogInterface.OnClickListener() 
                {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) 
                            {

                            }
                        })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int id) 
                            {
                            }
                        });
        dialog = builder.create();
        dialog.show();
  • It is the same feature which I acquired but I need this feature in dropdown i.e the check box should be shown as a drop down. – Dilip Nov 02 '15 at 06:02
  • then simply make a custom dialog. –  Nov 02 '15 at 06:58
0

As your view, Cant get the click events in AlertDialog.

Best solution for your Question. Just include the custom layout and get the Alertdialog Click events in your activity.

  1. create a XML layout which you want to design
  2. Include that XML layout in your activity through SetContentView()
  3. Get the click events through your activity.

Please check this link for reference http://www.mkyong.com/android/android-custom-dialog-example/

Happy Coding :)

Venkatesh Selvam
  • 1,382
  • 2
  • 15
  • 28
0

for Custom checkbox dropdown Dialogbox first we need to create MultiCheckAdaptar

public class MultiCheckAdaptar extends RecyclerView.Adapter<MultiCheckAdaptar.MyViewHolder>  {
    protected Context context;
    private LayoutInflater inflater;
    private ArrayList<ModelSpacialization> joblist   ;
    private String TYPE = "";
    private int count = 0 ;
    MultiCheckAdaptar.OnItemClickListener listener;

    public interface OnItemClickListener {
        void onClick(ModelSpacialization jobs, int pos , boolean type);
    }

    public MultiCheckAdaptar(Context context, ArrayList<ModelSpacialization> list, MultiCheckAdaptar.OnItemClickListener listener) {
        this.context = context;
        this.listener = listener;
        if (context != null) {
            inflater = LayoutInflater.from(context);
            this.joblist= new ArrayList<>();
            this.joblist.addAll(list);

         }
    }

    public MultiCheckAdaptar.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.layout_ckeck_item , parent, false);
        MultiCheckAdaptar.MyViewHolder holder = new MultiCheckAdaptar.MyViewHolder(view);
        return holder;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    @Override
    public void onBindViewHolder(@NonNull final MultiCheckAdaptar.MyViewHolder holder, final int position) {
        ModelSpacialization item = joblist.get(position);
        holder.txt_item.setText(item.getName());

        if(item.isCheck())
           holder.txt_item.setChecked(true) ;
         else
            holder.txt_item.setChecked(false) ;

        holder.txt_item.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                   listener.onClick(item, position, false);
                }
        });
    }

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

    class MyViewHolder extends RecyclerView.ViewHolder {
       CheckBox txt_item ;
          public MyViewHolder(View itemView) {
            super(itemView);
              txt_item = itemView.findViewById(R.id.txt_item);

        }

    }
}

layout_check_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <CheckBox
        android:id="@+id/txt_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/size_10"
        android:text="text"

        android:textSize="@dimen/size_16" />

</RelativeLayout>

dialog_dropdwon_recyle.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <TextView
        android:text="TItle"
        android:id="@+id/tv_dialogTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textColor="@color/primary"
        android:fontFamily="@font/poppins_medium"
        android:textSize="@dimen/size_16"

        tools:ignore="SpUsage" />

    <ImageButton
        android:id="@+id/btn_dialog_close"
        android:layout_width="@dimen/size_25"
        android:layout_height="@dimen/size_25"
        android:layout_alignParentEnd="true"
        android:layout_gravity="right"
        android:background="@drawable/btn_grey_transparent"
        android:elevation="1dp"
        android:padding="@dimen/size_5"
        android:src="@drawable/ic_close" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_dialog_close"
        android:paddingHorizontal="@dimen/size_5"
        android:orientation="vertical">

        <EditText
            android:id="@+id/edit_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search..."
            android:visibility="gone" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dialog_list"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/size_10"
            android:scrollbars="none" />
        <Button
            android:id="@+id/btn_save"
            android:layout_width="match_parent"
            android:layout_height="@dimen/size_40"
            android:layout_marginHorizontal="@dimen/size_10"
            android:layout_marginTop="@dimen/size_10"
            android:layout_marginBottom="@dimen/size_10"
            android:background="@drawable/button_primary"
            android:fontFamily="@font/poppins_medium"
            android:text="Save"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="@dimen/size_15" />
    </LinearLayout>
</RelativeLayout>

in Activity

Dialog dg_industries = new Dialog(context);
        selected_spaci = new HashSet<>();
        industresList.addAll(industries2);
        selected_spaci = new HashSet<>();
        dg_industries.setContentView(R.layout.dailogbox_dwondwon_recycle);
        final RecyclerView indview = (RecyclerView) dg_industries.findViewById(R.id.dialog_list);
        final TextView title = (TextView) dg_industries.findViewById(R.id.tv_dialogTitle);
        title.setVisibility(View.VISIBLE);
        title.setText("Select  Specialization");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        indview.setLayoutManager(linearLayoutManager);
        adaptor = new MultiCheckAdaptar(context, industresList, new MultiCheckAdaptar.OnItemClickListener() {
            @Override
            public void onClick(ModelSpacialization jobs, int pos, boolean type) {
                jobs.setCheck(type);
                selected_spaci.add(jobs);
                Log.e("industries2", new Gson().toJson(industresList));
            }
        });

        dg_industries.setCancelable(false);
        dg_industries.show();