Getting formatted string from strings.xml in Android

I found a way how to get formatted string from strings.xml file.

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 FaYnaSoft 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 FaYnaSoft 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.

  • Share/Bookmark

Related posts:

  1. Using SubActivities in Android
  2. Using XML file resources in Android
  3. Opening browser with specific URI in Android
  4. Checking if camera presents in Android device
  5. Image rotating in Android

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>