Dialog with MultipleChoice ListView

July 31, 2017
ScreenShots:






strings.xml:
<resources>
    <string name="app_name">Dialog with Multiple Choice</string>
    <string-array name="ITEMS">
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
        <item>Item4</item>
        <item>Item5</item>
    </string-array>
</resources>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">
    <Button         android:id="@+id/button_dialogList"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Dialog List" />     <TextView         android:id="@+id/textview"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_marginLeft="10dp"         android:layout_marginTop="20dp"         android:textSize="30sp"         android:textStyle="bold" /> </LinearLayout>
MainActivity.java:
package com.ramsandroid.dialogwithmultiplechoice;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    ArrayList<Integer> selectedItems;
    TextView textView;
    @Override     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        selectedItems = new ArrayList<>();
        textView = (TextView) findViewById(R.id.textview);
Button button_dialogList = (Button) findViewById(R.id.button_dialogList);
        button_dialogList.setOnClickListener(this);
    }
    @Override    public void onClick(View v) {
  AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
        builder.setTitle("Select Item");
        builder.setMultiChoiceItems(R.array.ITEMS, null, new DialogInterface.OnMultiChoiceClickListener() {
            @Override             public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                if (isChecked) {
                    selectedItems.add(which);
                } else
if (selectedItems.contains(which)) {
          selectedItems.remove(Integer.valueOf(which));
                }
            }
        });
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override             public void onClick(DialogInterface dialog, int which) {
                for (Integer i : selectedItems) {
    String[] ITEMS = getResources().getStringArray(R.array.ITEMS);
                    textView.append(ITEMS[i] + "\n");
              }
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override       public void onClick(DialogInterface dialog, int which) {
                selectedItems.clear();
            }
        });
        builder.create().show();
    }
}

Artikel Terkait

Latest
Previous
Next Post »