Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to modify my custom properties file in android

i am using a properties file to get the url of various webservices i am calling from my android. I want to provide a congiguration option so that ip address for web service can be modified. how to proceed ?

i have a resource folder in src folder which have the following values update=http://10.52.165.226:50000/android/rest/get/updateSfc ShopOrder=http://10.52.165.226:50000/android/rest/getShopOrder/bySite?site=

i am using resource bundle to use this values in android.?

I am thinking of reading the file and replace all occrence of Ip address. how to rad the properties file and edit it in android

like image 617
May13ank Avatar asked Dec 20 '25 05:12

May13ank


2 Answers

Here is a complete solution for you to use .properties file in your project.

1 Create a file named app.properties in assets folder of your android project

2 edit the file and write with in properties that you want to use for example as

test=success 

And Save file

3 Write this Method with in your Activity Class

  private Properties loadPropties() throws IOException {
  String[] fileList = { "app.properties" };
  Properties prop = new Properties();
  for (int i = fileList.length - 1; i >= 0; i--) {
     String file = fileList[i];
     try {
        InputStream fileStream = getAssets().open(file);
        prop.load(fileStream);
        fileStream.close();
     } catch (FileNotFoundException e) {
        Log.d(TAG, "Ignoring missing property file " + file);
     }
  }
  return prop;
  }

4 With in OnCreate Method write some thing like this

     Properties prop = null; 
     try {
        prop = loadPropties();
     } catch (IOException e) {
        Log.e(TAG, "Exception", e);
     }
     Toast.makeText(getApplicationContext(), "Result " + prop.getProperty("test"),
                    Toast.LENGTH_LONG).show();

5 add necessary imports

Hope this helps :)

like image 113
Rohit Sharma Avatar answered Dec 22 '25 20:12

Rohit Sharma


Read about Data Storage in Android and more specifically Shared Preferences. For more complete usage of saving user preferences, read about the PreferenceActivity.

A tutorial on using Shared Preferences can be found here

like image 21
Rajesh Avatar answered Dec 22 '25 20:12

Rajesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!