Events (4 Files Merged)
Events (4 Files Merged)
What is a Widget?
Widget: ImageView
Widget: CheckedTextView
2
Android: Where are we now …
3
Android: Views objects
GoogleMap
WebView
Widgets topic of the day
…
User-defined Views
4
Android: Views objects
TextView
EditText
Button
ToggleButton
Spinner
DataPicker
6
Widgets: Java and XML code
< TextView
android:id="@+id/textLabel”
android:width=“100dp”
android:height=“100dp”
android:layout_width="match_parent"
android:layout_height="wrap_content”
android:visibility="visible”
android:enabled="true”
android:scrollbars="vertical”
….
/>
(c) Luca Bedogni 2012
7
Widgets: Java and XML code
XML
< TextView
android:id="@+id/name1” />
text=(TextView)findViewById(R.id.name1);
CAST REQUIRED
public TextView text;
text=new TextView();
(c) Luca Bedogni 2012
8
Widgets: Java and XML code
ONCLICK event
View
TextView ImageView
AutoCompleteTextView CompoundButton
<TextView
android:text=”@string/textWelcome"
android:id="@+id/textLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content”
/>
12
Widgets: TextView methods
<EditText
android:text=”@string/textDefault"
android:id="@+id/editText”
android:inputType= “textCapSentences” | “textCapWords” |
“textAutoCorrect” | “textPassword” |
“textMultiLane” | “textNoSuggestions”
/>
15
Widgets: AutocompleteTextView
String[] tips=getResources().getStringArray(R.array.nani_array);
ArrayAdapter<String> adapter=new ArrayAdapter(this,
android.R.layout.simple_dropdown_item_1lines, tips);
AutoCompleteTextView acTextView=(AutoCompleteTextView)
findViewById(R.id.inputText);
acTextView.setAdapter(adapter);
<selector>
<Button <item android:color="#ff819191"
android:text="@string/textButton" android:state_pressed="true”>
android:id="@+id/idButton" </item>
android:background="@color/blue” </selector>
/>
res/color/blue.xml
checkBox CompoundButton
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonCheck"
android:text="CheckBox"
android:checked="true”
/>
checkBox CompoundButton
public boolean
setChecked(bool)
Listener:
onCheckedChangeListener
radioButton CompoundButton
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonRadio"
android:text="ButtonRadio"
android:checked="true"
/>
radioButton CompoundButton
Listener:
OnCheckedChangeListener
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical”>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonRadio1"
android:text="Option 1"
android:checked="true” />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonRadio2"
android:text="Option 2” />
</RadioGroup>
toggleButton CompoundButton
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/toggleButtonId"
android:textOn="Button ON"
android:textOff="Button OFF"
android:checked="false”
/>
(c) Luca Bedogni 2012
24
Widgets: Button and CompoundButton
toggleButton CompoundButton
Listener:
OnCheckedChangeListener
Spinner component
Spinner component
DataPicker component
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content”
android:id=“@+id/datePickerId”
android:endYear="1990"
android:startYear="2014"
android:maxDate="10/10/2014”
/>
ImageView component
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageId"
android:src="@drawable/android">
Methods:
void setChoiceMode(int choiceMode)
long[] getCheckItemIds()
int getCheckedItemPosition()
<Button
android:text="@string/textButton"
android:id="@+id/idButton" XML Layout File
android:onClick=”doSomething”
/>
@Override
public class ExampleActivity extends Activity implements OnClickListener {
… void onClick(View view) {
public
Button button=(Button)findViewById(R.id.buttonNext);
// Event management
button.setOnClickListener(this);
…
} public void onClick(View v) { } }
});
36
Views and Events: ActionListener
37
Views and Events: ActionListener
Some ActionListeners:
interface OnClickListener
abstract method: onClick()
interface OnLongClickListener
abstract method: onLongClick()
interface OnFocusChangeListener
abstract method: onFocusChange()
interface OnKeyListener
abstract method: onKey()
Some ActionListeners:
interface OnCheckedChangeListener
abstract method: onCheckedChanged()
interface OnItemSelectedListener
abstract method: onItemSelected()
interface OnTouchListener
abstract method: onTouch()
interface OnCreateContextMenuListener
abstract method: onCreateContextMenu()
39
Views and Events: ActionListener
Action Name
Data
Structure
of an Intent Category
Extra Flags
startActivity(intent);
Explicit Intent:
• To send parameter from activity to another one use :
intent.putExtra(“KEY”, VALUE);
Ex:
intent.putExtra(“grade”,75);
intent.putExtra(“name”,”Ali”);
intent.putExtra(“grade”,true);
To retrieve the parameters inserted from the calling
Activity
intent.getExtras().getTYPE(“KEY”);
Ex:
intent.getExtras().getInt(“grade”);
intent.getExtras().getString(“name”);
intent.getExtras().getBoolean(“grade”);
Explicit Intents
Activity 1 Activity 2
Explicit Intents
Activity 1 Activity 2
Explicit Intents
startActivity(intent);
Implicit Intent: Examples
Intent intent=new
Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if
(intent.resolveActivity(getPackageManager
()) != null) {
startActivity(intent);
}
Implicit Intent: Action Examples
Data passed from the caller to the called Component.
Def. of the data (URI) and Type of the data (MIME type)
Uniform Resource Identifier (URI)
1. void setData(Uri)
2. void setType(String)
Implicit Intent: Examples
Intent intent=new
Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
Intent intent=new
Intent(Intent.ACTION_VIEW);
intent.setData(
Uri.parse("content://contacts/people/1"));
startActivity(intent);
Implicit Intent: Examples
Intent intent=new
Intent(Intent.ACTION_VIEW);
intent.setData(
Uri.parse(”http://www.cs.unibo.it"));
startActivity(intent);
Implicit Intent: Type
type/subtype
void addCategory(String)
startActivity(intent);
Implicit Intent Intent Resolution
<intent-filter>
<action android:name=”ACTION_ECHO” />
</intent-filter>
The Intent resolution process resolves the
Intent-Filter that can handle a given Intent
(e.g. ACTION_ECHO).
<intent-filer … >
<data android:mimeType=“audio/*
android:scheme=“http”/>
<data android:mimeType=“video/mpeg
android:scheme=“http”/>
</intent-filter>
What is a resource?
Declaration of a resource
2
Application Resources Definition
3
Application Resources Definition
4
Application Resources Definition
5
Application Resources Definition
EXAMPLE
- Build the application layout
through XML files (like HTML)
- Define two different XML layouts
for two different devices
Device 1 Device 2 - At runtime, Android detects the
HIGH screen pixel density LOW screen pixel density
current device configuration and
Java App Code loads the appropriate resources
for the application
- No need to recompile!
- Just add a new XML file if you
need to support a new device
XML Layout File XML Layout File
Device 1 Device 2
8
Application Resources Definition
LAYOUT LAYOUT
SMARTPHONE TABLET
9
Application Resources Definition
MyProject
res
10
Application Resources Definition
11
Application Resources Definition
</resources>
12
Application Resources Definition
13
Application Resources Definition
…
final String hello=getResources().getString(R.string.hello);
final String label=getResources().getString(R.string.labelButton);
Log.i(STRING_TAG,” String1 “ + hello);
Log.i(STRING_TAG,” String2 “ + label);
…
…
14
Application Resources Definition
15
Access to Application Resources
@[<package_name>:]<resource_type>/<resource_name>
17
Access to Application Resources: XML
18
Access to Application Resources: Java
[<package_name>.]R.<resource_type>.<resource_name>
19
Access to Application Resources: Java
20
Resources Types: string and array
</resources>
22
Resources Types: string and array
MYFILE.JAVA
23
Resources Types: string and array
24
Resources Types: string and array
25
Other Resources Types
26
Resources Types: color, dimension, style
27
Resources Types: color, dimension, style
</resources>
28
Resources Types: color, dimension, style
Code Description
px Pixel units
in Inch units
mm Millimeter units
pt Points of 1/72 inch
dp Abstract unit, independent from pixel density of a display
These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly
equal to 1px. When running on a higher density screen, the number of pixels used
to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise,
when on a lower density screen, the number of pixels used for 1dp is scaled down
29
Resources Types: color, dimension, style
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
<TextView
MAIN.XML
android:layout_height="@dimen/textview_height"
android:layout_width="@dimen/textview_width"
android:textSize="@dimen/font_size"/>
30
Resources Types: color, dimension, style
<dimen name="textview_height">25dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">16sp</dimen>
</resources>
<TextView
MAIN.XML
android:layout_height="@dimen/textview_height"
android:layout_width="@dimen/textview_width"
android:textSize="@dimen/font_size"/>
31
Resources Types: color, dimension, style
32
Resources Types: color, dimension, style
<EditText style="@style/CustomText"
MAIN.XML
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
33
Resources Types: drawable
35
Resources Types: drawable
Usage: (i) Alias to the raw bitmap file, (ii) Specifiy additional
properties such as dithering and tiling.
36
Resources Types: drawable
37
Resources Types: xml and raw
38
Resources Types: xml and raw
The res/xml folder might contain arbitrary XML files that can be read
at runtime through the R.xml.<filename> constant.
XMLResourceParser parser=getResources().getXML(R.xml.myfile)
39
Resources Alternatives
40
Resources Alternatives
res
41
Resources Alternatives: Qualifiers
43
Resources Alternatives Matching
44
Resources Alternatives Matching
DEVICE CONFIGURATION
drawable/
drawable-it/
drawable-fr-rCA/
drawable-it-port/
drawable-it-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
45
Resources Alternatives Matching
DEVICE CONFIGURATION
drawable/
drawable-it/
drawable-fr-rCA/
drawable-it-port/
drawable-it-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
46
Resources Alternatives Matching
DEVICE CONFIGURATION
drawable/
drawable-it/
drawable-fr-rCA/
drawable-it-port/
drawable-it-notouch-12key/
drawable-port-ldpi/
drawable-port-notouch-12key/
47
Resources Alternatives
BEST PRACTICE
Instructions ﺗﻌﻠﯿﻤﺎت
▪ This exam consists of (2) questions on 3 pages. ﺻﻔﺤﺎت3 ( أﺳﺌﻠﺔ ﻋﻠﻰ2) ھﺬا اﻻﺧﺘﺒﺎر ﻣﺆﻟﻒ ﻣﻦ ▪
▪ All questions should be answered on the same sheets
▪ Electronic devices that could have a memory is not
ﺟﺎوب ﻋﻠﻰ ﺟﻤﯿﻊ اﻷﺳﺌﻠﺔ ﻋﻠﻰ ﻧﻔﺲ اﻻوراق ▪
allowed for theoretical part ﯾﻤﻨﻊ اﺳﺘﻌﻤﺎل اﻻﺟﮭﺰة اﻻﻟﻜﺘﺮوﻧﯿﺔ اﻟﺘﻲ ﺗﺤﺘﻮي ﻋﻠﻰ ذاﻛﺮة ▪
▪ Traditional calculator is allowed ﻟﻠﺠﺰء اﻟﻨﻈﺮي
▪ Examination rules must be adhered. (ﯾﺴﻤﺢ ﺑﺎﺳﺘﻌﻤﺎل اﻻﻟﺔ اﻟﺤﺎﺳﺒﺔ )دون ذاﻛﺮة ▪
● Books or other related materials are NOT allowed
.ﯾﺠﺐ اﻻﻟﺘﺰام ﺑﺠﻤﯿﻊ ﻗﻮاﻧﯿﻦ وﻟﻮاﺋﺢ اﻻﻣﺘﺤﺎﻧﺎت ▪
● Use blue pen only
ﻻ ﯾﺴﻤﺢ ﺑﺎﻟﻜﺘﺐ أو اﻟﻤﻮاد ذات اﻟﺼﻠﺔ داﺧﻞ ﻏﺮﻓﺔ اﻻﻣﺘﺤﺎن ▪
اﻟﻜﺘﺎﺑﺔ ﺗﻜﻮن ﺑﺎﻟﻘﻠﻢ اﻷزرق ﻓﻘﻂ ▪
3. To access a string resource called ‘day’, in a package that’s called android in java
a) R.android.string.day
b) String.android.R.day
c) [email protected]
d) android.R.string.day
b- Match the terms from the numbered column with statements in the second
1. R class 5 Used as an Alias to raw image files, and to specify additional properties
too
2. Explicit intents 3 Has a set of attributes to apply on a specific UI component
3. Style 6 Launch an activity expecting results upon its closure
4. onActivityResult 1 Automatically generated file, recreated if res/ directory is modified
5. XMLBitmap 2 Controls the application flow between activities in the same application
6. startActivityForResult 4 Used to handle the results sent from second activity
a) The activities design must be like the ones in the figure 1 (4 marks)
Set the application name to Chat Application
Define all the labels in the app as resources.
b) In the first activity (left), if the user clicks the ‘send’ button, the text in the edit text must be sent
to the second activity. (3 marks)
Note Copy the XML and Java file for each activity and upload it to the blackboard
Eaxm2
<resources>
<string name="app_name">ChatApplication</string>
<string name="send">Send</string>
<string name="textInit">Nothing is sent yet</string>
<string name="editHint">Enter your message here .. </string>
<string name="answer">Answer</string>
<string name="share">Share</string>
</resources>
activity_main.xml
<TextView
android:id="@+id/received"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textInit"
/>
<EditText
android:id="@+id/sender"
android:hint="@string/editHint"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send"
android:text="@string/send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="send"/>
</LinearLayout>
MainActivity.java
package com.example.satalites55.chatapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.sender);
textView = findViewById(R.id.received);
activity_main2.xml
<TextView
android:id="@+id/sent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/receiver"
android:hint="@string/editHint"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="answer"
android:text="@string/answer" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="share"
android:text="@string/share" />
</LinearLayout>
Main2Activity.xml
package com.example.satalites55.chatapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;