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
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"?>ResourceDemoActivity.java
<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>
package in.satworks.android.samples.activity;values/strings.xml:
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
}
}
<?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>