Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get value from spinner in android ?

How to get values from drop down menu and use them in body of new message to be send.

Following is my code,

 public void onItemSelected(AdapterView<?> parent, View view, int position,
            long id) {
        // On selecting a spinner item
        String label = parent.getItemAtPosition(position).toString();
            // Showing selected spinner item
        Toast.makeText(parent.getContext(), "You selected: " + label,
                Toast.LENGTH_LONG).show();
        }
String phoneNo = editPhoneNum.getText().toString();
             String sms = editSMS.getText().toString();
             try {
                     SmsManager smsManager =    SmsManager.getDefault();
                     smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                     Toast.makeText(getApplicationContext(), "SMS Sent!",Toast.LENGTH_LONG).show();


             }
like image 674
Android beginner Avatar asked Jan 08 '14 06:01

Android beginner


People also ask

How do I get the value from spinner in Kotlin?

This example demonstrates how to get Spinner value in Kotlin. Step 1 − Create a new project in Android Studio, go to File ⇉ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Which function is used to get the selected item from the spinner?

getItemAtPosition(i).


1 Answers

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // On selecting a spinner item
            String label = parent.getItemAtPosition(position).toString();
                // Showing selected spinner item
            Toast.makeText(parent.getContext(), "You selected: " + label,Toast.LENGTH_LONG).show();

                }
}
like image 122
learner Avatar answered Sep 25 '22 18:09

learner