Tuesday, November 9, 2010

Android #4: Working with Android Resources - String Resources

Typically the res folder within any android project contains all external resources such as icons, images, layout, string etc under the respective sub-folders within. All resources are bound to the app in a way that we can change them without the need to recompile the app or change any app code. Unlike assets, android generates an ID in R.java file (auto-generated file) for each resource file using which they will be accessed within the code. Resources are very much useful when developing multi-lingual app supporting l10n and i18n.

Types of resources:
  • Strings, colors, arrays, dimensions are defined in res/values/ directory. Typical usage would be in Localization and Internationalization
  • Images are defined in res/drawable directory. Typical usage would be to put all the images or icons that an application needs
  • XML’s are defined in res/xml/ directory for xml files. Typical usage would be to hold custom data that can be used in application
  • Layout resources are defined in res/layout/ for declaring the views. Typical usage would be to construct the user interface of the activities
Following example illustrates accessing resources from res folder in a simple android project:



Working with String resources:

Android offers three types of string resources: Plain Strings, string formats and styled texts. Refer the following examples

layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- ResourceDemo -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/resPlainStrTxt"
android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Plain String Resources - This is referenced from res/layout/main.xml"
android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:id="@+id/resPlainStrTxtId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:id="@+id/resFormattedStrTxtId"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:text="@string/resFormattedStrTxt" android:textColor="#CDE472" />

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

<TextView android:id="@+id/resStyledStrTxtId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textColor="#CDE472" android:text="@string/resStyledStrTxt"/>

<!-- Line -->
<View android:layout_width="fill_parent" android:layout_height="2dp"
android:background="#99CCFF" />

</LinearLayout>
ResourceDemoActivity.java
package in.satworks.android.samples.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class ResourceDemoActivity extends Activity {
private TextView resPlainStrTxtView=null;
private TextView resFormattedStrTxtView=null;
private TextView resStyledStrTxtView=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Working with resources - Start
// Accessing Resources for Plain String contents from the resources folder - start
resPlainStrTxtView=(TextView)findViewById(R.id.resPlainStrTxtId);
resPlainStrTxtView.setText("Plain String Resources - This is referenced from code");
// Accessing Resources for Plain String contents from the resources folder - end

// Accessing Resources for Formatted String contents from the resources folder - start
resFormattedStrTxtView=(TextView)findViewById(R.id.resFormattedStrTxtId);
String formatStr=getString(R.string.resFormattedStrTxt);
String formattedStr=String.format(formatStr, "- \"This New Formatted Text\"");
resFormattedStrTxtView.setText(formattedStr);
// Accessing Resources for Formatted String contents from the resources folder - end

// Accessing Resources for Formatted String contents from the resources folder - start
resStyledStrTxtView=(TextView)findViewById(R.id.resStyledStrTxtId);
/* without styling
String styleStr=getString(R.string.resStyledStrTxt);
resStyledStrTxtView.setText(styleStr);
*/
// with styling
resStyledStrTxtView.setText(R.string.resStyledStrTxt);
// Accessing Resources for Formatted String contents from the resources folder - end
// Working with resources - End
}
}
values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ResourceDemo</string>
<string name="resPlainStrTxt">Plain String Resources - This is referenced from res/values/string.xml</string>
<string name="resFormattedStrTxt">Formatted String Resources - This is referenced from res/values/string.xml and is formatted with %1$s</string>
<string name="resStyledStrTxt">Styled String Resources - This is referenced from res/values/string.xml and is styled with \n<b>bold</b>, \n<u>underline</u> and \n<i>italics</i></string>
</resources>

Monday, November 8, 2010

Android #3: Working with Android Assets

Assuming that the user knows to create a HelloWorld kinda simple android app, we will look into understanding assets and means to access them within the android app.

Working with Assets:

Typically the asset folder within any android project that contains static files that one wishes to package within the application for deployment onto the device, read as a stream of bytes and accessed as just like the way we access the file system in java programming language. All files within this folder doesn’t generate an ID in R.java file (auto-generated file).

Following example illustrates accessing contents of a static file from the assets folder in a simple android project:



main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<!-- AssetDemo -->
<TextView android:id="@+id/assetTxtId" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textColor="#FFCC33" />

</LinearLayout>
AssetDemoActivity.java:

package in.satworks.android.samples.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;

public class AssetDemoActivity extends Activity {
private TextView assetTxtView=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// Retrieving contents of a static file from the assets folder - start
assetTxtView=(TextView)findViewById(R.id.assetTxtId);
readAsset();
// Retrieving contents of a static file from the assets folder - end
}

private void readAsset()
{
//Get asset manager instance
AssetManager assetMngr=getAssets();
//Read contents from file under assets
try {
InputStream inStream=assetMngr.open("HelloAsset.txt");
BufferedReader bReader=new BufferedReader(new InputStreamReader(inStream));
String assetText="";
String asText="";
while((asText=bReader.readLine())!=null)
{
System.out.println("[AssetDemoActivity] [assetText] ***** "+assetText);
assetText=assetText+asText+"\n";
}
assetTxtView.setText(assetText);
bReader.close();
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

assets/HelloAsset.txt:
AssetDemo: This is referenced from assets/HelloAsset.txt