How to retrieve data from strings.xml file

n this tutorial how to retrieve the data from strings.xml file is shown and how to change color and text from strings.xml is shown.

Code:

activity_main.xml

<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”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<TextView
android:id=”@+id/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textColor=”@color/color1″
android:text=”@string/hello” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

strings.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>

<string name=”app_name”>Demo</string>
<string name=”action_settings”>Settings</string>
<string name=”hello_world”>Hello world!</string>
<color name=”color1″>#ff00f1</color>
<string name=”hello”>Hello Friends</string>

</resources>

 

Add a Comment

Your email address will not be published. Required fields are marked *