Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views

Lab2 EditText TextView Button

The document provides a tutorial on building a basic Android application using XML, focusing on creating an interface with EditText and TextView components. It explains the structure of an Android project, the purpose of various folders, and how to handle user input through event listeners. Additionally, it outlines key components of Android applications and their lifecycle, along with exercises to reinforce learning.

Uploaded by

haiyenvn22
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab2 EditText TextView Button

The document provides a tutorial on building a basic Android application using XML, focusing on creating an interface with EditText and TextView components. It explains the structure of an Android project, the purpose of various folders, and how to handle user input through event listeners. Additionally, it outlines key components of Android applications and their lifecycle, along with exercises to reinforce learning.

Uploaded by

haiyenvn22
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Week 3: A brief introduction to the basic components of Android

and the use of XML to develop Android applications


Requirements: Build an application that allows you to type 1 content and then display
that content below.
B1: Initialize a project.
File -> New -> Android Application Project tab . If you are new to Android
programming for the first time, perhaps the Android Project line will not appear, then
go to the bottom and select Other and then go to Android -> Android Application
Project .
B2: Fill in the information for the project.

Page 1
Step 3: In the Package Explore pane on the left, go to the res folder, you will see the
following folders:

- drawable: folder containing images to make icons or resources for the interface...
- layout: contains xml files for interface design.
- values: contains the values used in the application are defined, such as character
lines (strings), colors (colors), themes...
Step 4: Go to the layout folder, select the main.xml file and type the following code
instead of all available content:
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android =
"http://schemas.android.com/apk/res/android"
xmlns:tools = "http://schemas.android.com/tools"
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:paddingBottom = "@dimen/activity_vertical_margin"
android:paddingLeft = "@dimen/activity_horizontal_margin"
android:paddingRight = "@dimen/activity_horizontal_margin"
android:paddingTop = "@dimen/activity_vertical_margin"
tools:context = "com.example.vidu1.MainActivity" >
< EditText
android:id = "@+id/edit_text"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:hint = "@string/edit_hint" />
< TextView
android:id = "@+id/text_view"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:textColor = "@color/text_color"
android:textSize = "28px"
android:typeface = "monospace" />
</ RelativeLayout >

Page 2
In this XML fragment, declare a Linear Layout with its two children, an EditText
(used to type a string) and a TextView (displaying a string).
orientation keyword to indicate that the arrangement of the two child
components is vertical. As for layout_width, layout_height can be given a value of
"fill_parent" or "wrap_content" to indicate that this element will have the width
(length) to cover the parent element or just fit the content. In EditText and TextView
can be seen the id keyword, this keyword allows to declare the id of the elements to
get in the code (will be mentioned later). In addition, the hint keyword in EditText
allows to show the blurred content when Edit.
Text has no characters yet. The "@string/edit_hint" message gets in the
strings.xml file a string named edit_hint. As for the textColor of the Text View, the
text message will be displayed with the color taken in the colors.xml file, the textSize
indicates the font size is 28 pixels and the typeface indicates the font is monospace.
Step 5: Still in the res folder, go to values and select the strings.xml file. Add a
definition line for edit_hint as follows:
<? xml version = "1.0" encoding = "utf-8" ?>
< resources xmlns:android = "http://schemas.android.com/apk/res/android" >
< string name = "app_name" > ViDu1 </ string >
< string name = "hello_world" > Hello world! </ string >
< string name = "action_settings" > Settings </ string >
< string name = "edit_hint" > Enter the string to display </ string >
</ resources >
B6: In the values folder, create a colors.xml file (right click on the folder, select New
> Android XML File , and note the letter s, not color.xml). Enter the contents of the
file as follows:
<? xml version = "1.0" encoding = "utf-8" ?>
<resources> _ _
< color name = "text_color" > #ff3300 </ color >
</ resources >

OK, so created a new color for the text to be displayed in Text View (ff3300 is the
hexadecimal code for red). In fact, it is completely possible to type straight.
android:textColor="#ff3300"
In the main.xml file, there is no need to create a new colors.xml file, but the purpose
of XML in Android is to support easy editing. If you want to change the color of the
text in the future, just go to colors.xml and change it instead of fumbling around in
main.xml (it can be very long if the interface is complicated).
The above components are just the basic parts of XML. In addition, you can declare
more about Animation, Style and Theme.
B7: So we've finished the interface with XML, now it's time to write the code to
handle events for the components:

Page 3
=> go to the src folder (source code of the project)
=> com.example.vidu1
=> MainActivity.java, type the following code content:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
// Set create deliver face take from file main.xml
setContentView(R.layout. activity_main );
// Get about the wall part in main.xml through via id
final EditText edit = (EditText) findViewById(R.id. edit_text
);
final TextView text = (TextView) findViewById(R.id. text_view
);
// Set create judge physical give events to sue press knot
between belong to electricity phone
edit.setOnKeyListener( new View. OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if ( keyCode == KeyEvent. KEYCODE_ENTER ) {
if (event.getAction() == KeyEvent. ACTION_UP ) {
text .setText( edit .getText().toString());
edit .setText( "" );
} return true ;
} return false ;
}
});
}

Go through a little basic knowledge:


In Android, the classes used to create interfaces (Edit Text, Text View...) are all
subclasses of the View class. Some classes are frequently used to create interfaces:
- TextView
- EditText
- ListView
- Spinner
- CheckBox
- Button
- RadioButton
You can also create your own View by inheriting an existing View.
Listeners are used to catch an event. Here I use OnKeyListener to catch events
when pressing a key of the phone. Also often use OnClickListener to catch touch
event 1 View is showing on the screen. Each View must set its own Listener to handle
the event that interacts with it, and each View type also has its own Listener (eg
Page 4
CheckBox has OnCheckChangedListener) Here I use inner form function to define
handling handle for OnKeyListener.
Please note the R.id.edit_text section. To get or access the elements we have defined
in the XML we have to use R.* like R.layout.main, R.id.edit_text. The findViewById
command will return a View with the Id set in the XML section. Because View is a
superclass of EditText with TextView, here we have to force the style. In addition,
strings or colors can also be retrieved using the getResource() command.
Ex: getResource().getColor(R.color.text_color)
B8: Run the program. Select Run => Android Application and wait for the emulator
to start. Anyone with a real Android can connect via USB and test it out. Edit yourself
in the code and in the XML to understand more about Android programming.

If keypress does not work, check if ADB is enabled. Android Debug Bridge (adb) is a
flexible command line tool that allows communication with the Emulator or
connected Android device.

Page 5
Understanding Android Applications:
Understanding the components that make up an Android application is essential for
programming. These ingredients are divided into 6 categories including:
1. Activity: In simple terms, Activity is the background of an application. When
starting an Android application, there is always a main Activity called, displaying the
application's interface screen to allow users to interact.
2. Service: component that runs in the background in Android. Service used to update
data, give warnings (Notification) and never show it to the user.
3. Content Provider: shared data store. Content Providers are used to manage and
share data between applications.
4. Intent: the platform for transmitting messages. Intent is used to send out messages
to initiate an Activity or Service to do the desired job. For example, when opening a
web page, send an intent to create a new activity that displays that web page.
5. Broadcast Receiver: component that receives incoming external Intents. For
example, write a program to replace the default Android calling, then 1 BR is needed
to recognize Intents as incoming calls.
6. Notification: issue alerts without causing the Activity to stop working.
New Activity, Service, Broadcast Receiver and Content Provider are the main
components of an Android application, which must be declared in the
AndroidManifest.
Android Application Life Cycle:
Android has a mechanism to manage processes according to priority mode.
Processes with low priority will be released by Android without warning to preserve
resources.
1. Foreground process: is the process of the current application being
interacted by the user.
2. Visible process: is the process of the application that the activity is visible to
the user (onPaused() of the activity is called).
3. Service process: the Service is running.
4. Background process: is the process of the application that its activities are
not visible to the user (onStoped() of the activity is called).
5. Empty process: the process does not have any active components. Under
priority mode, when resources are needed, Android will automatically kill processes,
first empty processes.

Page 6
Exercises :
1. Build a program to calculate the sum, difference, product, and quotient of two
numbers
2. Build a program to check prime numbers.
3. Let's build an app that calculates Body Mass Index
- C stands for BMI ( Body Mass Index )
- Used to assess how thin or fat a person is. This index can help determine whether a
person is obese or malnourished.

The calculation is as follows:


Let W be the mass of a person (in kg) and H the height of the person (in m), body
mass index is calculated by the formula:

The classification for evaluation is as follows:


 BMI < 18: skinny person
 BMI = 18 – 24.9: normal person
 BMI = 25 – 29.9: obese people grade I
 BMI = 30 – 34.9: obese people grade II
 BMI > 35: obese people grade III

Page 7

You might also like