• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Listpreference question

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:
 preferences = PreferenceManager.getDefaultSharedPreferences(this);
		  String listpref = preferences.getString("listPref", "n/a");              
		  
		  if (listpref.equals("color1"))
		  {
			  Toast.makeText(MainActivity.this,	"Black" + listpref, Toast.LENGTH_LONG).show();
		  }
		  else if (listpref.equals("color2"))
		  {
			  Toast.makeText(MainActivity.this,	"Brown" + listpref, Toast.LENGTH_LONG).show();
		
		  }
		   else if (listpref.equals("color3"))
		  {
			  Toast.makeText(MainActivity.this,	"GoldGreen" + listpref, Toast.LENGTH_LONG).show();
		  }

Arrays.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="listArray">
   <item>Black</item>
   <item>Brown</item>
   <item>Gold-green</item>
   <item>Pink-white</item>
</string-array>
 
<string-array name="listValues">
   <item>color1</item>
   <item>color2</item>
   <item>color3</item>
   <item>color4</item>
</string-array>
</resources>
 
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:
Preference pref = getPreferenceScreen().findPreference("listPref"));
pref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
 public boolean onPreferenceChange(Preference p, Object newValue) {
  String color = (String) newValue;
  // make your toast here..
 return true;
 }
}
Hope this helps.
 
Thank you. Now this is my code, but nothing happens. Why?

Code:
final Preference listpref = getPreferenceScreen().findPreference("listPref");
		listpref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() 
		{
		 public boolean onPreferenceChange(Preference p, Object newValue) 
		 {
		  String color = (String) newValue;
		  
		  if (color.equals("color1"))
		  {
			  Toast.makeText(EditPreferences.this, "Black", Toast.LENGTH_SHORT);
		  }
		  else if (color.equals("color2"))
		  {
			  Toast.makeText(EditPreferences.this, "Brown", Toast.LENGTH_SHORT);
		  }
		  return true;
		 }
		});

However if i put some code here to change the widget's background, textview color... it works. But i never get the toast msg...
 
Back
Top Bottom