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.
Here is screen shoot of our simple example app where we are reading values from property file and showing in the Toast.
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.
Here is screen shoot of our simple example app where we are reading values from property file and showing in the Toast.
So we first create our simple property file keys and value pair as
below example and put it into assests folder of our project
nd 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
and we made a class to read this file as below
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;
}
}
and in our mainActivity Class we read Property from Property File as
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();
}
}
and our xml layout and manifest file content nothing special :P
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itdeveloper.khurram.PropEx"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.itdeveloper.khurram.PropEx.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
No comments :
Post a Comment