Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Development: Where to put static Key/Value Pairs?

Tags:

android

I have a list of static key/value pairs that I need to include in my project like this one:

givenName : First Name
sn        : Last Name
mail      : Email
... snip ...

Where in an Android project would I put this?

Thanks Eric


1 Answers

You need a XML file saved at res/values/strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="givenName">First Name</string>
    <string name="sn">Last Name</string>
    <string name="mail">Email</string>
</resources>

Here is how you can access the values from other xmls:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/givenName" />

Or this is how you can access the value from Java code:

String string = getString(R.string.givenName);
Log.d("Test", string); // Outputs "First Name" to LogCat console.

Check this Android Dev Guide for full reference on String Resources.

like image 67
pXel Avatar answered May 05 '26 00:05

pXel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!