Roger W
Roger W

Reputation: 107

Android - programatically adding text from strings.xml to TextView

OK, so I've been following a tutorial (http://javatechig.com/android/android-spinner-example) which is about creating Spinners and dynamically adding images (the locations of which are defined in the strings.xml file) to the view. Well that works fine but I'd like to extend the project so that I can include text arrays, again defined in the strings.xml file, to appear both before and after the image. My code follows and, as you can see I'd like to substitute the 2 setText lines with references to the spinner item that I've already selected. Any ideas? Thanks for your interest.

Thanks all for your input and I've now solved the problem and am showing some commented code below on how I resolved it...

public class MainActivity extends Activity {

private ImageView image;
private String[] states;
private Spinner spinner1;
private TypedArray imgs;
private String[] leaders;
private String[] descrip;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    states = getResources().getStringArray(R.array.countries);
    imgs = getResources().obtainTypedArray(R.array.leader_photos);

    // --- The following lines grab the problematic string arrays defined in
    // strings.xml
    leaders = getResources().getStringArray(R.array.leaders);
    descrip = getResources().getStringArray(R.array.descrip);

    image = (ImageView) findViewById(R.id.leaderPhoto);
    spinner1 = (Spinner) findViewById(R.id.spinner1);

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, states);
    dataAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(dataAdapter);

    spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                int position, long id) {

            // The variable index identifies what position the selected
            // spinner is. TextView text... locks to the TextView defined in
            // the activity_main.xml layout file. text.setText... loads the
            // appropriate value from the leaders string array
            int index = parent.getSelectedItemPosition();
            TextView text = (TextView) findViewById(R.id.tv1);
            text.setText(leaders[index]);

            image.setImageResource(imgs.getResourceId(
                    spinner1.getSelectedItemPosition(), -1));

            // See earlier comments above TextView tv1 for how this works
            TextView text2 = (TextView) findViewById(R.id.tv2);
            text2.setText(descrip[index]);

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }

    });
}

}

Upvotes: 1

Views: 3173

Answers (2)

VishalKale
VishalKale

Reputation: 798

You can use the position you get into the OnClickListener of Spinner to get the particular Leader and Description of the that states as follows. text.setText(leader[position]); text2.setText(descrip[position]);

Upvotes: 0

Android - programatically adding text from strings.xml to TextView -

If I understood you right?

 Resources resources = context.getResources();

 String[] textString = resources.getStringArray(R.array.mytext);

Then you have the text in a string-vector - easy to put in a textView

Then in the xml-file

 <string-array name = "mytext"> 
    <item> ... text </item>
    <item> ... text</item>
    <item> ... text</item>
 </string-array>

And in the textView-object

text.setText(textString[i]); // at position i

Upvotes: 1

Related Questions