Strings in an Android App!

Posted on Updated on

Hi everyone!

In the last post, we looked at the code for the layout of an android application. But we didn’t discuss discuss the strange value taken by the text attribute of the tag “TextView” and how it decides the text display by this TextView. Were you able to find any correlation?

In this post, I will discuss about strings in an android app. The concepts discussed in this post will be used to anwer the above query in the next post. So, let’s start!

Click on the values folder (It’s in the same directory as the layout folder). Do you find something which is related to our interest? Yes, a file named string.xml. Let’s open it! What do you see?

You can see that strings.xml opens up in an interactive editor. This editor is used to add Resources (to be discussed later) to our android application. For now, it’s sufficient to know that strings are treated as resources in an android app and hence, this editor allows us to make, modify or delete strings.

We can see three Resource Elements (as specified in the header), all being of type String as mentioned in the brackets beside the name. Thus, our android application has three resources, all of them being strings. The names that are visible are the names of the variables that are used to store the string type values (similar to the fact that x is a variable used to store integer type values when I write int x = 10 in C). Thus, hello and app_name are two string type variables which are automatically created during the creation of a project.

To look at the values stored by these variables, simply click on them. On clicking hello, you will see a panel in the right which displays the name of the variable and it’s value. Now, looking at the value of the variable hello, I hope that you can make out how the text displayed at the screen originally came about. Yes!, the value of this variable is Hello World, HelloWorldActivity!, the original text on the screen.

You must have seen another line of text in the screen of the app. Look at the value of the variable app_name and try to find out the place at which it is used. Comment below if you are able to find it!

Now, do you see why a variable named niktechs is there in the list? How it was created? Do you remember, while editing the text on the screen (Refer to the post Laying out the screen) , we made a new string variable? Yes, that variable is the same as displayed here. The value given to the field R.string there, is the name of the variable and the value is the same as given that time. Thus, when we changed the text displayed at the screen, we actually created a new string variable and gave its value to the TextView.

As mentioned earlier, we can create a variable by using this editor. Simply click on Add. A dialog box opens up. Don’t be afraid to see so many options. We will look at them as we proceed. For now, simply select String. In the resulting window, simply set the name (let’s set it to Blog) and the value (I am setting it to Android Development) as required and save the file. This creates a new String type variable.

We can modify a string by simply changing its value and saving the file. To delete a variable, simply select Remove. 

Thus, we have seen how to play with strings in an Android Application. What is left to discuss is how to use these strings in the app. How these variables relate to the text displayed on the screen? How it answers the query raised at the beginning of the post?

All of this coming in the next post!

Till then, BYE!

Advertisements

One thought on “Strings in an Android App!

    Rishabh said:
    May 25, 2012 at 4:22 pm

    “Comment below if you are able to find it!”
    i found everything 🙂

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s