Vous êtes sur la page 1sur 25

1 Android BUAP - FCC MALCH

Android View
Dr. Miguel Angel Len Chvez
2 Android BUAP - FCC MALCH
Android big picture
! View
Views are user interface (UI) elements that form the
basic building blocks of a user interface. Views are
hierarchical and they know how to draw themselves. A
view could be a button, label, text field, or other UIs.
! Activities
The building block of the user interface is the activity.
! Content Providers
They provide a level of abstraction for any data stored
on one deve that is accessible by multiple applications.
3 Android BUAP - FCC MALCH
Android big picture
! Intents
Intents are system messages, running around the inside
of the device, notifying applications of various events,
from hardware state changes to incoming data to
application events
! Services
Activities, content providers, and intent receivers are all
short-lived and can be shut down at any time. Services,
on the other hand, are designed to keep running, if
needed, independent of any activity.
4 Android BUAP - FCC MALCH
User interface
! The graphical user interface for an Android app is
built using a hierarchy of View and ViewGroup
objects.
! View objects are usually UI widgets such as
buttons or text fields
! ViewGroup objects are invisible view containers
that define how the child views are laid out, such
as in a grid or a vertical list
5 Android BUAP - FCC MALCH
UI
6 Android BUAP - FCC MALCH
UI
! Android provides an XML vocabulary that
corresponds to the subclasses of View and
ViewGroup so you can define your UI in XML
using a hierarchy of UI elements.
7 Android BUAP - FCC MALCH
UI
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal" >
</LinearLayout>
8 Android BUAP - FCC MALCH
UI
! LinearLayout is a view group (a subclass of
ViewGroup) that lays out child views in either a
vertical or horizontal orientation, as specified by the
android:orientation attribute.
! Each child of a LinearLayout appears on the screen in
the order in which it appears in the XML.
! Because the LinearLayout is the root view in the
layout, it should fill the entire screen area that's
available to the app by setting the width and height to
"match_parent".
9 Android BUAP - FCC MALCH
Add a text field
<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
10 Android BUAP - FCC MALCH
android:id
! Unique identifier for the view, to reference the object from
your app code, such as to read and manipulate the object.
! The at sign (@) is required for referring to any resource
object from XML. It is followed by the resource type (id
in this case), a slash, then the resource name
(edit_message).
! The plus sign (+) before the resource type is needed only
when you're defining a resource ID for the first time.
When you compile the app, the SDK tools use the ID
name to create a new resource ID in your project's gen
/R.java file that refers to the EditText element.
11 Android BUAP - FCC MALCH
Add a text field
! wrap_content
This value specifies that the view should be only as big
as needed to fit the contents of the view.
! android:hint
This is a default string to display when the text field is
empty.
12 Android BUAP - FCC MALCH
Add string resources
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message<
/string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity<
/string>
</resources>
13 Android BUAP - FCC MALCH
Add string resources
! String resources allow you to manage all UI text
in a single location, which makes it easier to find
and update text.
! By default, your Android project includes a string
resource file at res/values/strings.xml.
14 Android BUAP - FCC MALCH
Add a button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
15 Android BUAP - FCC MALCH
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
16 Android BUAP - FCC MALCH
Respond to the Send Button
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
17 Android BUAP - FCC MALCH
Respond to the Send Button
! MainActivity class
import android.view.View;
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
18 Android BUAP - FCC MALCH
Build an Intent
! An Intent is an object that provides runtime binding between
separate components (such as two activities).
! The Intent represents an apps "intent to do something.
! An intent not only allows you to start another activity, but it
can carry a bundle of data to the activity as well.
Inside the sendMessage() method:
Intent intent = new Intent(this, DisplayMessageActivity.class);
Parameters:
A Context as its first parameter (this is used because the Activity class is a subclass of
Context)
The Class of the app component to which the system should deliver the Intent
19 Android BUAP - FCC MALCH
Start the Second Activity
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this,
DisplayMessageActivity.class);
EditText editText = (EditText)
findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
20 Android BUAP - FCC MALCH
Create the Second Activity
public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
21 Android BUAP - FCC MALCH
Add it to the manifest
<application ... >
...
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" /> <
/activity>
</application>
22 Android BUAP - FCC MALCH
Receive the Intent
! In the DisplayMessageActivity classs onCreate()
method
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
23 Android BUAP - FCC MALCH
Display the Message
@Overridepublic void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Intent intent = getIntent();
String message =
intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
24 Android BUAP - FCC MALCH
Android App
25 Android BUAP - FCC MALCH
Referencias
! Android Developer Website: The Android SDK and developer
reference site: http://developer.android.com/
! http://developer.android.com/training/basics/firstapp/starting
-activity.html#StartActivity

Vous aimerez peut-être aussi