Final say on layout!

Posted on Updated on

Hello all!

So, it’s time to culminate our initial discussion on layouts. Till now, we have looked at the WYSIWYG editor, the code for the layout and a visual editor for playing with strings. Now, we shall come to the query raised in the last post, i.e., the value taken by the android:string attribute of the TextView which is described in the line:

android:text=”@string/niktechs” 

For those who have been following this blog, it must be really easy now. But, still I should give the details for the sake of completeness.

Clearly, @string is a way to tell the compiler that the variable which will follow after a ‘/’ represents a string. This is necessary to distinguish the different types of variables. Yes! There are other kinds of variables which represent color, dimension, array, etc. Right now, I am not going to discuss how to use those variables. But, if you are one of those who really like to stay ahead, open the strings.xml and click on add. You will see the following dialog box.

Now, it’s on you to explore!

Also, the name that follows the ‘/’ is the name of the variable whose value is to be given to the android:text and thus, is to be displayed on the screen. Here, since the name is niktechs, the text that is displayed on the screen will be same as the value of the variable niktechs which is Developing Android Applications with Nikhil Gupta.

Now, I think I have laid the foundation of discussing the other way of changing the text on the screen. Remember, in the post titled Laying out the screen!, we saw how to use WYSIWYG to change the text on the screen. For a quick flashback, we used the edit text option and created a new R.string whose value was the text that we wanted to display.

The other way results from our discussion in the previous post. There, I created a new variable of type string named Blog. Now, instead of using the WYSIWYG editor, we can simply change niktechs (in the code describing android:text attribute) to Blog and save the file.

Now, look at the GUI (click on main.xml and then, choose Graphical Layout tab). What do you see? Yes, the text has been changed to Android Development which is the value of the variable Blog. Why does this happen? I hope that it’s now trivial for all. Since Blog is just another string type variable like niktechs (discussed in the previous post), replacing the two will result in replacing the values of the android:text attribute and hence, the text on the screen.

So, we have looked at the basic layout features and understood each and every concept related to it. But there are various attributes that can be used in the code for layout which can greatly vary the screen. It’s up to you to explore them. I may discuss a few of them, but only in the later posts. If you want to explore, go to a tag, type android: and press Ctrl+Space. You will see the entire options that are possible.

In the next few post, we will finally go into the JAVA coding. So, a lot more interesting stuff coming soon!

Till next time, BYE!

Advertisements

2 thoughts on “Final say on layout!

    Prateek Sahu said:
    May 18, 2012 at 12:32 am

    i was trying out a simple app.i came across this problem while using the GUI.the error was in graphical layout.whenever i used a widget like button or checkbox(as i have in the picture) , the android:text=”Checkbox” gives an error:[18N]Hardcoded string “checkbox”,should use @string resource”.for this reason i even tries doing it to android:text=”@string/Button” for the button widget(also in picture) but the error pertained.i havent changed the code anything more.these are default generated code when i put a widget on the screen.please help.
    https://picasaweb.google.com/100411635904159755222/AndriodProgrammingErrors#5743576605504196514

      Nikhil Gupta responded:
      May 18, 2012 at 12:38 am

      Just make a new string variable whose name is Checkbox1 using the tutorial provided in the previous post. Then, simply set the value of android:text to “@string/Checkbox1”. Hope it helps!

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