3

I have implemented Custom Spinner using Button following wildnove's answer. Everything works fine, but I am not able to display the highlighted radio button for the selected button.

Below is the code.

((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // How to highlight Radio button of a selected Item???

            final String[] items = view.getResources().getStringArray(R.array.planets__entries);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
            new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ((Button) findViewById(R.id.btnSpinnerPlanets)).setText(items[which]);
                    dialog.dismiss();
                }
            }).create().show();
        }
    });     

Can somebody help me how to highlight selected Item's Radio button ...

Community
  • 1
  • 1
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90

2 Answers2

0

The problem with this code is that you are creating the Spinner each time the Button is clicked. Try the following code:

    @Override
        protected Dialog onCreateDialog(int id) {
            Dialog dialog;
            AlertDialog.Builder builder;
            switch(id) {
            case 1:
                Button b=((Button) findViewById(R.id.btnSpinnerPlanets));
                builder = new AlertDialog.Builder(MyFormActivity.this).setTitle("the prompt").setAdapter(get_the_adapter(b), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        b.setText(b.getResources().getStringArray(R.array.planets__entries)[which]);
                        dismissDialog(1);
                    }
                })
                dialog = builder.create();
                break;
            default:
                dialog = null;
            }
            return dialog;

        }
    }

    public ArrayAdapter<String> get_the_Adapter(Button view){
    String[] items = view.getResources().getStringArray(R.array.planets__entries);
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyFormActivity.this, android.R.layout.simple_spinner_dropdown_item, items);
                return adapter;
    }

And for the Button's onClick():

((Button) findViewById(R.id.btnSpinnerPlanets)).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           showDialog(1);

        }
    });    
Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47
0

Unfortunately this behavior is not natively implemented in Spinner component, however, you can always create your own BaseAdapter to show whatever you need weather is in the spinner it self or in the dropdown like this:

private class ExampleAdapter extends BaseAdapter{

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
                    //Here is where you actually get the chance to return whatever you want in the spinner component (the single bar with the arrow)
        return yourCommonView;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
              //Here is where you get the chance to return whatever you want in the dropdown menu so here you should validate what's the currently selected element and return an image accordingly...
        return yourSelectedView;
    }

}

The important method here is, getDropDownView that is the one that gives you the chance to return an element with a checked CheckBox, or any mark you want to use, of course you have to create your own layout and validate if the element currently created need to be marked or not...

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54