user3487657 user3487657 - 17 days ago 5
Android Question

Android Preference file set name

I am currently following this tutorial Android Settings

my problem is it save the app settings as

com.packagename_preferences.xml


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

mychoosennamehere_preferences.xml


or

somenamesiwanttouse.xml

Answer

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.remove(PlayListName);
editor.putString(fileName, fileName);
 editor.commit();
 PlayListName=fileName;
Comments