CIS493 Mobile Application Development using Android
Fall 2010HOMEWORK 1Due:Th. Oct 7th
Please implement a simple FLASHLIGHT Android application. The UI should look - at leatst - like the following screen-shots
HINT: Use nested Linear Layouts (as discussed in class) to produce the UI for this assignment.
Test your application in the Emulator.
Document your code (this will impact your grade).
Print the XML LAYOUT and CODE. Include SCREENSHOTS showing your application working (use the DDMS screen capture tool).
Extra points for improved design and features !
Code
package cis493.matos;
//Author: V. Matos 7-Oct-2010
// ------
// A simple FLASHLIGHT application. Color and intensity chosen
// by the user. Light is emitted from the device’s screen.
// Application uses all the available screen space by removing the
// STATUS line. Changes in orientation (horizontal/vertical) do not
// interrup the app’s ‘natural’ behavior. Color setting is retained.
// A LONG_TAP on the screen stops the application.
// ------
// Reference RGB Color Chart available at:
//
//
//
//------
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.SeekBar.OnSeekBarChangeListener;
publicclass FlashLight1 extends Activity implements OnClickListener {
LinearLayout myScreen;
TextView tvLight;
Button btnWhite;
Button btnGrey1;
Button btnRed;
Button btnGreen;
Button btnBlue;
SeekBar sBar1;
intred=255; intgreen= 255; intblue= 255; intalpha = 255;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// hide title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
// you may also remove title by adding to <application> section
// in the the manifest the next entry (this is a better option!)
// android:theme="@android:style/Theme.NoTitleBar"
setContentView(R.layout.main);
// hide status bar
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/////////////////////////////////////////////////////////////
// seekBar changes the opacity (alpha)of the displayed color
// 000 transparent (you can see the container's back color)
// 255 opaque (highest intensity of the selected color)
//
sBar1 = (SeekBar) findViewById(R.id.SeekBar01);
sBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
publicvoid onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
publicvoid onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
// equate seekBar changes (0-255) as OPACITY (alpha) setting
// making sure other color contributions do not change.
publicvoid onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
alpha = 255 - progress;
int myBackColor = android.graphics.Color.argb(alpha, red, green, blue);
tvLight.setBackgroundColor(myBackColor);
}
});
///////////////////////////////////////////////////////////////////////////
// a long press on the screen stops the flashlight
//
myScreen = (LinearLayout) findViewById(R.id.myScreen);
myScreen.setOnLongClickListener(new OnLongClickListener() {
publicboolean onLongClick(View v) {
finish();
returnfalse;
}
});
///////////////////////////////////////////////////////////////////////////
tvLight = (TextView) findViewById(R.id.tvLight);
btnWhite = (Button) findViewById(R.id.btnWhite);
btnGrey1 = (Button) findViewById(R.id.btnGrey1);
btnRed = (Button) findViewById(R.id.btnRed);
btnGreen = (Button) findViewById(R.id.btnGreen);
btnBlue = (Button) findViewById(R.id.btnBlue);
////////////////////////////////////////////////////////////////////////////
btnWhite.setOnClickListener(this);
btnGrey1.setOnClickListener(this);
btnRed.setOnClickListener(this);
btnGreen.setOnClickListener(this);
btnBlue.setOnClickListener(this);
}
@Override
publicvoid onConfigurationChanged(Configuration newConfig) {
// this is called when changing landscape/portrait (just showing)
super.onConfigurationChanged(newConfig);
int orientation =
getWindow().getWindowManager().getDefaultDisplay().getOrientation();
Toast.makeText(this, " 0:Portrait/1:Landscape " + orientation, 1).show();
}
/////////////////////////////////////////////////////////////////////////////////////
// a solid (opaque) RGB color is selected after detecting the user's choice
//
publicvoid onClick(View v) {
if (v.getId() == btnWhite.getId())
{
red = 255; green = 255; blue= 255;
}
elseif (v.getId() == btnGrey1.getId()) {
red = 196; green = 196; blue= 196;
}
elseif (v.getId() == btnRed.getId()) {
red = 255; green = 0; blue= 0;
}
elseif (v.getId() == btnGreen.getId()) {
red = 0; green = 255; blue= 0;
}
elseif (v.getId() == btnBlue.getId()){
red = 0; green = 0; blue= 255;
}
else{
red = 255; green = 255; blue= 255;
}
Toast.makeText(this,
"color: " + android.graphics.Color.argb( 0, red, green, blue),
1).show();
int myBackColor = android.graphics.Color.argb(255, red, green, blue);
tvLight.setBackgroundColor(myBackColor);
sBar1.setProgress(0);
}//onClick
// /////////////////////////////////////////////////////////////////////////
// A change of orientation (landscape/portrait) will produce
// the re-drawing of the application's screen. The application's
// life cycle is restarted from 'onCreate' once more.
// The following two methods save and restore the color selection
// made by the user before/after the application is re-executed.
//
// Testing this Code:
// Comment out the following methods, runn app, select a color,
// rotate the screen (Ctrl F11), it goes white loosing color.
// Uncomment code, repeat the test. Color choice will be retained
// after rotating the phone (Ctrl-F11)
@Override
protectedvoid onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// app has been paused,
// saving currently selected color in a bundle
// now, ready to be stopped
outState.putInt("redValue", red);
outState.putInt("greenValue", green);
outState.putInt("blueValue", blue);
outState.putInt("alphaValue", alpha);
}
@Override
protectedvoid onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// coming back to life.
// setting background to values set before reStarting
red = savedInstanceState.getInt("redValue");
green = savedInstanceState.getInt("greenValue");
blue = savedInstanceState.getInt("blueValue");
alpha = savedInstanceState.getInt("alphaValue");
int myBackColor = android.graphics.Color.argb(alpha, red, green, blue);
tvLight.setBackgroundColor(myBackColor);
}
////////////////////////////////////////////////////////////////////////
}//Flashlight1
Layout
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayout
android:id="@+id/myScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="
TextView
android:id="@+id/tvLight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:layout_weight="1"
</TextView
LinearLayout
android:id="@+id/linLayoutControl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_gravity="center_horizontal"
Button
android:id="@+id/btnWhite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="White"
android:layout_weight="2"
</Button
Button
android:id="@+id/btnGrey1"
android:layout_height="wrap_content"
android:text="Grey"
android:layout_width="wrap_content"
</Button
Button
android:id="@+id/btnRed"
android:layout_height="wrap_content"
android:text="Red"
android:layout_width="wrap_content"
</Button
Button
android:id="@+id/btnGreen"
android:layout_height="wrap_content"
android:text="Green"
android:layout_width="wrap_content"
</Button
Button
android:id="@+id/btnBlue"
android:layout_height="wrap_content"
android:text="Blue"
android:layout_width="wrap_content"
</Button
</LinearLayout
SeekBar
android:id="@+id/SeekBar01"
android:max="255"
android:progress="1"
android:padding="10dip"
android:maxHeight="1dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
</SeekBar
</LinearLayout
Manifest
<?xmlversion="1.0"encoding="utf-8"?>
manifestxmlns:android="
package="cis493.matos"
android:versionCode="1"
android:versionName="1.0"
applicationandroid:icon="@drawable/flashlight"
android:label="@string/app_name"
activityandroid:name=".FlashLight1"
android:label="@string/app_name"
android:configChanges="orientation"
intent-filter
actionandroid:name="android.intent.action.MAIN"/>
categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter
</activity
</application
uses-sdkandroid:minSdkVersion="8"/>
</manifest