Example usage for android.widget Spinner setSelection

List of usage examples for android.widget Spinner setSelection

Introduction

In this page you can find the example usage for android.widget Spinner setSelection.

Prototype

@Override
    public void setSelection(int position) 

Source Link

Usage

From source file:com.redwoodsystems.android.apps.LightingListFragment.java

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    Log.d(TAG, "LightingListFragment.onResume called");

    //query settings
    Cursor settingsCursor = mSettingsDbHelper.fetchAllSettings();
    settings = mSettingsDbHelper.getSettings(settingsCursor);

    //Spinner//from  w  w w  . j  a va2 s . c om
    setupLocationSpinner();

    //Set Spinner to stored value if present
    //check if stored value still exists. It could have been deleted
    int storedLocationId = settings.getLastLocationId();
    if (storedLocationId > -1) {
        Spinner spinner = (Spinner) getActivity().findViewById(R.id.locationSpinner);
        int pos = -1;
        for (int i = 0; i < spinner.getCount(); i++) {
            Cursor c = (Cursor) spinner.getItemAtPosition(i);
            int locId = mLocationDbHelper.getLocationId(c);
            if (locId == storedLocationId) {
                pos = i;
                Log.d(TAG, "setting spinner to stored value = " + Integer.toString(storedLocationId));
                spinner.setSelection(i);
                mCurrentLocation = mLocationDbHelper.getLocation(c);
            }
        }
    }

}

From source file:org.akvo.caddisfly.ui.activity.MainActivity.java

private void setupActionBarSpinner() {
    ActionBar ab = getActionBar();/*  w  w  w . j  a v a 2 s  .c om*/

    ArrayList<TestInfo> tests = null;
    try {
        tests = JsonUtils.loadTests(FileUtils.readRawTextFile(this, R.raw.tests_json));
    } catch (JSONException e) {
        e.printStackTrace();
    }

    mTopLevelSpinnerAdapter.clear();
    MainApp mainApp = (MainApp) getApplicationContext();

    int selectedIndex = 0;
    int index = 0;
    assert tests != null;
    for (TestInfo test : tests) {
        mTopLevelSpinnerAdapter.addItem(test.getCode(), test.getName());
        if (test.getCode().equalsIgnoreCase(mainApp.currentTestInfo.getCode())) {
            selectedIndex = index;
        }
        index++;
    }

    @SuppressLint("InflateParams")
    View spinnerContainer = LayoutInflater.from(this).inflate(R.layout.actionbar_spinner, null);
    ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    assert ab != null;
    ab.setCustomView(spinnerContainer, lp);

    Spinner spinner = (Spinner) spinnerContainer.findViewById(R.id.actionbar_spinner);
    spinner.setAdapter(mTopLevelSpinnerAdapter);

    spinner.setSelection(selectedIndex);

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> spinner, View view, int position, long itemId) {
            MainApp mainApp = (MainApp) getApplicationContext();
            String testType = mTopLevelSpinnerAdapter.getTag(position);
            mainApp.setSwatches(testType);
            mCalibrateFragment.dataChanged();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {
        }
    });
    updateActionBarNavigation(getCurrentFragmentIndex());
}

From source file:com.bangalore.barcamp.activity.ShareActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.share_screen);
    mDrawerToggle = BCBFragmentUtils.setupActionBar(this, "Share");

    // BCBUtils.createActionBarOnActivity(this);
    // BCBUtils.addNavigationActions(this);
    ((EditText) findViewById(R.id.editText1)).addTextChangedListener(new TextWatcher() {

        @Override/*from   www . j  a  v  a 2  s .c o  m*/
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            ((TextView) findViewById(R.id.charsLeftTextView)).setText("Chars left: " + (140 - s.length() - 7));
        }
    });
    if (getIntent().hasExtra(SHARE_STRING)) {
        ((EditText) findViewById(R.id.editText1)).setText(getIntent().getStringExtra(SHARE_STRING));
    }
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    final PackageManager pm = getPackageManager();
    final Spinner spinner = (Spinner) findViewById(R.id.shareTypeSpinner);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(this,
            android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    final List<ResolveInfo> matches = pm.queryIntentActivities(intent, 0);
    String selectedItem = BCBSharedPrefUtils.getShareSettings(getApplicationContext());
    int selectedPos = -1;
    for (ResolveInfo info : matches) {
        adapter.add(info.loadLabel(pm));
        if (selectedItem.equals(info.loadLabel(pm))) {
            selectedPos = matches.indexOf(info);
        }
    }
    spinner.setAdapter(adapter);

    if (selectedPos != -1) {
        spinner.setSelected(true);
        spinner.setSelection(selectedPos);
    }
    ((TextView) findViewById(R.id.charsLeftTextView)).setText("Chars left: 140");
    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            int appSelectedPos = spinner.getSelectedItemPosition();
            ResolveInfo info = matches.get(appSelectedPos);
            intent.setClassName(info.activityInfo.packageName, info.activityInfo.name);

            BCBSharedPrefUtils.setShareSettings(getApplicationContext(), (String) info.loadLabel(pm));
            intent.putExtra(Intent.EXTRA_TEXT,
                    ((EditText) findViewById(R.id.editText1)).getText().toString() + " #barcampblr");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);
            finish();

        }
    });

    BCBFragmentUtils.addNavigationActions(this);
    supportInvalidateOptionsMenu();
    Tracker t = ((BarcampBangalore) getApplication()).getTracker();

    // Set screen name.
    t.setScreenName(this.getClass().getName());

    // Send a screen view.
    t.send(new HitBuilders.AppViewBuilder().build());

}

From source file:com.cmput301w17t07.moody.EditMoodActivity.java

/**
 * display the attributes of the mood that was selected to view <br>
 * Spinner dropdown logic taken from <br>
 * link: http://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-list <br>
 * Author: Nicolas Tyler, 2013/07/15 8:47 <br>
 * taken by Xin Huang 2017-03-04 15:30 (used and swith function written by Nick 2017/03/12 14:30) <br>
 *///  w w w  . j  av a  2s  . co  m
private void displayAttributes() {
    final ImageButton deleteLocation = (ImageButton) findViewById(R.id.deleteLocation);
    if (address == null) {
        deleteLocation.setVisibility(View.INVISIBLE);
        deleteLocation.setEnabled(false);
    }

    Spinner dropdown = (Spinner) findViewById(R.id.editEmotion);

    String[] items = new String[] { "anger", "confusion", "disgust", "fear", "happiness", "sadness", "shame",
            "surprise" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
            items);
    dropdown.setAdapter(adapter);

    switch (editMood.getFeeling()) {
    case "anger":
        dropdown.setSelection(0);
        break;
    case "confusion":
        dropdown.setSelection(1);
        break;
    case "disgust":
        dropdown.setSelection(2);
        break;
    case "fear":
        dropdown.setSelection(3);
        break;
    case "happiness":
        dropdown.setSelection(4);
        break;
    case "sadness":
        dropdown.setSelection(5);
        break;
    case "shame":
        dropdown.setSelection(6);
        break;
    case "surprise":
        dropdown.setSelection(7);
        break;

    }
    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            EmotionText = parent.getItemAtPosition(position).toString();
            editMood.setFeeling(EmotionText);
            TextView sizeView = (TextView) findViewById(R.id.editSocialText);
            sizeView.setText("Feeling " + EmotionText + " " + SocialSituation);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(EditMoodActivity.this, "Please pick a feeling!", Toast.LENGTH_SHORT).show();
        }
    });

    Spinner dropdown_SocialSituation = (Spinner) findViewById(R.id.editSocialSituation);
    String[] item_SocialSituation = new String[] { "", "alone", "with one other person", "with two people",
            "with several people", "with a crowd" };
    ArrayAdapter<String> adapter_SocialSituation = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, item_SocialSituation);
    dropdown_SocialSituation.setAdapter(adapter_SocialSituation);
    switch (editMood.getSocialSituation()) {
    case "alone":
        dropdown_SocialSituation.setSelection(1);
        break;
    case "with one other person":
        dropdown_SocialSituation.setSelection(2);
        break;
    case "with two people":
        dropdown_SocialSituation.setSelection(3);
        break;
    case "with several people":
        dropdown_SocialSituation.setSelection(4);
        break;
    case "with a crowd":
        dropdown_SocialSituation.setSelection(5);
        break;

    }

    dropdown_SocialSituation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            SocialSituation = parent.getItemAtPosition(position).toString();
            editMood.setSocialSituation(SocialSituation);
            TextView sizeView = (TextView) findViewById(R.id.editSocialText);
            sizeView.setText("Feeling " + EmotionText + " " + SocialSituation);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    Description = (EditText) findViewById(R.id.editDescription);
    Description.setText(editMood.getMoodMessage());
    editBitmapImage = editMood.decodeImage();
    if (editBitmapImage != null) {
        image.setImageBitmap(editBitmapImage);
    } else {
        image.setImageBitmap(bitmapImage);
    }
}

From source file:de.nico.ha_manager.activities.AddHomework.java

private void handleIntent(Intent intent) {
    Bundle extras = intent.getExtras();/* w  w w.  j  a v  a2s . c  o  m*/
    if (extras != null) {
        // Set ID
        ID = extras.getString(Source.allColumns[0]);

        // Set Urgent
        if (!extras.getString(Source.allColumns[1]).equals("")) {
            CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox_urgent);
            checkBox.setChecked(true);
        }

        // Set Subject
        String subject = extras.getString(Source.allColumns[2]);
        Spinner subSpin = (Spinner) findViewById(R.id.spinner_subject);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, subjects);
        // Get position in subject list
        int spinnerPostion = adapter.getPosition(subject);
        // If subject is not in subject list
        if (spinnerPostion == -1) {
            int size = subjects.length;
            String[] tmp = new String[size + 1];
            System.arraycopy(subjects, 0, tmp, 0, size);
            tmp[size] = subject;
            Arrays.sort(tmp);

            subjects = tmp;
            setSpinner();
            adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, subjects);
            spinnerPostion = adapter.getPosition(subject);
        }

        subSpin.setSelection(spinnerPostion);

        // Set Homework
        EditText hwEdit = (EditText) findViewById(R.id.editText_homework);
        hwEdit.setText(extras.getString(Source.allColumns[3]));

        // Set Until
        Button untilButton = (Button) findViewById(R.id.button_until);
        untilButton.setText(extras.getString(Source.allColumns[4]));
        until = extras.getString(Source.allColumns[4]);
    }
}

From source file:com.cmput301w17t07.moody.CreateMoodActivity.java

/**
 * displayAttributes method used when the user gets back from the "Map" display
 * and displays the changed spinner attributes.
 * <br>//from   www  .j  a  v  a 2s.  com
 * Spinner dropdown logic taken from <br>
 * link: http://stackoverflow.com/questions/13377361/how-to-create-a-drop-down-listm <br>
 * Author: Nicolas Tyler, 2013/07/15 8:47 <br>
 * taken by Xin Huang 2017-03-04 15:30 (used and switch function written by Nick 2017/03/12 14:30) <br>
 */
private void displayAttributes() {
    Spinner dropdown = (Spinner) findViewById(R.id.Emotion);
    String[] items = new String[] { "anger", "confusion", "disgust", "fear", "happiness", "sadness", "shame",
            "surprise" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,
            items);
    dropdown.setAdapter(adapter);

    switch (tempMood.getFeeling()) {
    case "anger":
        dropdown.setSelection(0);
        break;
    case "confusion":
        dropdown.setSelection(1);
        break;
    case "disgust":
        dropdown.setSelection(2);
        break;
    case "fear":
        dropdown.setSelection(3);
        break;
    case "happiness":
        dropdown.setSelection(4);
        break;
    case "sadness":
        dropdown.setSelection(5);
        break;
    case "shame":
        dropdown.setSelection(6);
        break;
    case "surprise":
        dropdown.setSelection(7);
        break;

    }
    dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            EmotionText = parent.getItemAtPosition(position).toString();
            tempMood.setFeeling(EmotionText);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(CreateMoodActivity.this, "Please pick a feeling!", Toast.LENGTH_SHORT).show();
        }
    });

    Spinner dropdown_SocialSituation = (Spinner) findViewById(R.id.SocialSituation);
    String[] item_SocialSituation = new String[] { "", "alone", "with one other person", "with two people",
            "with several people", "with a crowd" };
    ArrayAdapter<String> adapter_SocialSituation = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, item_SocialSituation);
    dropdown_SocialSituation.setAdapter(adapter_SocialSituation);
    switch (tempMood.getSocialSituation()) {
    case "alone":
        dropdown_SocialSituation.setSelection(1);
        break;
    case "with one other person":
        dropdown_SocialSituation.setSelection(2);
        break;
    case "with two people":
        dropdown_SocialSituation.setSelection(3);
        break;
    case "with several people":
        dropdown_SocialSituation.setSelection(4);
        break;
    case "with a crowd":
        dropdown_SocialSituation.setSelection(5);
        break;

    }

    dropdown_SocialSituation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            SocialSituation = parent.getItemAtPosition(position).toString();
            tempMood.setSocialSituation(SocialSituation);
            TextView sizeView = (TextView) findViewById(R.id.SocialText);
            sizeView.setText("  " + SocialSituation);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            Toast.makeText(CreateMoodActivity.this, "Please pick a feeling!", Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:com.z299studio.pb.DeleteCategory.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView;//ww w.  j  a  v  a 2  s  . co  m
    if (Application.getInstance() == null || Application.getInstance().getAccountManager() == null) {
        return null;
    }
    if (savedInstanceState != null) {
        mPosition = savedInstanceState.getInt("category");
    }
    rootView = inflater.inflate(R.layout.dialog_delete_category, container);
    Button button = (Button) rootView.findViewById(R.id.ok);
    button.setOnClickListener(this);
    button = (Button) rootView.findViewById(R.id.cancel);
    button.setOnClickListener(this);
    Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
    String[] allNames = Application.getInstance().getSortedCategoryNames();
    String[] deletableNames = new String[allNames.length - 1];
    int i, j = 0;
    for (i = 1; i < allNames.length; ++i) {
        deletableNames[j++] = allNames[i];
    }
    if (mPosition < 0) {
        int[] allIds = Application.getInstance().getSortedCategoryIds();
        for (i = 1; i < allIds.length; ++i) {
            if (mCategory == allIds[i]) {
                mPosition = i;
                break;
            }
        }
        mPosition -= 1;
        if (mPosition < 0) {
            mPosition = 0;
        }
    }
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_spinner_dropdown_item, deletableNames);
    spinner.setAdapter(spinnerAdapter);
    spinner.setSelection(mPosition);
    spinner.setOnItemSelectedListener(this);
    mCheckBox = (CheckBox) rootView.findViewById(R.id.checkbox);
    return rootView;
}

From source file:com.heneryh.aquanotes.ui.controllers.OutletsDataAdapter.java

/** {@inheritDoc} */
@Override// w  w  w. j  av  a 2 s . c  om
public void bindView(View view, Context context, Cursor cursor) {

    String outletName = cursor.getString(OutletDataViewQuery.NAME);
    ((TextView) view.findViewById(R.id.outlet_title)).setText(outletName);

    String deviceId = cursor.getString(OutletDataViewQuery.DEVICE_ID);
    ((TextView) view.findViewById(R.id.outlet_subtitle)).setText(deviceId);

    Spinner spinner = (Spinner) view.findViewById(R.id.spin);
    spinner.setAdapter(adapter);

    Integer controllerId = cursor.getInt(OutletDataViewQuery.CONTROLLER_ID);

    spinner.setOnItemSelectedListener(new myOnItemSelectedListener(outletName, controllerId));

    // Assign track color to visible block
    String val = cursor.getString(OutletDataViewQuery.VALUE);
    final ImageView iconView = (ImageView) view.findViewById(android.R.id.icon1);
    Resources res = mActivity.getResources();

    ////  Axx = Auto-On or Auto-Off
    ////  OFF = Manual Off
    ////  ON = Manual On

    if (val.equalsIgnoreCase("AON")) {
        iconView.setImageDrawable(res.getDrawable(R.drawable.on));
        spinner.setSelection(0);
    } else if (val.equalsIgnoreCase("AOF")) {
        iconView.setImageDrawable(res.getDrawable(R.drawable.off));
        spinner.setSelection(0);
    } else if (val.equalsIgnoreCase("OFF")) {
        iconView.setImageDrawable(res.getDrawable(R.drawable.off));
        spinner.setSelection(1);
    } else if (val.equalsIgnoreCase("ON")) {
        iconView.setImageDrawable(res.getDrawable(R.drawable.on));
        spinner.setSelection(2);
    } else {
        iconView.setImageDrawable(new ColorDrawable(Color.BLUE));
    }
}

From source file:org.elasticdroid.SshConnectorView.java

/**
 * Method called to populate the open ports spinner in the UI
 *//*from w w w. ja v a  2s.c o  m*/
private void populateSpinner() {
    if (openPorts == null) {
        return;
    }

    int selectedIndex = 0; //set selected index to index of port 22 if available.
    Spinner portSpinner = (Spinner) findViewById(R.id.sshConnectorPortSpinner);

    if (openPorts.contains("22")) {
        Log.d(TAG, "Found port 22 in openPorts.Setting as selected.");
        selectedIndex = openPorts.indexOf("22");
    }
    //create an ArrayAdapter<String> to hold this
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,
            openPorts);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    portSpinner.setAdapter(spinnerAdapter);
    portSpinner.setSelection(selectedIndex); //0 if port 22 unavailable, else indexof(port22)
}

From source file:org.rapidandroid.activity.FormCreator.java

/**
 * Gets called from onActivityResult// w  ww .ja  v a  2s. c o m
 * 
 * @param f
 */
private void restoreForm(Form f) {
    EditText etxFormName = (EditText) findViewById(R.id.etx_formname);
    etxFormName.setText(f.getFormName());

    EditText etxFormPrefix = (EditText) findViewById(R.id.etx_formprefix);
    etxFormPrefix.setText(f.getPrefix());

    EditText etxDescription = (EditText) findViewById(R.id.etx_description);
    etxDescription.setText(f.getDescription());

    Spinner spin_forms = (Spinner) findViewById(R.id.spinner_formparser);
    int cnt = spin_forms.getCount();
    for (int i = 0; i < cnt; i++) {
        if (spin_forms.getItemAtPosition(i).toString().toLowerCase(Locale.getDefault())
                .contains(f.getParserType().name().toLowerCase(Locale.getDefault())))
            spin_forms.setSelection(i);
    }

    // add fields
    Field[] fields = f.getFields();
    Arrays.sort(fields, new Comparator<Field>() {
        public int compare(Field f1, Field f2) {
            if (f1.getSequenceId() < f2.getSequenceId())
                return -1;
            else if (f1.getSequenceId() > f2.getSequenceId())
                return 1;
            return 0;
        }
    });

    for (Field fi : fields) {
        Bundle b = new Bundle();
        b.putString(ResultConstants.RESULT_KEY_FIELDNAME, fi.getName());
        b.putString(ResultConstants.RESULT_KEY_DESCRIPTION, fi.getDescription());
        b.putInt(ResultConstants.RESULT_KEY_FIELDTYPE_ID, ((SimpleFieldType) fi.getFieldType()).getId());

        addNewFieldFromActivity(b);
    }

    // save state so that the "current_form" is in the intent
    // and will therefore be loaded in the onResume call
    saveState();
}