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.garlicg.screenrecord4cm.SettingsActivity.java

/**
 * Create video size setting view/*from  w ww.  j a  v  a  2s.c  o  m*/
 */
private void createVideoSize(Bundle savedInstanceState) {
    int vp = mPrefs.getVideoPercentage();
    final VideoPercentage[] spinnerItems = AppPrefs.videoPercentages();
    int spinnerSelection = AppPrefs.findVideoPercentagePosition(spinnerItems, vp);

    ArrayAdapter<VideoPercentage> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item,
            spinnerItems);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    final Spinner spinner = ViewFinder.byId(this, R.id.videoPercentageSpinner);
    spinner.setAdapter(adapter);

    spinner.setSelection(spinnerSelection);
    spinner.post(new Runnable() {
        @Override
        public void run() {
            // setSelection later
            spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (view == null)
                        return;

                    VideoPercentage item = (VideoPercentage) parent.getItemAtPosition(position);
                    new AppPrefs(view.getContext()).saveVideoPercentage(item);
                }

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

From source file:com.sentaroh.android.TaskAutomation.Config.ProfileMaintenanceTimeProfile.java

final static private void setSpinnerMin(final GlobalParameters mGlblParms, Dialog dialog, Spinner spinner,
        CustomSpinnerAdapter adapter, String selected) {
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setPrompt(mGlblParms.context.getString(R.string.msgs_edit_profile_time_select_min));
    spinner.setAdapter(adapter);/*www .j a va2 s.  c o m*/
    adapter.clear();

    for (int i = 0; i <= 59; i++)
        if (i > 9)
            adapter.add("" + i);
        else
            adapter.add("0" + i);
    String hh = selected.substring(0, selected.indexOf(":"));
    String mm = selected.replace(hh + ":", "");
    if (!mm.equals("**")) {
        int min = Integer.parseInt(mm);
        spinner.setSelection(min);
    } else
        spinner.setSelection(0);
}

From source file:at.bitfire.davdroid.ui.CreateCalendarActivity.java

@Override
public void onLoadFinished(Loader<AccountInfo> loader, AccountInfo info) {
    Spinner spinner = (Spinner) findViewById(R.id.time_zone);
    String[] timeZones = TimeZone.getAvailableIDs();
    spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, timeZones));
    // select system time zone
    String defaultTimeZone = TimeZone.getDefault().getID();
    for (int i = 0; i < timeZones.length; i++)
        if (timeZones[i].equals(defaultTimeZone)) {
            spinner.setSelection(i);
            break;
        }//from  w  ww.  j  ava  2s . c o  m

    if (info != null) {
        spinner = (Spinner) findViewById(R.id.home_sets);
        spinner.setAdapter(
                new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, info.homeSets));
    }
}

From source file:li.klass.fhem.fragments.TimerDetailFragment.java

private void selectTargetState(String targetState) {
    if (targetDevice == null || getView() == null) {
        return;/*from  w  ww. jav a 2 s . c o m*/
    }
    this.targetState = targetState;

    Spinner targetStateSpinner = getTargetStateSpinner(getView());
    if (targetState == null) {
        targetStateSpinner.setSelection(0);
        return;
    }

    List<String> targetStates = targetDevice.getSetList().getSortedKeys();
    for (int i = 0; i < targetStates.size(); i++) {
        String availableTargetState = targetStates.get(i);
        if (availableTargetState.equals(targetState)) {
            targetStateSpinner.setSelection(i);
            break;
        }
    }
}

From source file:com.bodeme.easycloud.syncadapter.EnterCredentialsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.enter_credentials, container, false);

    textUrl = (TextView) v.findViewById(R.id.text_url);

    // protocol selection spinner
    textHttpWarning = (TextView) v.findViewById(R.id.http_warning);

    editURL = (EditText) v.findViewById(R.id.url);
    editURL.addTextChangedListener(this);

    editUserName = (EditText) v.findViewById(R.id.userName);
    editUserName.addTextChangedListener(this);

    editPassword = (EditText) v.findViewById(R.id.password);
    editPassword.addTextChangedListener(this);

    // Remove views for editing ownCloud-url, when constant is given
    if (Constants.OWNCLOUD_URL != null) {
        textUrl.setVisibility(View.GONE);
        editURL.setVisibility(View.GONE);
        textHttpWarning.setVisibility(View.GONE);
    }//from   w  w w .j  a  va2 s . c  o m

    // ownCloud Type
    Spinner spnrType = (Spinner) v.findViewById(R.id.select_type);
    spnrType.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            typePosition = position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            typePosition = 0;
        }
    });
    spnrType.setSelection(0);

    // hook into action bar
    setHasOptionsMenu(true);

    return v;
}

From source file:de.unipassau.isl.evs.ssh.app.activity.EditUserDeviceFragment.java

/**
 * Creates and shows a dialogs that gives the user the option to edit a group.
 *///from   w ww . j  a v a  2 s .  com
private void showEditUserDeviceDialog(Bundle bundle) {
    final UserDevice userDevice = (UserDevice) bundle.getSerializable(EDIT_USERDEVICE_DIALOG);
    if (userDevice == null) {
        Log.i(TAG, "No device found.");
        return;
    }

    final String[] groupNames = bundle.getStringArray(ALL_GROUPS_DIALOG);
    if (groupNames == null) {
        Log.i(TAG, "Empty bundle");
        return;
    }

    final AppUserConfigurationHandler handler = getComponent(AppUserConfigurationHandler.KEY);
    if (handler == null) {
        Log.i(TAG, "Container not yet connected!");
        return;
    }

    final LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.dialog_edituserdevice, null);

    final TextView title = (TextView) dialogView.findViewById(R.id.editdevicedialog_title);
    title.setText(String.format(getResources().getString(R.string.edit_user_device), userDevice.getName()));

    final EditText userDeviceName = (EditText) dialogView.findViewById(R.id.editdevicedialog_username);
    userDeviceName.setText(userDevice.getName());

    final ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
            android.R.layout.simple_spinner_dropdown_item, groupNames);
    final Spinner groupName = ((Spinner) dialogView.findViewById(R.id.editdevicedialog_spinner));
    groupName.setAdapter(adapter);
    groupName.setSelection(adapter.getPosition(userDevice.getInGroup()));

    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    final Resources res = getResources();
    final AlertDialog editDialog = builder.setView(dialogView).setNegativeButton(R.string.cancel, null)
            .setPositiveButton(R.string.edit, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    final AppMainActivity activity = (AppMainActivity) getActivity();
                    if (activity.hasPermission(Permission.CHANGE_USER_NAME)
                            && activity.hasPermission(Permission.CHANGE_USER_GROUP)) {
                        String name = userDeviceName.getText().toString();
                        String group = ((String) groupName.getSelectedItem());
                        DeviceID userDeviceID = userDevice.getUserDeviceID();

                        // no permission check as method only gets called when user can edit user name / user group
                        handler.setUserName(userDeviceID, name);
                        handler.setUserGroup(userDeviceID, group);
                    } else {
                        showToast(R.string.you_can_not_edit_user_devices);
                    }
                }
            }).setNeutralButton(R.string.remove, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (((AppMainActivity) getActivity()).hasPermission(Permission.DELETE_USER)) {
                        handler.removeUserDevice(userDevice.getUserDeviceID());
                        String toastText = String.format(res.getString(R.string.device_removed),
                                userDevice.getName());
                        Toast.makeText(getActivity(), toastText, Toast.LENGTH_SHORT).show();
                    } else {
                        showToast(R.string.you_can_not_remove_users);
                    }
                }
            }).create();

    // open the soft keyboard when dialog gets focused
    userDeviceName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                editDialog.getWindow()
                        .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        }
    });

    editDialog.show();
    userDeviceName.requestFocus();
}

From source file:com.andrada.sitracker.ui.SearchActivity.java

private void populateSearchVariants() {
    Spinner searchOptionSpinner = (Spinner) findViewById(R.id.search_option_spinner);
    if (searchOptionSpinner != null) {
        final SearchSpinnerAdapter adapter = new SearchSpinnerAdapter();
        adapter.addItem(getString(R.string.search_type_name));
        adapter.addItem(getString(R.string.search_type_keyword));
        searchOptionSpinner.setAdapter(adapter);
        searchOptionSpinner.setSelection(mCurrentSearchType);
        searchOptionSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override//from www  .j  ava2  s. c om
            public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
                if (position >= 0 && position < adapter.getCount()) {
                    onSearchTypeSelected(position);
                }
            }

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

From source file:me.johnmh.boogdroid.ui.BugStatusFragment.java

@NonNull
private Spinner setupStatusSpinner(View view) {
    Spinner spinnerStatus = (Spinner) view.findViewById(R.id.status);
    String statusName = bug.getStatus();
    Server server = bug.getProduct().getServer();
    StatusInfo statusInfo = server.getStatusChanges().get(statusName);
    ArrayAdapter<ChangeStatusInfo> adapter = new ArrayAdapter<>(getActivity().getBaseContext(),
            android.R.layout.simple_spinner_item, statusInfo.getChangeList());
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerStatus.setAdapter(adapter);/*from   www .j av  a  2s.c o m*/
    spinnerStatus.setSelection(adapter.getPosition(server.findChangeStatusInfo(statusName)));
    return spinnerStatus;
}

From source file:com.sentaroh.android.TaskAutomation.Config.ProfileMaintenanceTimeProfile.java

final static private void setSpinnerMonth(final GlobalParameters mGlblParms, Dialog dialog, Spinner spinner,
        CustomSpinnerAdapter adapter, String selected) {
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setPrompt(mGlblParms.context.getString(R.string.msgs_edit_profile_time_select_month));
    spinner.setAdapter(adapter);// ww  w . jav a2 s .  c o m
    adapter.clear();

    for (int i = 1; i <= 12; i++)
        if (i > 9)
            adapter.add("" + i);
        else
            adapter.add("0" + i);
    String yyyy = selected.substring(0, selected.indexOf("/"));
    String mm = selected.replace(yyyy + "/", "").substring(0, 2);
    if (!mm.equals("**")) {
        int month = Integer.parseInt(mm);
        spinner.setSelection(month - 1);
    } else
        spinner.setSelection(0);
}

From source file:com.asksven.betterbatterystats.RawStatsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.raw_stats);//from w  w  w.  j av  a 2 s . c o m

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(getString(R.string.label_raw_stats));

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayUseLogoEnabled(false);

    swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh);

    swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            doRefresh();
        }
    });

    // Spinner for selecting the stat
    Spinner spinnerStat = (Spinner) findViewById(R.id.spinnerStat);

    ArrayAdapter spinnerStatAdapter = ArrayAdapter.createFromResource(this, R.array.stats,
            R.layout.bbs_spinner_layout);
    spinnerStatAdapter.setDropDownViewResource(R.layout.bbs_spinner_dropdown_item);

    spinnerStat.setAdapter(spinnerStatAdapter);
    // setSelection MUST be called after setAdapter
    spinnerStat.setSelection(m_iStat);
    spinnerStat.setOnItemSelectedListener(this);

    TextView tvSince = (TextView) findViewById(R.id.TextViewSince);

    long sinceMs = SystemClock.elapsedRealtime();

    if (sinceMs != -1) {
        String sinceText = DateUtils.formatDuration(sinceMs);

        tvSince.setText(sinceText);
        Log.i(TAG, "Since " + sinceText);
    } else {
        tvSince.setText("n/a ");
        Log.i(TAG, "Since: n/a ");

    }

}