Lab2 EditText TextView Button
Lab2 EditText TextView Button
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:
@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 ;
}
});
}
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.
Page 7