Example usage for android.preference PreferenceScreen addPreference

List of usage examples for android.preference PreferenceScreen addPreference

Introduction

In this page you can find the example usage for android.preference PreferenceScreen addPreference.

Prototype

public boolean addPreference(Preference preference) 

Source Link

Document

Adds a Preference at the correct position based on the preference's order.

Usage

From source file:github.popeen.dsub.fragments.SettingsFragment.java

private PreferenceScreen expandServer(final int instance) {
    final PreferenceScreen screen = this.getPreferenceManager().createPreferenceScreen(context);
    screen.setTitle(R.string.settings_server_unused);
    screen.setKey(Constants.PREFERENCES_KEY_SERVER_KEY + instance);

    final EditTextPreference serverNamePreference = new EditTextPreference(context);
    serverNamePreference.setKey(Constants.PREFERENCES_KEY_SERVER_NAME + instance);
    serverNamePreference.setDefaultValue(getResources().getString(R.string.settings_server_unused));
    serverNamePreference.setTitle(R.string.settings_server_name);
    serverNamePreference.setDialogTitle(R.string.settings_server_name);

    if (serverNamePreference.getText() == null) {
        serverNamePreference.setText(getResources().getString(R.string.settings_server_unused));
    }/*w w  w. ja  va 2s. c  o  m*/

    serverNamePreference.setSummary(serverNamePreference.getText());

    final EditTextPreference serverUrlPreference = new EditTextPreference(context);
    serverUrlPreference.setKey(Constants.PREFERENCES_KEY_SERVER_URL + instance);
    serverUrlPreference.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    serverUrlPreference.setDefaultValue("http://yourhost");
    serverUrlPreference.setTitle(R.string.settings_server_address);
    serverUrlPreference.setDialogTitle(R.string.settings_server_address);

    if (serverUrlPreference.getText() == null) {
        serverUrlPreference.setText("http://yourhost");
    }

    serverUrlPreference.setSummary(serverUrlPreference.getText());
    screen.setSummary(serverUrlPreference.getText());

    final EditTextPreference serverLocalNetworkSSIDPreference = new EditTextPreference(context) {
        @Override
        protected void onAddEditTextToDialogView(View dialogView, final EditText editText) {
            super.onAddEditTextToDialogView(dialogView, editText);
            ViewGroup root = (ViewGroup) ((ViewGroup) dialogView).getChildAt(0);

            Button defaultButton = new Button(getContext());
            defaultButton.setText(internalSSIDDisplay);
            defaultButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.setText(internalSSID);
                }
            });
            root.addView(defaultButton);
        }
    };
    serverLocalNetworkSSIDPreference.setKey(Constants.PREFERENCES_KEY_SERVER_LOCAL_NETWORK_SSID + instance);
    serverLocalNetworkSSIDPreference.setTitle(R.string.settings_server_local_network_ssid);
    serverLocalNetworkSSIDPreference.setDialogTitle(R.string.settings_server_local_network_ssid);

    final EditTextPreference serverInternalUrlPreference = new EditTextPreference(context);
    serverInternalUrlPreference.setKey(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance);
    serverInternalUrlPreference.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    serverInternalUrlPreference.setDefaultValue("");
    serverInternalUrlPreference.setTitle(R.string.settings_server_internal_address);
    serverInternalUrlPreference.setDialogTitle(R.string.settings_server_internal_address);
    serverInternalUrlPreference.setSummary(serverInternalUrlPreference.getText());

    final EditTextPreference serverUsernamePreference = new EditTextPreference(context);
    serverUsernamePreference.setKey(Constants.PREFERENCES_KEY_USERNAME + instance);
    serverUsernamePreference.setTitle(R.string.settings_server_username);
    serverUsernamePreference.setDialogTitle(R.string.settings_server_username);

    final EditTextPreference serverPasswordPreference = new EditTextPreference(context);
    serverPasswordPreference.setKey(Constants.PREFERENCES_KEY_PASSWORD + instance);
    serverPasswordPreference.getEditText()
            .setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    serverPasswordPreference.setSummary("***");
    serverPasswordPreference.setTitle(R.string.settings_server_password);
    final CheckBoxPreference serverSyncPreference = new CheckBoxPreference(context);
    serverSyncPreference.setKey(Constants.PREFERENCES_KEY_SERVER_SYNC + instance);
    serverSyncPreference.setChecked(Util.isSyncEnabled(context, instance));
    serverSyncPreference.setSummary(R.string.settings_server_sync_summary);
    serverSyncPreference.setTitle(R.string.settings_server_sync);

    final Preference serverOpenBrowser = new Preference(context);
    serverOpenBrowser.setKey(Constants.PREFERENCES_KEY_OPEN_BROWSER);
    serverOpenBrowser.setPersistent(false);
    serverOpenBrowser.setTitle(R.string.settings_server_open_browser);
    serverOpenBrowser.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            openInBrowser(instance);
            return true;
        }
    });

    Preference serverRemoveServerPreference = new Preference(context);
    serverRemoveServerPreference.setKey(Constants.PREFERENCES_KEY_SERVER_REMOVE + instance);
    serverRemoveServerPreference.setPersistent(false);
    serverRemoveServerPreference.setTitle(R.string.settings_servers_remove);

    serverRemoveServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Util.confirmDialog(context, R.string.common_delete, screen.getTitle().toString(),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Reset values to null so when we ask for them again they are new
                            serverNamePreference.setText(null);
                            serverUrlPreference.setText(null);
                            serverUsernamePreference.setText(null);
                            serverPasswordPreference.setText(null);

                            // Don't use Util.getActiveServer since it is 0 if offline
                            int activeServer = Util.getPreferences(context)
                                    .getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
                            for (int i = instance; i <= serverCount; i++) {
                                Util.removeInstanceName(context, i, activeServer);
                            }

                            serverCount--;
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount);
                            editor.commit();

                            removeCurrent();

                            SubsonicFragment parentFragment = context.getCurrentFragment();
                            if (parentFragment instanceof SettingsFragment) {
                                SettingsFragment serverSelectionFragment = (SettingsFragment) parentFragment;
                                serverSelectionFragment.checkForRemoved();
                            }
                        }
                    });

            return true;
        }
    });

    Preference serverTestConnectionPreference = new Preference(context);
    serverTestConnectionPreference.setKey(Constants.PREFERENCES_KEY_TEST_CONNECTION + instance);
    serverTestConnectionPreference.setPersistent(false);
    serverTestConnectionPreference.setTitle(R.string.settings_test_connection_title);
    serverTestConnectionPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            testConnection(instance);
            return false;
        }
    });

    screen.addPreference(serverNamePreference);
    screen.addPreference(serverUrlPreference);
    screen.addPreference(serverInternalUrlPreference);
    screen.addPreference(serverLocalNetworkSSIDPreference);
    screen.addPreference(serverUsernamePreference);
    screen.addPreference(serverPasswordPreference);
    screen.addPreference(serverSyncPreference);
    screen.addPreference(serverTestConnectionPreference);
    screen.addPreference(serverOpenBrowser);
    screen.addPreference(serverRemoveServerPreference);

    return screen;
}

From source file:github.daneren2005.dsub.fragments.SettingsFragment.java

private PreferenceScreen expandServer(final int instance) {
    final PreferenceScreen screen = this.getPreferenceManager().createPreferenceScreen(context);
    screen.setTitle(R.string.settings_server_unused);
    screen.setKey(Constants.PREFERENCES_KEY_SERVER_KEY + instance);

    final EditTextPreference serverNamePreference = new EditTextPreference(context);
    serverNamePreference.setKey(Constants.PREFERENCES_KEY_SERVER_NAME + instance);
    serverNamePreference.setDefaultValue(getResources().getString(R.string.settings_server_unused));
    serverNamePreference.setTitle(R.string.settings_server_name);
    serverNamePreference.setDialogTitle(R.string.settings_server_name);

    if (serverNamePreference.getText() == null) {
        serverNamePreference.setText(getResources().getString(R.string.settings_server_unused));
    }//  w w w.j a va  2s.c o  m

    serverNamePreference.setSummary(serverNamePreference.getText());

    final EditTextPreference serverUrlPreference = new EditTextPreference(context);
    serverUrlPreference.setKey(Constants.PREFERENCES_KEY_SERVER_URL + instance);
    serverUrlPreference.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    serverUrlPreference.setDefaultValue("http://yourhost");
    serverUrlPreference.setTitle(R.string.settings_server_address);
    serverUrlPreference.setDialogTitle(R.string.settings_server_address);

    if (serverUrlPreference.getText() == null) {
        serverUrlPreference.setText("http://yourhost");
    }

    serverUrlPreference.setSummary(serverUrlPreference.getText());
    screen.setSummary(serverUrlPreference.getText());

    final EditTextPreference serverLocalNetworkSSIDPreference = new EditTextPreference(context) {
        @Override
        protected void onAddEditTextToDialogView(View dialogView, final EditText editText) {
            super.onAddEditTextToDialogView(dialogView, editText);
            ViewGroup root = (ViewGroup) ((ViewGroup) dialogView).getChildAt(0);

            Button defaultButton = new Button(getContext());
            defaultButton.setText(internalSSIDDisplay);
            defaultButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    editText.setText(internalSSID);
                }
            });
            root.addView(defaultButton);
        }
    };
    serverLocalNetworkSSIDPreference.setKey(Constants.PREFERENCES_KEY_SERVER_LOCAL_NETWORK_SSID + instance);
    serverLocalNetworkSSIDPreference.setTitle(R.string.settings_server_local_network_ssid);
    serverLocalNetworkSSIDPreference.setDialogTitle(R.string.settings_server_local_network_ssid);

    final EditTextPreference serverInternalUrlPreference = new EditTextPreference(context);
    serverInternalUrlPreference.setKey(Constants.PREFERENCES_KEY_SERVER_INTERNAL_URL + instance);
    serverInternalUrlPreference.getEditText().setInputType(InputType.TYPE_TEXT_VARIATION_URI);
    serverInternalUrlPreference.setDefaultValue("");
    serverInternalUrlPreference.setTitle(R.string.settings_server_internal_address);
    serverInternalUrlPreference.setDialogTitle(R.string.settings_server_internal_address);
    serverInternalUrlPreference.setSummary(serverInternalUrlPreference.getText());

    final EditTextPreference serverUsernamePreference = new EditTextPreference(context);
    serverUsernamePreference.setKey(Constants.PREFERENCES_KEY_USERNAME + instance);
    serverUsernamePreference.setTitle(R.string.settings_server_username);
    serverUsernamePreference.setDialogTitle(R.string.settings_server_username);

    final EditTextPreference serverPasswordPreference = new EditTextPreference(context);
    serverPasswordPreference.setKey(Constants.PREFERENCES_KEY_PASSWORD + instance);
    serverPasswordPreference.getEditText()
            .setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    serverPasswordPreference.setSummary("***");
    serverPasswordPreference.setTitle(R.string.settings_server_password);

    final CheckBoxPreference serverTagPreference = new CheckBoxPreference(context);
    serverTagPreference.setKey(Constants.PREFERENCES_KEY_BROWSE_TAGS + instance);
    serverTagPreference.setChecked(Util.isTagBrowsing(context, instance));
    serverTagPreference.setSummary(R.string.settings_browse_by_tags_summary);
    serverTagPreference.setTitle(R.string.settings_browse_by_tags);
    serverPasswordPreference.setDialogTitle(R.string.settings_server_password);

    final CheckBoxPreference serverSyncPreference = new CheckBoxPreference(context);
    serverSyncPreference.setKey(Constants.PREFERENCES_KEY_SERVER_SYNC + instance);
    serverSyncPreference.setChecked(Util.isSyncEnabled(context, instance));
    serverSyncPreference.setSummary(R.string.settings_server_sync_summary);
    serverSyncPreference.setTitle(R.string.settings_server_sync);

    final Preference serverOpenBrowser = new Preference(context);
    serverOpenBrowser.setKey(Constants.PREFERENCES_KEY_OPEN_BROWSER);
    serverOpenBrowser.setPersistent(false);
    serverOpenBrowser.setTitle(R.string.settings_server_open_browser);
    serverOpenBrowser.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            openInBrowser(instance);
            return true;
        }
    });

    Preference serverRemoveServerPreference = new Preference(context);
    serverRemoveServerPreference.setKey(Constants.PREFERENCES_KEY_SERVER_REMOVE + instance);
    serverRemoveServerPreference.setPersistent(false);
    serverRemoveServerPreference.setTitle(R.string.settings_servers_remove);

    serverRemoveServerPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Util.confirmDialog(context, R.string.common_delete, screen.getTitle().toString(),
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Reset values to null so when we ask for them again they are new
                            serverNamePreference.setText(null);
                            serverUrlPreference.setText(null);
                            serverUsernamePreference.setText(null);
                            serverPasswordPreference.setText(null);

                            // Don't use Util.getActiveServer since it is 0 if offline
                            int activeServer = Util.getPreferences(context)
                                    .getInt(Constants.PREFERENCES_KEY_SERVER_INSTANCE, 1);
                            for (int i = instance; i <= serverCount; i++) {
                                Util.removeInstanceName(context, i, activeServer);
                            }

                            serverCount--;
                            SharedPreferences.Editor editor = settings.edit();
                            editor.putInt(Constants.PREFERENCES_KEY_SERVER_COUNT, serverCount);
                            editor.commit();

                            removeCurrent();

                            SubsonicFragment parentFragment = context.getCurrentFragment();
                            if (parentFragment instanceof SettingsFragment) {
                                SettingsFragment serverSelectionFragment = (SettingsFragment) parentFragment;
                                serverSelectionFragment.checkForRemoved();
                            }
                        }
                    });

            return true;
        }
    });

    Preference serverTestConnectionPreference = new Preference(context);
    serverTestConnectionPreference.setKey(Constants.PREFERENCES_KEY_TEST_CONNECTION + instance);
    serverTestConnectionPreference.setPersistent(false);
    serverTestConnectionPreference.setTitle(R.string.settings_test_connection_title);
    serverTestConnectionPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            testConnection(instance);
            return false;
        }
    });

    screen.addPreference(serverNamePreference);
    screen.addPreference(serverUrlPreference);
    screen.addPreference(serverInternalUrlPreference);
    screen.addPreference(serverLocalNetworkSSIDPreference);
    screen.addPreference(serverUsernamePreference);
    screen.addPreference(serverPasswordPreference);
    screen.addPreference(serverTagPreference);
    screen.addPreference(serverSyncPreference);
    screen.addPreference(serverTestConnectionPreference);
    screen.addPreference(serverOpenBrowser);
    screen.addPreference(serverRemoveServerPreference);

    return screen;
}