Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
171 views13 pages

Graphical User Interface in Android: Mobile Device Application

This document discusses creating user interfaces in Android applications. It covers: 1) There are two ways to create UIs - using XML layout files or programmatically in code. UIs are built using View and ViewGroup objects like buttons, text fields, and layouts. 2) Common layouts include RelativeLayout and TableLayout, which contain views like buttons and text fields. Layouts can be nested. 3) Event handling uses interfaces like OnClickListener to define code that runs when events occur, like a button click. The document provides examples of creating activities with layouts, adding click listeners to buttons, and using intents to start new activities. It also covers best practices for sizing

Uploaded by

lengsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
171 views13 pages

Graphical User Interface in Android: Mobile Device Application

This document discusses creating user interfaces in Android applications. It covers: 1) There are two ways to create UIs - using XML layout files or programmatically in code. UIs are built using View and ViewGroup objects like buttons, text fields, and layouts. 2) Common layouts include RelativeLayout and TableLayout, which contain views like buttons and text fields. Layouts can be nested. 3) Event handling uses interfaces like OnClickListener to define code that runs when events occur, like a button click. The document provides examples of creating activities with layouts, adding click listeners to buttons, and using intents to start new activities. It also covers best practices for sizing

Uploaded by

lengsan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 13

Mobile Device Application

Graphical User Interface in Android

1
Creating User Interface in Android
2 ways to create
In XML
In code
UI built using the View and ViewGroup objects
View class is the base for subclasses called Widgets like
text fields (EditText), labels (TextView) and buttons
ViewGroup class is the base for subclasses called
Layouts like LinearLayout, TableLayout and
RelativeLayout

2
Layouts
Most phone screens are 480 by 800 pixels (medium
density, HVGA)
The 2 most flexible and common layouts are
RelativeLayout
TableLayout
Within the layout are the various views (widgets)
such as TextView, Button, EditText (text field)
Layouts can also be nested within layouts

3
activity_main.xml
This is the default layout file in the res/layout folder
You can create many more layout xml files and put them
in the res/layout folder
To use a layout, the setContentView() method must be
invoked in the code for that particular xml file e.g.
setContentView(R.layout.activity_main);
setContentView(R.layout.another); //another.xml

4
5
Referencing view objects in code
Each view has an ID associated with it e.g.
<Button android:id="@+id/my_button" />
@ is used by the XML parser to identify the string as an
ID resource
+ means that the ID resource must be created and
added to the R.java file
In the code, the button resource is referenced as follows:
Button btn1 = (Button) findViewByID(R.id.my_button);

6
7
MainActivity.java
ImageButton imageButton = (ImageButton)findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener() {

8
Size in UI
As far as possible, absolute pixel values should not be
used so that automatic scaling can be done when the
device is moved from the portrait to the landscape view
and vice versa
Values should be in dip (dp)
density independent pixel
Font size can use
sp (scale independent pixel)

9
Event Handling
Event listener is an interface in the View class
For button, the listener OnClickListener will have the
onClick method which you will need to override
// Register and implement the listener anonymously
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}//end onClick

});//end of setOnClickListener 10
Lab 7
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imageButton =
(ImageButton)findViewById(R.id.imageButton1);
imageButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent msg1 = new Intent(MainActivity.this,
SecondActivity.class);
startActivity(msg1);
}
});
}
} 11
Lab 7
public class SecondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = (Button)findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent msg2 = new Intent(SecondActivity.this,
MainActivity.class);
startActivity(msg2);
}
});
}
}
12
Review Question 1
Which of the following is NOT activated by intents?

A. Activities
B. Services
C. Content Providers
D. Broadcast Receivers

Refer to previous lecture if necessary

13

You might also like