Home > Mobile >  How to implement a OnItemSelectedListener for Spinner in Dialog Box
How to implement a OnItemSelectedListener for Spinner in Dialog Box

Time:04-05

I have a Dialog Box in my MainActivity that is activated by touching a cog button in my Layout. When it appears, it contains a drop down Spinner. The Spinner works and produces a scrollable list of items, so that is good at least.

Albeit, I am stuck on correctly applying an OnSpinnerItemSelectedListener that works, which I want to customise in order to provide responses for each item selected.

Is anybody able to help me by providing advice on how best to apply a Listener and get it to work please? Your help would be massively appreciated!

With the code below, I have only included the portion of my MainActivity that references the Dialog Box and Spinner inside of that box. If you require further code, then please let me know - I'm more than happy to provide more info! PowerSpinnerView references the type of Spinner I'm using (from a third party dependency implemented in my Gradle file).

options = findViewById(R.id.optionscog);

options.setOnClickListener(new View.OnClickListener() {

    private PowerSpinnerView spItems;

    ArrayAdapter arrayAdapter;

    @Override
    public void onClick(View view) {

        final Dialog dialog = new Dialog(MainActivity.this);

        dialog.setContentView(R.layout.activity_options_menu);

        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(true);

        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(dialog.getWindow().getAttributes());

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;

        View back = dialog.findViewById(R.id.arrow);

        PowerSpinnerView powerSpinnerView = (PowerSpinnerView) 
        dialog.findViewById(R.id.spItems);

        ArrayAdapter<String> adapter =
                new ArrayAdapter<String>(MainActivity.this,
                        android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        back.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

        Bitmap map = takeScreenShot(MainActivity.this);

        Bitmap fast = fastblur(map, 50);

        final Drawable draw=new BitmapDrawable(getResources(),fast);
        dialog.getWindow().setBackgroundDrawable(draw);

        dialog.getWindow().setAttributes(lp);

        dialog.show();

    }

});

CodePudding user response:

You use PowerSpinnerView from a 3rd party, so I looked at its documentation for the click listener:

powerSpinnerView.setOnSpinnerItemSelectedListener(new OnSpinnerItemSelectedListener<String>() {
 @Override public void onItemSelected(int oldIndex, @Nullable String oldItem, int newIndex, String newItem) {
   // do whatever you need here 
 }
});

the item you select is newItem and its index is new index

  • Related