[Android] Display of input candidates using List Popup Window

0. Execution example

スクリーンショット 2019-03-27 19.13.33.png

1. Whole code

MainActivity.java


package com.example.listpopupwindowtest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListPopupWindow;

public class MainActivity extends AppCompatActivity {

    String[] strList = {"AIUEO","Kakikukeko","SA Shi Su Se So"};

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

        final EditText et_1 = (EditText)findViewById(R.id.et_1);
        final ListPopupWindow lpw = new ListPopupWindow(this);
        lpw.setAdapter(new ArrayAdapter<String>(
                this,android.R.layout.simple_list_item_1,strList));
        lpw.setAnchorView(et_1);
        lpw.setModal(true);
        et_1.setOnTouchListener(new View.OnTouchListener() {
            final int DRAWABLE_RIGHT = 2;
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_UP){
                    if(event.getX()>=v.getWidth()-((EditText) v)
                            .getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width()){
                        lpw.show();
                        ((InputMethodManager)getSystemService(INPUT_METHOD_SERVICE))
                                .hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                        return true;
                    }
                }
                return false;
            }
        });
        lpw.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                et_1.setText(strList[position]);
                lpw.dismiss();
            }
        });
    }
}

2. Add a ▼ mark to the right end of the input field

Download the resource file below and copy core / res / res / drawable-hdpi / numberpicker_down_normal_holo_light.png to the drawable folder. https://android.googlesource.com/platform/frameworks/base/+/efd1c67

Specify the copied image file with drawableRight of EditText.


    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/numberpicker_down_normal_holo_light"/>

reference

EditText with a Popup List

Recommended Posts

[Android] Display of input candidates using List Popup Window
Enumeration of combination patterns using List
[Android] Implementation of side-scrolling ListView using RecyclerView
Acquisition of input contents using Scanner (Java)
Ability to display a list of products
[Rails] Display of multi-layered data using select boxes
(Android) Try to display static text using DataBinding
How to display the result of form input
Story of test automation using Appium [Android / java]