1. Weyard Wiz's Avatar
    Im following the steps laid out here (https://developer.android.com/codelabs/android-training-adding-settings-to-app?index=..%2F..%2Fandroid-training#3) for using the Settings Activity Template and despite some changes (e.g. now a root_preferences.xml file gets created instead of multiple xml files for different preference screens when selecting the Settings Activity template) I pretty much have nailed down the steps very similarly.

    It says towards the end:

    The first time you run the app, you should see "-1" displayed in the Toast because you haven't changed the setting yet.

    However, instead of -1, Im seeing US as the toast message. Im thinking thats because its the default value, but they also set the default value as the US, yet the expectation according to the instructions is -1 the first time they run the app...how come?

    Here is my code and the result Im getting first time running the app

    SettingsActivity.java:

    package com.example.droidcafe;

    import android.os.Bundle;

    import androidx.appcompat.app.ActionBar;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.preference.PreferenceFragmentCompat;

    public class SettingsActivity extends AppCompatActivity {

    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_activity);
    if (savedInstanceState == null) {
    getSupportFragmentManager()
    .beginTransaction()
    .replace(R.id.settings, new SettingsFragment())
    .commit();
    }
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
    actionBar.setDisplayHomeAsUpEnabled(true);
    }
    }

    public static class SettingsFragment extends PreferenceFragmentCompat {
    @override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    setPreferencesFromResource(R.xml.root_preferences, rootKey);
    }
    }
    }
    MainActivity.java:
    @override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
    @override
    public void onClick(View view) {
    Intent intent =
    new Intent(MainActivity.this, OrderActivity.class);
    intent.putExtra(EXTRA_MESSAGE, mOrderMessage);
    startActivity(intent);
    }

    });

    PreferenceManager.setDefaultValues(this,
    R.xml.root_preferences, false);

    SharedPreferences sharedPref = PreferenceManager
    .getDefaultSharedPreferences(this);
    String marketPref = sharedPref
    .getString("market", "-1");
    displayToast(marketPref);

    }
    root_preferences.xml

    <PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">

    <PreferenceCategory app:title="@string/pref_header_account">

    <ListPreference
    app:defaultValue="US"
    app:entries="@array/pref_market_titles"
    app:entryValues="@array/pref_market_values"
    app:key="market"
    app:negativeButtonText="@null"
    appositiveButtonText="@null"
    app:title="@string/pref_title_account" />

    </PreferenceCategory>

    </PreferenceScreen>
    strings.xml:

    <string name="pref_header_account">Account</string>
    <!-- Sync Preferences -->
    <string name="pref_title_account">Market</string>
    <string-array name="pref_market_titles">
    <item>United States</item>
    <item>Canada</item>
    <item>United Kingdom</item>
    <item>India</item>
    <item>Japan</item>
    <item>Other</item>
    </string-array>

    <string-array name="pref_market_values">
    <item>US</item>
    <item>CA</item>
    <item>UK</item>
    <item>IN</item>
    <item>JA0</item>
    <item>-1</item>
    </string-array>

    first time running app:

    Toast message: US

    Expectation: -1

    I realize the default value is US, but their codelab default is also US, yet the codelab says it should display first time -1.
    11-19-2022 08:23 PM
  2. B. Diddy's Avatar
    Welcome to Android Central! I moved this to the Software Development forum for more specific traffic.
    11-20-2022 03:12 AM

Similar Threads

  1. Samsung secure folder app causes major problems[Solved]
    By maricopa1963 in forum Samsung Galaxy Z Fold 4
    Replies: 1
    Last Post: 11-19-2022, 07:57 PM
  2. How do i fix broken camera on zfold 4
    By Android Central Question in forum Samsung Galaxy Z Fold 4
    Replies: 2
    Last Post: 11-19-2022, 07:50 PM
  3. Replies: 2
    Last Post: 11-19-2022, 06:28 PM
  4. Is there a name for the surface of the phone?
    By nuraman00 in forum Samsung Galaxy Note 4
    Replies: 2
    Last Post: 11-19-2022, 04:12 PM
  5. How do i fix broken camera on zfold 4
    By Android Central Question in forum Ask a Question
    Replies: 0
    Last Post: 11-19-2022, 02:43 PM
LINK TO POST COPIED TO CLIPBOARD