0

What I want to be able to achieve is to select multiple items from an alert dialog box (which I have managed to do) and then display these selections in a list view.

I have tried a few different ideas but as my experience is limited I am struggling.

If anyone can help it would be much appreciated, thanks in advance.

Here is my .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="match_parent"
tools:context="com.example.android.colors01.MainActivity">

    <Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="Tone"
    android:layout_centerHorizontal="true"/>

    <TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/btn"
    android:textSize="20dp"/>

</RelativeLayout>

and this is my .java

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = findViewById(R.id.btn);
        final TextView tv = findViewById(R.id.tv);

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

                // Build an AlertDialog
                AlertDialog.Builder builder = new alertDialog.Builder(MainActivity.this);

                // String array for alert dialog multi choice items
                final String[] tone = new String[]{
                        "Red",
                        "Yellow",
                        "Green",
                        "Purple",
                        "Pink"
                };

                final boolean [] checkedTones = new boolean[]{
                        false,  // Red
                        false,  // Yellow
                        false,  // Green
                        false,  // Purple
                        false   // Pink
                };

                final List<String> toneList = Arrays.asList(tone);

                builder.setMultiChoiceItems(tone, checkedTones, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which, boolean isChecked) {

                        // Update the current focused item's checked status
                        checkedTones[which] = isChecked;

                        // Get the current focused item
                        String currentItem = toneList.get(which);

                        // Notify the current action
                        Toast.makeText(getApplicationContext(), tone[which], Toast.LENGTH_SHORT).show();
                    }
                });

                // Specify the dialog is not cancelable
                builder.setCancelable(false);

                // Set a title for alert dialog
                builder.setTitle("Your Tone");

                // Set the positive/yes button click listener
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click positive button
                        tv.setText("Your Tone\n");
                        for (int i = 0; i<checkedTones.length; i++){
                            boolean checked = checkedTones[i];
                            if (checked) {
                                tv.setText(tv.getText() + toneList.get(i) + "\n");
                            }
                        }
                    }
                });

                // Set the negative/no button click listener
                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the negative button
                    }
                });

                // Set the neutral/cancel button click listener
                builder.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something when click the neutral button
                    }
                });

                AlertDialog dialog = builder.create();
                // Display the alert dialog on interface
                dialog.show();
            }
        });
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Rickie
  • 1
  • 1
  • does this help ?https://stackoverflow.com/questions/20017329/android-select-items-in-a-multi-select-listview-inside-alertdialog – Angel Koh Feb 26 '18 at 01:57
  • Thanks that is what I am trying to achieve. I've tried to replicate that code for the list view when the positive button is clicked however I am not having any luck. What exactly do I need to change to get it to work. – Rickie Feb 26 '18 at 20:53

1 Answers1

0

You can use custom dialog for this with your own ui interface and logic example is here https://www.mkyong.com/android/android-custom-dialog-example/. Or you can use Dialog fragment which is light weight and reusable where ever you want. Example is here https://medium.com/@xabaras/creating-a-custom-dialog-with-dialogfragment-f0198dab656d

kalyan
  • 91
  • 3