Q: Android set preferences from XML with PreferenceManager I am trying to get preferences from XML but without using a PreferenceActivity.
I just want to load from the file when my Main activity is created and toast a value from preferences. But the problem is that the toast is empty (null?). I have a class that load the preferences.
Here is the onCreate method of the Main Activity
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Preferences prefs = new Preferences();
}

Preferences class (only the constructor...)
public Preferences(Context context) {
    PreferenceManager.setDefaultValues(context, "MyPrefs", 0, R.xml.preferences, false);
    SharedPreferences sharedPreferences = context.getSharedPreferences("MyPrefs", 0);
    Editor editor = sharedPreferences.edit();
    String myValue = sharedPreferences.getString("myKey", null); // I don't know if null is OK
    Toast.makeText(context.getApplicationContext(), myValue, Toast.LENGTH_SHORT).show();
}

And the XML file
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <Preference android:key="myKey" android:defaultValue="hello" />
</PreferenceScreen>

The first time I launch the app, I would like the default value to be set. So here I want the app to toast "hello" while I haven't set the value with editor.putString(key, value).
Do you know what could be wrong?
Thanks

A: You are programming in java. By suns convention I think you are obliged to use config.propeties file.
I will give a quick and full tutorial to get you going in this matter. I really recommend you using this method cause most of programmers do like that.
I will give you a quick tutorial how to make this file. Where to put it. And how to get data from it.
Begin.
Put a file config.properties into assets folder:

SAMPLE of config.properties
domain=@domain.com.pl
errorTextColor=\#FF0000
serverPort=1234

Method how to access and retrieve data from config.properties
public static String getConfigurationPropertiesValue(String value,
         Context context) {
      try {
         Resources resources = context.getResources();
         AssetManager assetManager = resources.getAssets();
     try {
        InputStream inputStream = assetManager.open("config.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        return properties.getProperty(value);
     } catch (IOException e) {
        Log.e("getConfigurationPropertiesValue",
              "Failed to open config property file");
     }
  } catch (Exception e) {
     e.printStackTrace();
  }
  return null;

}
Literature:
http://en.wikipedia.org/wiki/.properties
http://www.mkyong.com/java/java-properties-file-examples/
EDIT:
You can also use sharedPreferences to have more control over data like adding data/deleting data/update data. SharedPreferences are more like a SQLite database of android with a nice api to use so you don't really need to know the location of database or SQL.
In order to use it you need to create your data. You only need to do this once. Or more if user decides that he has an urge to clean you app data from settings -> applications.
Creating data:
   public static void create(Context cw) {
      SharedPreferences sharedPreferences = cw.getSharedPreferences(
            ANDROID_MESSENGER, Activity.MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPreferences.edit();
      editor.putBoolean("isRegistered", false);
      editor.putString("phoneNumber", null);
      editor.putString("callingCode", null);
      String uuid = UUID.randomUUID().toString();
      editor.putString("token", uuid);

      editor.putBoolean("internetOnly", false);
      editor.putBoolean("logToDev", true);
      editor.putBoolean("dataTransfer", true);
      Log.i("create", "Generating Token: " + uuid);
      editor.commit();
   }

Accesing existing data:
   public static String getToken(Context cw) {
      SharedPreferences sharedPreferences = cw.getSharedPreferences(
            ANDROID_MESSENGER, Activity.MODE_PRIVATE);
      return sharedPreferences.getString("token", null);
   }

Updating data:
   public static void setPhoneNumber(Context cw, String phoneNumber) {
      SharedPreferences sharedPreferences = cw.getSharedPreferences(
            ANDROID_MESSENGER, Activity.MODE_PRIVATE);
      SharedPreferences.Editor editor = sharedPreferences.edit();
      editor.putString("phoneNumber", phoneNumber);
      editor.commit();
   }

For an mechanism that will check if data exist or if user deleted it you can use something simple like an additional variable that should be true if all your data is configured. Or there should be an checked method from appshared preferences for that.
Cheers!

A: I tried for a long time and found a solution for this :-)
Just a "Preference" is not being recognized by android. Though i don't know why.
If you change it as a Any of the tag like "EditTextPreference" or "CheckboxPreference" its working fine.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <EditTextPreference android:key="CLIENT_HOMESCREEN_TITLE"
                android:defaultValue="Home Screen"/>
    <EditTextPreference android:key="CLIENT_ADMIN_BUTTON"
                android:defaultValue="Admin"/>
    <EditTextPreference android:key="CLIENT_PLAYER_BUTTON"
                android:defaultValue="Player"/>
    <EditTextPreference android:key="CLIENT_SAVE_BUTTON"
                android:defaultValue="Save"/>
    <EditTextPreference android:key="CLIENT_CANCEL_BUTTON"
                android:defaultValue="Cancel"/>
    <EditTextPreference android:key="CLIENT_SERVER_SETUP_IP"
                android:defaultValue="IP:"/>
    <EditTextPreference android:key="CLIENT_SERVER_SETUP_PORT"
                android:defaultValue="Port:"/>
    <EditTextPreference android:key="CLIENT_SERVER_SETUP_TITLE"
                android:defaultValue="Server Setup"/>
</PreferenceScreen>

