CHAPTER 5
MANIPULATING
THE
JAVA CODE
Understanding strings.xml
In the previous , example we changed the caption of TextView
by directly assigning the string “I am learning android” to the
text property of TextView. This is called hard-coding the
string
Although it works , but Google does not recommends this
approach.
Rather it advises us placing strings into a separate file and
then referencing them as it makes localization easy.
Understanding strings.xml
Every project includes a default strings file named as
strings.xml.
It is used to create string-values (called as string resources)
and then we can refer to these strings in our
activity_main.xml file as well as Java code.
Opening String Resources
In the Project tool window, find
the app/res/values directory, reveal its contents, and
open strings.xml.
This is the name
It’s default contents are : of the app
<resources>
<string name="app_name">MyFirstApplication</string>
</resources>
Creating String Resources
To create our own string resource , we can create a new
<string> tag with name=“message” as shown below:
<resources>
<string name="app_name">MyFirstApplication</string>
<string name=“message”> Android Rocks!</string>
</resources>
Using string resource in layout
Once we have created the string resource , we can now access
it in our layout file .
The syntax is “@string/xxx” where xxx is the name given to
the resource.
So in our case we would write it as :
<TextView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/message” />
The Output
Resources And Resource Id
As discussed earlier , a resource is a piece of our application
that is not code – things like image files, audio files, and XML
files.
Resources for our project live in a subdirectory of
the app/res directory.
Our strings.xml file, which contains string resources, lives in
res/values/.
Resources And Resource Id
Now many times we may want to access these resource from
our Java code.
Say for example , we want to change the title of our
application .
Now to do this we need to take 3 steps:
Create a string resource to be used as application title
Access this string resource in java code
Set it as the title
Step 1-
1- Creating A String
Resource
As previously discussed we create a new string resource in
strings.xml The backslash is necessary for
<resources> single quote otherwise error
will appear
.
.
.
<string name=“title”>Sachin\’s App</string>
</resources>
Step 2-
2- Accessing String
Resource In Java Code
Before we can access a resource in java , we need to
understand about a very important file called R.java.
This file is auto-generated by Android Studio in our project.
To view this file we need to open project view in the
project explorer window
Step 2-
2- Accessing String
Resource In Java Code
This file is in the following location : <application name>/
app/build/generated/source/r/debug.
In this directory, we can find our project’s package name
and open R.java within that package.
Step 2-
2- Accessing String
Resource In Java Code
The R.java file consists of all the resources that are used in the
XML files for strings, layout, drawables etc as static integer
constants.
For example in our code this file will be as shown below:
public final class R {
public static final class layout {
.
.
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int title=0x7f040001;
}
}
Step 2-
2- Accessing String
Resource In Java Code
As we can observer all the resource folders are inner classes
and their members as static final int variables.
Now we can pull these members in our java code by using
appropriate methods. For pulling strings the method is
getString( ) belonging to the , AppCompatActivity class and
whose prototype is : This is static field declared
inside the R.java file with the
public Sring getString(int resid) same name as our resource
So our call would be :
String s=getString(R.string.title);
Step 3-
3- Setting The Title
After we have obtained the string resource , we can now
pass it to the method setTitle( ) of AppCompatActivity class.
This method takes a string as argument and sets it as title of
our application
So our call would be :
String s=getString(R.string.title);
setTitle(s);
The Complete Code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String str=getString(R.string.myself);
setTitle(str);
}
}
The Output
Accessing Views In Java Code
The components which we create in our layout XML file
might need to be accessed in java code.
To do this we need to take 2 steps:
Assign an id to the component in XML
Call the method findViewByID( ) in java to access it
Providing Id To Views
To assign id to the view we use a property in our layout XML
file called android:id .
To set it the general syntax is:
android:id=“@+id/someid”
The syntax "@+id" tells Android to include the ID as a
resource in its resource file R.java.
We must include the "+" whenever we define a new view in the
layout. If we don’t, Android won’t add the ID as a resource and
we’ll get errors in your code.
Using The Method
findViewById(( )
findViewById
After creating views(gui components) in XML file, we
frequently need to refer to them in our java code to
manipulate their properties or for other tasks.
This can be done using the method findViewById()
provided by the Activity class
Using The Method
findViewById(( )
findViewById
It’s prototype is:
public View findViewById (int id)
The argument passed to this method is id of the component as
specified in R.java.
For example if we have a TextView with id “t1” set in XML file
then the call would be:
findViewById(R.id.t1);
Using The Method
findViewById(( )
findViewById
The return value of findViewById() is a reference to the
specified view if it is found , otherwise it returns null.
Moreover since return type of the method is View we
need to downcast it to specific component.
So the complete call would be:
TextView tv;
tv=(TextView)findViewById(R.id.t1);
EXERCISE
Design an app to display current date and time in a
TextView.
SOLUTION(activity_main
SOLUTION( activity_main))
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent“
.
.>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</RelativeLayout>
SOLUTION(MainActivity.java
SOLUTION( MainActivity.java))
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv=(TextView)findViewById(R.id.textView);
Date today=new Date();
tv.setText(today.toString());
}
}
OUTPUT
End Of Lecture
Call us @ : 0755-4271659, 7879165533
Agenda for Next Lecture:
1. Adding Button To The Layout
2. Positioning Attributes
3. Introduction To Event Handling
4. Handling Click Event