MulitpleChoiceInDialogDemo
Step 1 : create MCListDemo class.
Step 2 : create AddingMCList. activity.
Step 3 : create layout file like activity_adding_mclist.xml
Step 4: run and watch the output & all the best...
Total Logic was taken from Android Studio. Link::https://developer.android.com/guide/topics/ui/dialogs.html
Step 1:
public class MCListDemo extends DialogFragment{
List<String> selectedList;
private String[] colors;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
colors = new String[]{"Red", "Green", "Blue"};
selectedList = new ArrayList<>();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select your favourite")
.setMultiChoiceItems(colors, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
if(isChecked){
selectedList.add(colors[which]);
}else {
selectedList.remove(which);
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Select List is ::"+selectedList, Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "Bye", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
Step 2:
public class AddingMCList extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adding_mclist);
}
public void meth(View view) {
DialogFragment dialogFragment = new MCListDemo();
dialogFragment.show(getFragmentManager(),"mcdemo");
}
}
Step 3:
<?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:id="@+id/activity_adding_mclist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mycompany.com.myapplication.AddingMCList">
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/mcListDialogfragment"
tools:text="MulitpleChoiseListDialog"
android:onClick="meth" />
</RelativeLayout>
Step 4:
Output: