Download Android Layout Types

Document related concepts
no text concepts found
Transcript
ELET4133: Embedded Systems
Topic 15
Sensors
Agenda
•
•
•
•
9/27/2013
What is a sensor?
Different types of sensors
Detecting sensors
Example application of the accelerometer
2
What is a sensor?
• Piece of hardware that collects data from the
physical world, transforms it into useable data
and transfers it to the computer or
microcontroller
– Apps read the data (as voltage or current),
– Makes a decision based on its value
– Does whatever it needs to do
• Sensors operate as input devices only
• To receive data we have to setup a listener to
react to new incoming data
9/27/2013
3
Sensor Types
•
•
•
•
•
•
•
•
9/27/2013
• As of Android 2.3
Light Sensor
− Gravity Sensor
Proximity Sensor
Temperature Sensor − Linear Accerlation
Sensor
Pressure Sensor
− Rotation Vector Sensor
Gyroscope Sensor
− Near Field
Accelerometer
Commincation (NFC)
Magnetic Field Sensor Sensor
− Works differently
Orientation Sensor
than the others
4
Detecting a Sensor
• Not all devices have all sensors
• The emulator has only an accelerometer
– Basically for orientation purposes
– To discover which sensors are on your device you
can use a direct or indorect method
• Direct: get a list of available sensors from the
SensorManager, set up listeners for them, then
retrieve data from them
– Assumes user has already installed the App
• Indirect: in the manifest you can specify the resoruces
a device must have to use this App
9/27/2013
5
Develop an App to List Sensors
• The App should look like this:
– It will use a simple Linear
Layout, but have a ScrollView
– Then a TextView within the
ScrollView
• In case there is a long list of
sensors
9/27/2013
6
Start with the Layout
• Setup a TextView
within a Scroll
View within a
Linear Layout
– Try it graphically:
First: changed the default layout
(Relative) to a Vertical Linear
Layout
9/27/2013
7
Start with the Layout
• Setup a TextView
within a Scroll
View within a
Linear Layout
– Try it graphically:
First: changed the default layout
(Relative) to a Vertical Linear
Layout
Then: Select a Scroll View from the Composite Pallete
9/27/2013
8
Start with the Layout
• Setup a TextView
within a Scroll
View within a
Linear Layout
– Try it graphically:
First: changed the default layout
(Relative) to a Vertical Linear
Layout
Then: Select a Scroll View from the Composite Pallete
and drag it over to the graphical layout screen
9/27/2013
9
Start with the Layout
As you drag it into the Linear Layout, it
will turn orange
9/27/2013
10
Start with the Layout
As you drag it into the Linear Layout, it
will turn orange
When you drop it, it will turn
blue.
9/27/2013
11
Start with the Layout
As you drag it into the Linear Layout, it
will turn orange
When you drop it, it will turn
blue.
Then if you pull it down and let go, the
scrollview will expand to fill the screen.
9/27/2013
12
Start with the Layout
As you drag it into the Linear Layout, it
will turn orange
When you drop it, it will turn
blue.
Then if you pull it down and let go, the
scrollview will expand to fill the screen.
Then drop in the textview
9/27/2013
13
Start with the Layout
As you drag it into the Linear Layout, it
will turn orange
When you drop it, it will turn
blue.
Then if you pull it down and let go, the
scrollview will expand to fill the screen.
Then drop in the textview
The xml file appears as shown here
9/27/2013
14
Start with the Layout
As you drag it into the Linear Layout, it
will turn orange
When you drop it, it will turn
blue.
Then if you pull it down and let go, the
scrollview will expand to fill the screen.
Then drop in the textview
The xml file appears as shown here
9/27/2013
The “warning message” states that
either the LinearLayout or the ScrollView
are unecessary
15
Start with the Layout
But, it was left the way it was because
it didn’t hurt anything and it was just a
warning
9/27/2013
16
Add Functionality
9/27/2013
17
Start the Java File
• This is the Java file
that has (so far)
been generated by
Eclipse
9/27/2013
18
Add the TextView
• This is the Java file
that has (so far)
been generated by
Eclipse
TextView myTextView =
(TextView)findViewById(R.id.textView1);
This statement was added to create an identifier and a
reference for the TextView. When this statement was first typed
in, an error was generated, but there was nothing wrong that I
could see.
9/27/2013
19
Add the TextView
• This is the Java file
that has (so far)
been generated by
Eclipse
TextView myTextView =
(TextView)findViewById(R.id.textView1);
This statement was added to create an identifier and a
reference for the TextView. When this statement was first typed
So, [CNTRL][SHIFT]+O was typed, which added the correct import statement
in, an error was generated, but there was nothing wrong that I
so that the error was resolved
could see.
9/27/2013
20
Add the SensorManager
• Next, we need a
reference to the
Sensor Manager
• The command is:
SensorManager sensorManager =
(SensorManager) this.getSystemService(SENSOR_SERVICE);
There can be only one Sensor Manager, and so we need it for the system’s sensor service. The
command is inserted here.
9/27/2013
21
Add the getSensorList Command
• Now that we have a
sensor manager, we
can use the
getSensorList ( )
method
• It will return a list of
all the sensors
• The command is:
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
9/27/2013
22
Add the getSensorList Command
• Now that we have a
sensor manager, we
can use the
getSensorList ( )
method
• It will return a list of
all the sensors
• The command is:
List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ALL);
And, again, we have an error. [CNTRL][SHIFT]+O imports the correct package
9/27/2013
23
Help Windows
• As the command is
being typed, several
Help screens pop
up:
9/27/2013
24
Help Windows
• The .getSensorList
help
9/27/2013
25
Help Windows
• The .getSensorList
help and the Type
Help (Type of List)
9/27/2013
26
The Information
• Next, we need to
build a String to hold
the information that
we want to display in
the TextView:
– The list of sensors
9/27/2013
27
The Information
• To do this, use a
StringBuilder
– Allows you to append
information to an
existing String
• First, create a new
StringBuilder
• Then “append” your
starting message to
it
9/27/2013
28
The Information
• This command
returns a list of all
the sensors
• Then, we have to use
some kind of loop to
build the list of
sensors
9/27/2013
29
The Information
• Create a new Sensor
called sensor
9/27/2013
30
The Information
• Create a new Sensor
called sensor
• As the
sensor.getName
statement is being
typed, the help
screen pops up so
we can see what
methods are
available to sensor
9/27/2013
31
The Information
• Create a new Sensor
called sensor
• As the
sensor.getName
statement is being
typed, the help
screen pops up so
we can see what
methods are
available to sensor
9/27/2013
32
The Information
• An identifier name
(sensorTypes) was
used, but it has no
corresponding type
– So an error occurs
• This type has not
been entered yet
• If you hover the cursor over the error, 9
quick fixes are suggested:
9/27/2013
33
The Information
• An identifier name
(sensorTypes) was
used, but it has no
corresponding type
– So an error occurs
• This type has not
been entered yet
• If you hover the cursor over the error, 9
quick fixes are suggested:
9/27/2013
34
The Information
• But, to fix it, we are
going to create a
HashMap
• A HashMap is a
subclass of Map that
allows us to create a
list of “key” and
value pairs
• This will allow us to get a string for the Type
of sensor in the list
9/27/2013
35
The HashMap
private HashMap<Integer, String> sensorTypes = new HashMap<Integer, String>();
{// Initialize the map of sensor types and values
sensorTypes.put(Sensor.TYPE_ACCELEROMETER, "TYPE_ACCELEROMETER");
sensorTypes.put(Sensor.TYPE_GYROSCOPE, "TYPE_GYROSCOPE");
sensorTypes.put(Sensor.TYPE_LIGHT, "TYPE_LIGHT");
sensorTypes.put(Sensor.TYPE_MAGNETIC_FIELD, "TYPE_MAGNETIC_FIELD");
sensorTypes.put(Sensor.TYPE_ORIENTATION, "TYPE_ORIENTATION");
sensorTypes.put(Sensor.TYPE_PRESSURE, "TYPE_PRESSURE");
sensorTypes.put(Sensor.TYPE_PROXIMITY, "TYPE_PROXIMITY");
sensorTypes.put(Sensor.TYPE_TEMPERATURE, "TYPE_TEMPERATURE");
sensorTypes.put(Sensor.TYPE_GRAVITY, "TYPE_GRAVITY");
sensorTypes.put(Sensor.TYPE_LINEAR_ACCELERATION, "TYPE_LINEAR_ACCELERATION");
sensorTypes.put(Sensor.TYPE_ROTATION_VECTOR, "TYPE_ROTATION_VECTOR");
} //End of Hash Map
• The HashMap matches an Integer constant
(TYPE_LIGHT) to a String “TYPE_LIGHT”
9/27/2013
36
The HashMap
private HashMap<Integer, String> sensorTypes = new HashMap<Integer, String>();
{// Initialize the map of sensor types and values
sensorTypes.put(Sensor.TYPE_ACCELEROMETER, "TYPE_ACCELEROMETER");
sensorTypes.put(Sensor.TYPE_GYROSCOPE, "TYPE_GYROSCOPE");
sensorTypes.put(Sensor.TYPE_LIGHT, "TYPE_LIGHT");
sensorTypes.put(Sensor.TYPE_MAGNETIC_FIELD, "TYPE_MAGNETIC_FIELD");
// sensorTypes.put(Sensor.TYPE_ORIENTATION, "TYPE_ORIENTATION");
sensorTypes.put(Sensor.TYPE_PRESSURE, "TYPE_PRESSURE");
sensorTypes.put(Sensor.TYPE_PROXIMITY, "TYPE_PROXIMITY");
// sensorTypes.put(Sensor.TYPE_TEMPERATURE, "TYPE_TEMPERATURE");
sensorTypes.put(Sensor.TYPE_GRAVITY, "TYPE_GRAVITY");
sensorTypes.put(Sensor.TYPE_LINEAR_ACCELERATION, "TYPE_LINEAR_ACCELERATION");
sensorTypes.put(Sensor.TYPE_ROTATION_VECTOR, "TYPE_ROTATION_VECTOR");
} //End of Hash Map
Two of the TYPES gave warning messages saying that they had been deprecated for this
version of Android, so for now they were commented
9/27/2013
37
One more thing
• We have to set the
text into the
TextView so that it
will print the
available sensors to
the screen of the
android
9/27/2013
38
Execution
• The project was then tested
on the emulator
– There are no sensors on the
emulator, so all that was
shown was the first string
that was built
9/27/2013
39
Execution
• The project was then tested
on the emulator
– There are no sensors on the
emulator, so all that was
shown was the first string
that was built
• Then, it was tested on the
phone
– All of its sensors (that were
found in that list) were
printed on the screen
9/27/2013
40
Accelerometer Test App
9/27/2013
41
Accelerometer Test App
• This App will read the values on the
Acceleromoter and print them on the screen
• If nothing else, it is a Test App to make usre
that:
– There is an Accelerometer on the phone
– The Accelerometer is working
– The program will read the Acceleromoeter
9/27/2013
42
Start with the Layout
• Again, we’ll start
with the Layout
• Keep it very
simple
– The default
RelativeLayout
9/27/2013
43
Start with the Layout
• Again, we’ll start
with the Layout
• Keep it very
simple
– The default
RelativeLayout
• Add 4 TextViews to print the values for X, Y,
and Z (and the title)
9/27/2013
44
The Layout
• The default Relative Layout is:
– Has the single TextView for “Hello World”
<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=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
9/27/2013
45
The TextViews
• The first part:
– The 1st TextView is for the Title
<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:id="@+id/relative">
<TextView
android:textSize="30dp"
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content“ />
<TextView
android:textSize="20dp"
android:layout_below="@+id/name"
android:id="@+id/xval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
. . . . .
9/27/2013
46
The TextViews
• The rest:
<TextView
android:textSize="20dp"
android:id="@+id/yval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/xval"/>
<TextView
android:textSize="20dp"
android:id="@+id/zval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/yval"/>
</RelativeLayout>
9/27/2013
47
The MainActivity.java
• We are going to use the Accelerometer, so we
have several things we must do:
1.
2.
3.
4.
9/27/2013
Extend Activity
Implement a SensorEventListener (for changes)
onCreate( ) method
Implement an event handler for when the
accelerometer changes values
48
The MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
Create the necessary identifiers
TextView title,tv,tv1,tv2;
RelativeLayout layout;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
layout = (RelativeLayout)findViewById(R.id.relative);
//get textviews
title=(TextView)findViewById(R.id.name);
tv=(TextView)findViewById(R.id.xval);
tv1=(TextView)findViewById(R.id.yval);
tv2=(TextView)findViewById(R.id.zval);
}
9/27/2013
49
The MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
TextView title,tv,tv1,tv2;
RelativeLayout layout;
Setup the onCreate( ) method
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
layout = (RelativeLayout)findViewById(R.id.relative);
//get textviews
title=(TextView)findViewById(R.id.name);
tv=(TextView)findViewById(R.id.xval);
tv1=(TextView)findViewById(R.id.yval);
tv2=(TextView)findViewById(R.id.zval);
}
9/27/2013
50
The MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
TextView title,tv,tv1,tv2;
RelativeLayout layout;
Set the content view
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
layout = (RelativeLayout)findViewById(R.id.relative);
//get textviews
title=(TextView)findViewById(R.id.name);
tv=(TextView)findViewById(R.id.xval);
tv1=(TextView)findViewById(R.id.yval);
tv2=(TextView)findViewById(R.id.zval);
}
9/27/2013
51
The MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
Create a new SensorManager (always
TextView title,tv,tv1,tv2;
before the sensor)
RelativeLayout layout;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
layout = (RelativeLayout)findViewById(R.id.relative);
//get textviews
title=(TextView)findViewById(R.id.name);
tv=(TextView)findViewById(R.id.xval);
tv1=(TextView)findViewById(R.id.yval);
tv2=(TextView)findViewById(R.id.zval);
}
9/27/2013
52
The MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
Create a new accelerometer
TextView title,tv,tv1,tv2;
RelativeLayout layout;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
layout = (RelativeLayout)findViewById(R.id.relative);
//get textviews
title=(TextView)findViewById(R.id.name);
tv=(TextView)findViewById(R.id.xval);
tv1=(TextView)findViewById(R.id.yval);
tv2=(TextView)findViewById(R.id.zval);
}
9/27/2013
53
The MainActivity.java
public class MainActivity extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
TextView title,tv,tv1,tv2;
RelativeLayout layout;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
//get layout
layout = (RelativeLayout)findViewById(R.id.relative);
//get textviews
title=(TextView)findViewById(R.id.name);
tv=(TextView)findViewById(R.id.xval);
tv1=(TextView)findViewById(R.id.yval);
tv2=(TextView)findViewById(R.id.zval);
}
9/27/2013
Setup the needed textviews
54
The Event Handler
@Override
public final void onSensorChanged(SensorEvent event)
{
// Many sensors return 3 values, one for each axis.
float x = event.values[0];
float y = event.values[1];
The Event Handler declaration
float z = event.values[2];
//display values using TextView
title.setText(R.string.app_name);
tv.setText("X axis" +"\t\t"+x);
tv1.setText("Y axis" + "\t\t" +y);
tv2.setText("Z axis" +"\t\t" +z);
}
9/27/2013
55
The Event Handler
@Override
public final void onSensorChanged(SensorEvent event)
{
// Many sensors return 3 values, one for each axis.
float x = event.values[0];
float y = event.values[1];
The values for x, y, and z as read off the
float z = event.values[2];
accelerometer
//display values using TextView
title.setText(R.string.app_name);
tv.setText("X axis" +"\t\t"+x);
tv1.setText("Y axis" + "\t\t" +y);
tv2.setText("Z axis" +"\t\t" +z);
}
9/27/2013
56
The Event Handler
@Override
public final void onSensorChanged(SensorEvent event)
{
// Many sensors return 3 values, one for each axis.
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
//display values using TextView
title.setText(R.string.app_name);
tv.setText("X axis" +"\t\t"+x);
tv1.setText("Y axis" + "\t\t" +y);
tv2.setText("Z axis" +"\t\t" +z);
Print out the title (AccelerometerDemo)
}
9/27/2013
57
The Event Handler
@Override
public final void onSensorChanged(SensorEvent event)
{
// Many sensors return 3 values, one for each axis.
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
//display values using TextView
title.setText(R.string.app_name);
tv.setText("X axis" +"\t\t"+x);
tv1.setText("Y axis" + "\t\t" +y);
tv2.setText("Z axis" +"\t\t" +z);
Print out the three values)
}
9/27/2013
58
Other Methods
@Override
protected void onResume()
{
super.onResume();
mSensorManager.registerListener(this, mAccelerometer,
SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
mSensorManager.unregisterListener(this);
}
• Two of the necessary methods
– In case the App is paused and resumed
9/27/2013
59
Execution
• When executed on the
emulator
– Again, nothing happens
– Must run it on the phone to
test it
9/27/2013
60
Summary
• We defined a sensor
• And looked at the different types of sensors
on an android phone
• We wrote an App to detect the sensors on
the phone and list them on the screen
• Developed an example application of the
accelerometer
9/27/2013
61