Upload
ismael-tlapaya-tepech
View
230
Download
0
Embed Size (px)
Citation preview
8/12/2019 Android View
1/25
1Android BUAP - FCC MALCH
Android View
Dr. Miguel Angel Len Chvez
8/12/2019 Android View
2/25
2Android 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. Aview 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.
8/12/2019 Android View
3/25
3Android 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 toapplication 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.
8/12/2019 Android View
4/25
4Android BUAP - FCC MALCH
User interface
! The graphical user interface for an Android app isbuilt using a hierarchy of View and ViewGroup
objects.
! View objects are usually UI widgets such asbuttons or text fields
! ViewGroup objects are invisible view containersthat define how the child views are laid out, such
as in a grid or a vertical list
8/12/2019 Android View
5/25
5Android BUAP - FCC MALCH
UI
8/12/2019 Android View
6/25
6Android BUAP - FCC MALCH
UI
! Android provides an XML vocabulary thatcorresponds to the subclasses of View and
ViewGroup so you can define your UI in XML
using a hierarchy of UI elements.
8/12/2019 Android View
7/25
7Android BUAP - FCC MALCH
UI
8/12/2019 Android View
8/25
8Android BUAP - FCC MALCH
UI
! LinearLayout is a view group (a subclass ofViewGroup) 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 thelayout, it should fill the entire screen area that'savailable to the app by setting the width and height to
"match_parent".
8/12/2019 Android View
9/25
9Android BUAP - FCC MALCH
Add a text field
8/12/2019 Android View
10/25
10Android BUAP - FCC MALCH
android:id
! Unique identifier for the view, to reference the object fromyour app code, such as to read and manipulate the object.
! The at sign (@) is required for referring to any resourceobject from XML. It is followed by the resource type (idin this case), a slash, then the resource name(edit_message).
! The plus sign (+) before the resource type is needed onlywhen 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.
8/12/2019 Android View
11/25
11Android 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.
8/12/2019 Android View
12/25
12Android BUAP - FCC MALCH
Add string resources
My First AppEnter a messageSendSettingsMainActivity
8/12/2019 Android View
13/25
13Android BUAP - FCC MALCH
Add string resources
! String resources allow you to manage all UI textin a single location, which makes it easier to find
and update text.
! By default, your Android project includes a stringresource file at res/values/strings.xml.
8/12/2019 Android View
14/25
14Android BUAP - FCC MALCH
Add a button
8/12/2019 Android View
15/25
15Android BUAP - FCC MALCH
8/12/2019 Android View
16/25
16Android BUAP - FCC MALCH
Respond to the Send Button
8/12/2019 Android View
17/25
17Android BUAP - FCC MALCH
Respond to the Send Button
! MainActivity classimport android.view.View;
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
8/12/2019 Android View
18/25
18Android BUAP - FCC MALCH
Build an Intent
! An Intent is an object that provides runtime binding betweenseparate 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
8/12/2019 Android View
19/25
19Android 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);
}
8/12/2019 Android View
20/25
20Android 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 APIsif (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);
}
}
8/12/2019 Android View
21/25
21Android BUAP - FCC MALCH
Add it to the manifest
...
8/12/2019 Android View
22/25
22Android BUAP - FCC MALCH
Receive the Intent
! In the DisplayMessageActivity classs onCreate()method
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
8/12/2019 Android View
23/25
23Android 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);
}
8/12/2019 Android View
24/25
24Android BUAP - FCC MALCH
Android App
8/12/2019 Android View
25/25
25Android BUAP - FCC MALCH
Referencias
! Android Developer Website: The Android SDK and developerreference site: http://developer.android.com/
! http://developer.android.com/training/basics/firstapp/starting-activity.html#StartActivity