Anda di halaman 1dari 11

Strings.

XML

Strings.xml is used to declare new strings, string array, and colours(color).
Item declared in strings folder can be called and use on all layout les globally.

Adding a new string

In the source code mode of the layout le, you can declare the words to be display on screen by
typing android:text=Your text here.
But when you save your layout le, a warning line will appear with a warning:
[Hardcoded string , should use @string resource]
There is 2 methods to overcome this warning.

Method 1: Adding string object manually.

Open strings.xml in values folder

Add the desired string by the following format

<string name=purple">Purple</string>

Info: name= - the string value to be called in your layout
>Yellow< - string to be display on the layout le.

Note: All strings are case sensitive.

Method 2: Adding strings through layout le

Same as above, words to be displayed is declare through android:text= Your text here.

Instead of declaring the strings manually, highlight the text within the double quote ( )

After highlighting the text, press they keyboard keys SHIFT + ALT + A



A window will appear at the lower right, select Extract Android String

OR

You can click CTRL+1(Windows) / CMD+1(Mac) to bring forward the quick x window and
select Extract String


A window will appear to prompt the name and the value of the string to be added.
Note:Usually we leave the string to have upper case and the R.string.value to be lower case.

Click Ok to add the following string to the strings.xml folder in the values folder.

To use the following string again in any of the layout folder again just type in as shown below:

android:text=@string/ Your lower case string value

Note: Make sure to save your project every time you add a new string to remove the warning sign.
If youre are sure that you have create the string value properly and the warning sign still
exist, go to Projects -> Clean. This will prepare your workspace again and check for
changes.

Using Relative Layout

In activity_main.xml le, add in 3 buttons to anywhere of the layout screen.

Change the textViews text from hello world to Colours. (remember to declare string)

Change textSize to 30sp

Add in a buttons to any part of the screen below the TextView

Change the buttons colour to the background similar to the background colour of screen2.xml

Change the button text according to the button colour.

Change the button ID to the buttonColour

eg: android:id=@+id/buttonYellow

Repeat the above steps to add in 2 more buttons but change the button colours to the
background colour of screen3.xml and screen4.xml respectively.


Enter source code mode, notice in relative layout, some buttons would be aligned to other objects
of the screen.

3 Linear Layout Screens (Lab2 exercise)

The objective to create this 3 screen is to create screen hoping from one screen to another.#
Screen2
Screen3
Screen4
SRC(source) folder

The source folder is where you save all your function codings for an android application.

Package - Acts like a folder for your source codes. A project can have multiple package folder.
Class le - Java coding to declare the functionality of the application. Files are in .java.

Adding a new class le into package
Right click com.example.screens -> New -> Class#
Package
Class les
A window will appear and type in screen2 as the name.
Click Finish and the newly created class le will appear on screen like this.
Repeat the steps to create class les for screen3 and screen4.
Changing background colour and text through class les

In the previous labs, you have learned how to change the background colour of layouts through the
layout les. Now we are going to change the background colour based on the colour of the button
we selected.

Open MainActivity.java
Declaring objects in the layout
In our activity_main.xml we have 1 TextView and 3 Buttons so we have to declare it in the class le
so we can use it. (Similar to declaring integers for a C le)

Type in the following after setContentView(R.layout.activity_main):

TextView Colour = (TextView) ndViewById(R.id.textView1);
Button Yellow = (Button) ndViewById(R.id.buttonYellow);
Button White = (Button) ndViewById(R.id.buttonWhite);
Button Grey = (Button) ndViewById(R.id.buttonGrey);
View view = this.getWindow().getDecorView();


Info: ndViewById - located the id of the object we declared in layout le.


Note: You may get warning signs and underline while declaring the objects.This is because you
have not import the library les to the class le from the SDK Library. You can do this by pressing
CTRL+SHIFT+O.#
Declaring the class le
is an activity
Creating a state to declare the
activity and dene its layout le
Declaring the activity allows Menu Options
To create a declaration of
the layout view
Creating Button Listeners when click (OnClickListener)

OnClickListener is androids way to declare what would happen to an object when it is being click.
A click is similar to a tap to the screen like how we use touchscreen keyboards.

Bonus Info: To do an action after a long hold of the object we use OnLongClickListeners.

Depending on the Button Colours you created and declared type in the following code:

Yellow.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

view.setBackgroundColor(Color.YELLOW);
Colour.setText("Yellow");
}

});


Note: To declare colours through HTML colour codes. You can add it into your string.xml le like
the following:
<color name=grey">#848583</color>

To use the change the background colour using the newly added colour from the resource folder,
we still use setBackgroundColor but instead of using a library colour, we will use
getResource().getColor(R.id)


Repeat the following to add all 3 buttons into MainActivity.java

Grey.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

view.setBackgroundColor(getResources().getColor(R.color.grey));
Colour.setText("Grey");
}
});
The Final Coding should be something similar to the following:

Anda mungkin juga menyukai