Home > Android, Android how to > Getting formatted string from strings.xml in Android

Getting formatted string from strings.xml in Android


We moved. Please visit this link for this post.

 

 

 

I found way to get formatted string from strings.xml.

I am going to show how I did it. Please refer to getString method.

strings.xml

<string name="text">My name is %s. I work at FaYna Soft Labs %d months.</string>

Activity

getResources().getString(R.string.text, "Oleg Mazurashu", 7)

Example:

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

/**
 * Activity which shows how to use formatted string from strings.xml
 *
 * @author FaYna Soft Labs
 */
public class Main extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      TextView tv = (TextView) findViewById(R.id.textId);
      tv.setText(getResources().getString(R.string.text, "Oleg Mazurashu", 7));
  }
}

I like this approach.

Advertisements
Categories: Android, Android how to
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: