[Fuente: http://khurramitdeveloper.blogspot.com.es/2013/07/properties-file-in-android.html]
Properties files are one of the most frequently used mechanism for storing configuration information for applications. And so is the code to access these properties from the application. This article is intended to demonstrate a simple way of configuring and using Properties files in Android applications.
Each parameter is stored as a pair of strings, one storing the name of the parameter (called the key), and the other storing the value.
So we first create our simple property file keys and value pair as below example and put it into assests folder of our project
and here is content of properties file.
MyBlog= khurramitdeveloper@blogspot.com Profile=http://in.linkedin.com/pub/mohammed-khurram-mohammed-basheer/66/557/3b8 Name= Mohammed Khurram Ahmed Profession= Software Developer
package com.itdeveloper.khurram.PropEx; /** * @author Khurram * */ import java.io.IOException; import java.io.InputStream; import java.util.Properties; import android.content.Context; import android.content.res.AssetManager; import android.util.Log; public class AssetsPropertyReader { private Context context; private Properties properties; public AssetsPropertyReader(Context context) { this.context = context; /** * Constructs a new Properties object. */ properties = new Properties(); } public Properties getProperties(String FileName) { try { /** * getAssets() Return an AssetManager instance for your * application's package. AssetManager Provides access to an * application's raw asset files; */ AssetManager assetManager = context.getAssets(); /** * Open an asset using ACCESS_STREAMING mode. This */ InputStream inputStream = assetManager.open(FileName); /** * Loads properties from the specified InputStream, */ properties.load(inputStream); } catch (IOException e) { // TODO Auto-generated catch block Log.e("AssetsPropertyReader",e.toString()); } return properties; } }
package com.itdeveloper.khurram.PropEx; import java.util.Properties; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.widget.Toast; public class MainActivity extends Activity { private AssetsPropertyReader assetsPropertyReader; private Context context; private Properties p; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; assetsPropertyReader = new AssetsPropertyReader(context); p =assetsPropertyReader.getProperties("MyStringsFile.properties"); Toast.makeText(context, p.getProperty("MyBlog"), 1).show(); } }
NOTA: Con Graddle hay que colocar la carpeta ‘assets’ dentro de la carpeta ‘main’