1. Are you ready for the Galaxy S20? Here is everything we know so far!

Listpreference question

Discussion in 'Android Development' started by erdomester, Feb 9, 2011.

  1. erdomester

    erdomester Newbie
    Thread Starter

    Hello,
    I have a program:
    EditPreferences.java: to add preferences
    HelloWidget.java: just an intent referring to UpdateService.class
    MainActivity.java: for listview, textviews, buttons
    UpdateService: for the widget, uploading contents of textviews of the widget

    config.xml: xml for the activity
    main.xml: xml for the widget
    row.xml: xml for two textviews that is part of a listview in config.xml
    arrays.xml
    strings.xml
    preferences.xml
    widget_provider.xml

    These are my files, but we need just some of them.
    When user opens the app there is a Settings button for preferences, where user can choose skins.
    I want the app to show a toast message when user clicks a skin.

    I put this code into MainActivity.java but there is no toast message at the selection. However if user selects one, then quits app and then opens it again, a toast message appears with the correct text. I know this maybe is because it is in the onCreate() method of the activity? Where should i put this?

    Code (Text):
    1.  preferences = PreferenceManager.getDefaultSharedPreferences(this);
    2.           String listpref = preferences.getString("listPref", "n/a");              
    3.          
    4.           if (listpref.equals("color1"))
    5.           {
    6.               Toast.makeText(MainActivity.this, "Black" + listpref, Toast.LENGTH_LONG).show();
    7.           }
    8.           else if (listpref.equals("color2"))
    9.           {
    10.               Toast.makeText(MainActivity.this, "Brown" + listpref, Toast.LENGTH_LONG).show();
    11.        
    12.           }
    13.            else if (listpref.equals("color3"))
    14.           {
    15.               Toast.makeText(MainActivity.this, "GoldGreen" + listpref, Toast.LENGTH_LONG).show();
    16.           }
    Arrays.xml:
    Code (Text):
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <resources>
    3. <string-array name="listArray">
    4.    <item>Black</item>
    5.    <item>Brown</item>
    6.    <item>Gold-green</item>
    7.    <item>Pink-white</item>
    8. </string-array>
    9.  
    10. <string-array name="listValues">
    11.    <item>color1</item>
    12.    <item>color2</item>
    13.    <item>color3</item>
    14.    <item>color4</item>
    15. </string-array>
    16. </resources>
    17.  
    18.  
     



    1. Download the Forums for Android™ app!


      Download

       
  2. Tapirboy

    Tapirboy Newbie

    Hi Erdomester,

    you are correct, this is because you have put the code in onCreate() for MainActivity.java.

    What you want is to add onPreferenceChangeListener to your preferences in your onCreate() in EditPreferences.java instead. They will fire when a preference has changed.

    sample:

    Code (Text):
    1. Preference pref = getPreferenceScreen().findPreference("listPref"));
    2. pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    3.  public boolean onPreferenceChange(Preference p, Object newValue) {
    4.   String color = (String) newValue;
    5.   // make your toast here..
    6.  return true;
    7.  }
    8. }
    Hope this helps.
     
  3. erdomester

    erdomester Newbie
    Thread Starter

    Thank you. Now this is my code, but nothing happens. Why?

    Code (Text):
    1.  
    2. final Preference listpref = getPreferenceScreen().findPreference("listPref");
    3.         listpref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
    4.         {
    5.          public boolean onPreferenceChange(Preference p, Object newValue)
    6.          {
    7.           String color = (String) newValue;
    8.          
    9.           if (color.equals("color1"))
    10.           {
    11.               Toast.makeText(EditPreferences.this, "Black", Toast.LENGTH_SHORT);
    12.           }
    13.           else if (color.equals("color2"))
    14.           {
    15.               Toast.makeText(EditPreferences.this, "Brown", Toast.LENGTH_SHORT);
    16.           }
    17.           return true;
    18.          }
    19.         });
    20.  
    However if i put some code here to change the widget's background, textview color... it works. But i never get the toast msg...
     
Loading...

Share This Page

Loading...