Android- How to use Spinner in an AlertDialog?
Asked Answered
C

3

6

I have a spinner, how can i bind it to an AlertDialog? It is possible to do that?

Congeal answered 6/2, 2012 at 9:19 Comment(0)
T
21

please try this :

 public class WvActivity extends Activity {

TextView tx;
String[] s = { "India ", "Arica", "India ", "Arica", "India ", "Arica",
        "India ", "Arica", "India ", "Arica" };
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final ArrayAdapter<String> adp = new ArrayAdapter<String>(WvActivity.this,
            android.R.layout.simple_spinner_item, s);

    tx= (TextView)findViewById(R.id.txt1);
    final Spinner sp = new Spinner(WvActivity.this);
    sp.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    sp.setAdapter(adp);

    AlertDialog.Builder builder = new AlertDialog.Builder(WvActivity.this);
    builder.setView(sp);
    builder.create().show();
  }
 }
Tuckerbag answered 6/2, 2012 at 9:28 Comment(3)
can I ask you a question: where is the LayoutParams.Wrap_content from? I mean which package should I importCongeal
import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams;Tuckerbag
Thanks, I got it. In fact, how can I get the chosen result in this spinner after I clicked to one row? What method should I use? Thanks in advance.Congeal
M
0

Instead why don't you use an activity and set it to Dialog style. Should be like the same visual approach and you can design your dialog just as you want it.

Marplot answered 6/2, 2012 at 9:22 Comment(0)
C
0

public void CreateAlertDialog1() {

    LayoutInflater inflater = getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.note_dialog, null);
    final EditText etName = (EditText) alertLayout.findViewById(R.id.enter_name);
    final TextView call_date = alertLayout.findViewById(R.id.call_date);
    call_date.setVisibility(View.GONE);
    date_time_display = alertLayout.findViewById(R.id.date_time_display);
     date_time_select = alertLayout.findViewById(R.id.date_time_select);
    final Spinner spSex = (Spinner) alertLayout.findViewById(R.id.spinner);
    String[] status_note = new String[] {
            "Laceflower",
            "Sugar maple",
            "Mountain mahogany",
            "Butterfly weed"
    };

    final List<String> plantsList = new ArrayList<>(Arrays.asList(status_note));

    // Initializing an ArrayAdapter
    @SuppressLint({"NewApi", "LocalSuppress"}) final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
            Objects.requireNonNull(getActivity()),R.layout.spinner_item,plantsList);

    spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
    spSex.setAdapter(spinnerArrayAdapter);



    date_time_select.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");

        }
    });

    AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
    alert.setTitle(client_name_display);
    alert.setIcon(getActivity().getResources().getDrawable(R.drawable.addnote));
    alert.setView(alertLayout);
    alert.setCancelable(false);
    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getActivity().getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();

            OkHttpHandler okHttpHandler = new OkHttpHandler(status, res, start_date_time, end_date_time, etName.getText().toString());
            okHttpHandler.execute("https://management.reflectdm.com/api/call-management");

        }
    });

    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            String user = etName.getText().toString();
            String sex = String.valueOf(spSex.getSelectedItem());
            //Toast.makeText(getBaseContext(), "Name: " + user + "\nBirthday: " + bday + "\nSex: " + sex + "\nHungry: " + hungry, Toast.LENGTH_SHORT).show();

            OkHttpHandler okHttpHandler = new OkHttpHandler(status, res, start_date_time, end_date_time, etName.getText().toString());
            okHttpHandler.execute("https://management.reflectdm.com/api/call-management");


        }
    });
    AlertDialog dialog = alert.create();
    dialog.show();
}
Circumfluous answered 18/3, 2020 at 4:35 Comment(1)
Welcome to stackoverflow! As a courtesy, it is required that whenever you post an answer, you give an explanation. Also, should you post code snippets, kindly clean them with proper documentation.Bucktooth

© 2022 - 2024 — McMap. All rights reserved.