Example usage for android.preference CheckBoxPreference setIcon

List of usage examples for android.preference CheckBoxPreference setIcon

Introduction

In this page you can find the example usage for android.preference CheckBoxPreference setIcon.

Prototype

public void setIcon(Drawable icon) 

Source Link

Document

Sets the icon for this Preference with a Drawable.

Usage

From source file:com.guipenedo.pokeradar.activities.settings.PokemonFilterSettingsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    getDelegate().installViewFactory();/*ww w .  j a va2s.c  om*/
    getDelegate().onCreate(savedInstanceState);
    super.onCreate(savedInstanceState);
    PreferenceScreen screen = getPreferenceManager().createPreferenceScreen(this);
    PreferenceCategory category = new PreferenceCategory(this);
    category.setTitle(R.string.filter_pokemons);
    screen.addPreference(category);
    try {
        JSONArray pokemonList = new JSONArray(Utils.loadJSONFromFile(this, "pokemon.json"));
        for (int i = 0; i < pokemonList.length(); i++) {
            JSONObject pokemon = pokemonList.getJSONObject(i);
            CheckBoxPreference checkBox = new CheckBoxPreference(this);
            checkBox.setTitle(pokemon.getString("Name"));
            checkBox.setIcon(new BitmapDrawable(getResources(),
                    Utils.bitmapForPokemon(this, Integer.parseInt(pokemon.getString("Number")))));
            checkBox.setDefaultValue(true);
            checkBox.setSummary(String.format(getString(R.string.setting_filter_pokemon_summary),
                    pokemon.getString("Name")));
            checkBox.setKey("pref_key_show_pokemon_" + Integer.parseInt(pokemon.getString("Number")));
            category.addPreference(checkBox);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }

    setPreferenceScreen(screen);
}

From source file:com.harshad.linconnectclient.ApplicationSettingsActivity.java

private void setupSimplePreferencesScreen() {
    addPreferencesFromResource(R.xml.pref_application);

    applicationCategory = (PreferenceCategory) findPreference("header_application");

    // Listen for check/uncheck all tap
    findPreference("pref_all").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
        @Override//from   ww  w  . j  a v a2 s. co m
        public boolean onPreferenceChange(Preference arg0, Object arg1) {
            for (int i = 0; i < applicationCategory.getPreferenceCount(); i++) {
                // Uncheck or check all items
                ((CheckBoxPreference) (applicationCategory.getPreference(i))).setChecked((Boolean) arg1);
            }
            return true;
        }
    });

    class ApplicationTask extends AsyncTask<String, Void, List<ApplicationInfo>> {
        private PackageManager packageManager;

        @Override
        protected void onPreExecute() {
            progressDialog = ProgressDialog.show(ApplicationSettingsActivity.this, null, "Loading...", true);
        }

        @Override
        protected List<ApplicationInfo> doInBackground(String... notif) {

            packageManager = getApplicationContext().getPackageManager();

            // Comparator used to sort applications by name
            class CustomComparator implements Comparator<ApplicationInfo> {
                @Override
                public int compare(ApplicationInfo arg0, ApplicationInfo arg1) {
                    return arg0.loadLabel(packageManager).toString()
                            .compareTo(arg1.loadLabel(packageManager).toString());
                }
            }

            // Get installed applications
            Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            List<ApplicationInfo> appList = getApplicationContext().getPackageManager()
                    .getInstalledApplications(PackageManager.GET_META_DATA);

            // Sort by application name
            Collections.sort(appList, new CustomComparator());

            return appList;
        }

        @Override
        protected void onPostExecute(List<ApplicationInfo> result) {
            // Add each application to screen
            for (ApplicationInfo appInfo : result) {
                CheckBoxPreference c = new CheckBoxPreference(ApplicationSettingsActivity.this);
                c.setTitle(appInfo.loadLabel(packageManager).toString());
                c.setSummary(appInfo.packageName);
                c.setIcon(appInfo.loadIcon(packageManager));
                c.setKey(appInfo.packageName);
                c.setChecked(true);

                c.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
                    @Override
                    public boolean onPreferenceChange(Preference arg0, Object arg1) {
                        // On tap, show an enabled/disabled notification on the desktop
                        Object[] notif = new Object[3];

                        if (arg1.toString().equals("true")) {
                            notif[0] = arg0.getTitle().toString() + " notifications enabled";
                            notif[1] = "via LinConnect";
                            notif[2] = arg0.getIcon();
                        } else {
                            notif[0] = arg0.getTitle().toString() + " notifications disabled";
                            notif[1] = "via LinConnect";
                            notif[2] = arg0.getIcon();
                        }

                        new TestTask().execute(notif);

                        return true;
                    }

                });

                applicationCategory.addPreference(c);
            }
            progressDialog.dismiss();
        }
    }

    new ApplicationTask().execute();

}

From source file:com.mattprecious.notisync.preferences.DevicePreferenceFragment.java

@Override
public void onResume() {
    super.onResume();

    BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
    if (btAdapter == null) {
        return;//w w w . j a  v  a2  s .c  o  m
    }

    PreferenceScreen screen = getPreferenceScreen();
    screen.removeAll();

    Set<String> selectedDevices = Preferences.getDevices(getActivity());

    // Get a set of currently paired devices
    Set<BluetoothDevice> pairedDevices = btAdapter.getBondedDevices();
    for (BluetoothDevice device : pairedDevices) {
        CheckBoxPreference preference = new CheckBoxPreference(getActivity());
        preference.setOnPreferenceChangeListener(preferenceListener);
        preference.setTitle(device.getName());
        preference.setSummary(device.getAddress());

        int iconResId = Helpers.getBtClassDrawable(device);
        if (iconResId != 0) {
            preference.setIcon(iconResId);
        }

        if (selectedDevices.contains(device.getAddress())) {
            preference.setDefaultValue(true);
            localDeviceSet.add(device.getAddress());
        }

        getPreferenceScreen().addPreference(preference);
    }
}

From source file:de.tum.in.tumcampus.fragments.SettingsFragment.java

private void populateNewsSources() {
    PreferenceCategory news_sources = (PreferenceCategory) findPreference("card_news_sources");
    NewsManager cm = new NewsManager(mContext);
    Cursor cur = cm.getNewsSources();
    if (cur.moveToFirst()) {
        do {/*from w  w w .j ava  2 s .c o  m*/
            final CheckBoxPreference pref = new CheckBoxPreference(mContext);
            pref.setKey("card_news_source_" + cur.getString(0));
            pref.setDefaultValue(true);
            if (Build.VERSION.SDK_INT >= 11) {
                // Load news source icon in background and set it
                final String url = cur.getString(1);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        NetUtils net = new NetUtils(mContext);
                        final Bitmap bmp = net.downloadImageToBitmap(url);
                        mContext.runOnUiThread(new Runnable() {
                            @TargetApi(11)
                            @Override
                            public void run() {
                                pref.setIcon(new BitmapDrawable(getResources(), bmp));
                            }
                        });
                    }
                }).start();
            }
            pref.setTitle(cur.getString(2));
            news_sources.addPreference(pref);
        } while (cur.moveToNext());
    }
    cur.close();
}