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;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

No related posts.

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>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.