user3487657 user3487657 - 1 year ago 80
Android Question

Android Preference file set name

I am currently following this tutorial Android Settings

my problem is it save the app settings as


Is there any way I can set its file name? like this




Answer Source

Although you can rename the preference file but you should not do that. Actually, the exact location and name of this preference file is not documented, so I'd suggest you don't rely on some conventions when trying to access this file directly, since the location and name may be changed in future - SharedPreferences should be the only way to access this file.And your App will stop working.

Anyhow here is the way to rename your preference file

String fileName="mychoosennamehere_preferences";
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+"com.packagename_preferences"+".xml");
 f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));

 SharedPreferences  mySharedPreferences=getSharedPreferences(fileName,Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString(fileName, fileName);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download