[JAVA] I want to select multiple items with a custom layout in Dialog

Thing you want to do

There was no problem in selecting only one (Single Choice), but I was addicted to multiple selections, so I made a note.

activity_main.xml Layout that just displays the button.

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:gravity="center"
              android:orientation="vertical"
              tools:context="example.com.samplemultichoice.MainActivity">

    <TextView
        android:id="@+id/btnOpenDialog"
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:background="#000000"
        android:textColor="#FFFFFF"
        android:gravity="center"
        android:text="Dialog open"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold"/>

</LinearLayout>

choice.xml Layout for one case in the dialog

choice.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:layout_gravity="center" >

    <LinearLayout
        android:id="@+id/checkboxArea"
        android:layout_width="0dp"
        android:layout_height="80dp"
        android:layout_weight="20">

        <ImageView
            android:id="@+id/agentCheck"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="10dp"
            android:scaleType="fitCenter"
            android:src="@drawable/check_off" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="80">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                >

                <TextView
                    android:id="@+id/textMain"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:textSize="32dp"
                    android:text="Dummy text" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <TextView
                    android:id="@+id/textSub"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:textSize="20dp"
                    android:text="Dummy text" />

            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</LinearLayout>

MainActivity.java The final thing.

  1. Prepare a subclass with information for choices
  2. Prepare your own adapter, display this here, and write something like
  3. Prepare Dialog generation part

Direction such as. Actually, from here, I narrow down by name before displaying.

By the way, the test data uses Use the list of managers in Osaka City.

MainActivity.java


package example.com.samplemultichoice;

import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private TextView btnOpenDialog; //Button to open Dialog with tap

    private ArrayList<Item> allItems;   //Storage location of option information
    private Context con = MainActivity.this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnOpenDialog = (TextView) findViewById(R.id.btnOpenDialog);

        //Prepare choices
        allItems = this.setData();

        //Button click event
        btnOpenDialog.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Prepare choices
                DialogAdapter adapter = new DialogAdapter(getApplicationContext(), 0, allItems);
                ListView listView = new ListView(con);
                AlertDialog.Builder dialog = new AlertDialog.Builder(con);

                //Prepare processing when tapping an option
                listView.setOnItemClickListener(new ListView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        //Switching check status
                        allItems.get(position).setFlg(!(allItems.get(position).isFlg()));

                        //Switch the display of the check image
                        ImageView checkImg = view.findViewById(R.id.agentCheck);
                        if (allItems.get(position).isFlg()) {
                            checkImg.setImageResource(R.drawable.check_on);
                        } else {
                            checkImg.setImageResource(R.drawable.check_off);
                        }
                    }
                });

                //Preparation of title
                TextView newTitle = new TextView(con);
                newTitle.setText("Please select");
                newTitle.setTextSize(24.0f);

                //Dialog display
                listView.setAdapter(adapter);
                dialog.setView(listView).create();
                dialog.setPositiveButton("Done", null);
                dialog.setCustomTitle(newTitle);
                dialog.show();
            }
        });
    }

    //Original adapter
    private class DialogAdapter extends ArrayAdapter<Item> {
        private LayoutInflater inflater;

        private DialogAdapter(Context context, int resource, List<Item> objects) {
            super(context, resource, objects);
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            Item item = getItem(position);
            if (null == v) {
                v = inflater.inflate(R.layout.choice, null);
            }

            //Main display item
            TextView textMain = v.findViewById(R.id.textMain);
            textMain.setText(item.getName());

            //Sub display item
            TextView textSub = v.findViewById(R.id.textSub);
            textSub.setText(item.getSub());

            //Switch the check box image depending on the check status
            ImageView checkImg = v.findViewById(R.id.agentCheck);
            if (item.isFlg()) {
                checkImg.setImageResource(R.drawable.check_on);
            } else {
                checkImg.setImageResource(R.drawable.check_off);
            }

            return v;
        }
    }

    //Subclass for holding information displayed as an option
    private class Item {
        private int id      = 0;     //For system identification
        private String name = "";    //Main display item
        private String sub  = "";    //Sub display item
        private boolean flg = false; //Check status

        private int getId() {
            return id;
        }
        private void setId(int id) {
            this.id = id;
        }
        private String getName() {
            return name;
        }
        private void setName(String name) {
            this.name = name;
        }
        private String getSub() {
            return sub;
        }
        private void setSub(String sub) {
            this.sub = sub;
        }
        private boolean isFlg() {
            return flg;
        }
        private void setFlg(boolean flg) {
            this.flg = flg;
        }
    }

    //Create choice data for testing
    private ArrayList<Item> setData() {
        Item item;
        ArrayList<Item> testData = new ArrayList<>();

        item = new Item();
        item.setId(1);item.setName("Kenji Hand");item.setSub("Deputy Capital Promotion Bureau Director");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(2);item.setName("Yukihiro Inoue");item.setSub("Deputy Capital Promotion Bureau Director");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(3);item.setName("Yoshihiro Tanaka");item.setSub("Deputy Capital Promotion Bureau Director");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(4);item.setName("Katsuhiro Mizumori");item.setSub("Deputy Capital Promotion Bureau General Affairs Department Manager");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(5);item.setName("Kimihide Yoshimura");item.setSub("Deputy Capital Promotion Bureau General Affairs Department Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(6);item.setName("Gusukuma Masaki");item.setSub("Deputy Capital Promotion Bureau General Affairs Section Manager");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(7);item.setName("Nishioka Daizo");item.setSub("Deputy Capital Promotion Bureau General Affairs Section Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(8);item.setName("Yoshikazu Matsui");item.setSub("Deputy Capital Promotion Bureau Deputy Capital Planning Promotion Department Manager");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(9);item.setName("Hiroshi Sakata");item.setSub("Deputy Capital Promotion Bureau Deputy Capital Planning Promotion Department Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(10);item.setName("Yuji Kawaguchi");item.setSub("Deputy Capital Promotion Bureau Planning Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(11);item.setName("Hashimoto Shizuko");item.setSub("Deputy Capital Promotion Bureau Planning Section Chief");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(12);item.setName("Okumura Kakuichi");item.setSub("Deputy Capital Promotion Bureau Business Restructuring Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(13);item.setName("Hironori Hashimoto");item.setSub("Deputy Capital Promotion Bureau Business Restructuring Section Chief");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(14);item.setName("Tomohiro Inoue");item.setSub("Deputy Capital Promotion Bureau System Planning Manager");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(15);item.setName("Fukuoka Hirotaka");item.setSub("Deputy Capital Promotion Bureau System Planning Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(16);item.setName("Tomohiro Enoshita");item.setSub("Deputy Capital Promotion Bureau System Planning Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(17);item.setName("Kenji Masuda");item.setSub("Deputy Capital Promotion Bureau System Planning Section Chief");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(18);item.setName("Kobayashi Masumi");item.setSub("Deputy Capital Promotion Bureau System Planning Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(19);item.setName("Takeshi Awara");item.setSub("Deputy Capital Promotion Bureau Financial Coordination Section Chief");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(20);item.setName("Masanobu Kusumi");item.setSub("Deputy Capital Promotion Bureau Financial Coordination Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(21);item.setName("Masaaki Kawada");item.setSub("Deputy Capital Promotion Bureau Asset and Debt Section Chief");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(22);item.setName("Yoshiyuki Nakagawa");item.setSub("Deputy Capital Promotion Bureau Asset and Debt Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(23);item.setName("Shinzen Okada");item.setSub("Deputy Capital Promotion Bureau System Coordination Department Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(24);item.setName("Isshi Oshita");item.setSub("Deputy Capital Promotion Bureau System Coordination Department Manager");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(25);item.setName("Kuroda alone");item.setSub("Deputy Capital Promotion Bureau Strategic Coordination Section Chief");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(26);item.setName("Hideaki Mizuno");item.setSub("Deputy Capital Promotion Bureau Strategic Coordination Section Chief");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(27);item.setName("Makoto Tsujimoto");item.setSub("Deputy Capital Promotion Bureau Office Business Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(28);item.setName("Mitsuru Saito");item.setSub("Deputy Capital Promotion Bureau Office Business Manager");item.setFlg(true);
        testData.add(item);
        item = new Item();
        item.setId(29);item.setName("Yoshihiro Taniguchi");item.setSub("Deputy Capital Promotion Bureau Organizational System Manager");item.setFlg(false);
        testData.add(item);
        item = new Item();
        item.setId(30);item.setName("Takashi Sekoguchi");item.setSub("Deputy Capital Promotion Bureau Organizational System Manager");item.setFlg(true);
        testData.add(item);

        return testData;
    }
}

bonus

Sample source

Recommended Posts

I want to select multiple items with a custom layout in Dialog
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I want to display a PDF in Chinese (Korean) with thin reports
I want to ForEach an array with a Lambda expression in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Javassist second decoction)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (black magic edition)
I want to use a little icon in Rails
I want to monitor a specific file with WatchService
I want to define a function in Rails Console
I want to click a GoogleMap pin in RSpec
I want to change the path after new registration after logging in with multiple devises.
I want to find a relative path in a situation using Path
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
I want to create a form to select the [Rails] category
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
I want to make a button with a line break with link_to [Note]
I want to add a browsing function with ruby on rails
[Ruby] I want to put an array in a variable. I want to convert to an array
[Java] I want to perform distinct with the key in the object
I want to extract between character strings with a regular expression
I want to develop a web application!
I want to write a nice build.gradle
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
I want to use @Autowired in Servlet
[Rails] I want to send data of different models in a form
[Note] I want to get in reverse order using afterLast with JdbcTemplate
I tried to build a Firebase application development environment with Docker in 2020
I want to create a dark web SNS with Jakarta EE 8 with Java 11
I wanted to implement a slide show in a fashionable way with slick.
[Ruby] I want to display posted items in order of newest date
"Teacher, I want to implement a login function in Spring" ① Hello World
[For beginners] I want to automatically enter pre-registered data in the input form with a selection command.
When installing a gem with C extension in Ruby, I want to finish it quickly using multiple CPU cores like make -j4
I want to test Action Cable with RSpec test
I want to send an email in Java.
I want to use arrow notation in Ruby
[Go To Travel] I searched for a plan with a quo card in Jalan
I want to be able to read a file using refile with administrate [rails6]
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to pass APP_HOME to logback in Gradle
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I want to simply write a repeating string
I want to design a structured exception handling
rsync4j --I want to touch rsync in Java.
I want to play with Firestore from Rails
Rails6 I want to make an array of values with a check box
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
SpringSecurity I was addicted to trying to log in with a hashed password (solved)
Let's create a TODO application in Java 2 I want to create a template with Spring Initializr and make a Hello world
I tried to break a block with java (1)
I want to perform aggregation processing with spring-batch
[Rails] I want to load CSS with webpacker
I want to get the value in Ruby
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (gray magic that is not so much as black magic)
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (Royal road edition that is neither magic nor anything)