0% found this document useful (0 votes)
70 views33 pages

Mobile Application Development Laboratory

The document describes steps to create an Android application in Eclipse that uses event handlers. It includes code for an Activity class with two buttons that change the text size of a text view when clicked. The buttons are assigned onClickListeners that call setTextSize() on the text view with different values, demonstrating how to handle button click events.

Uploaded by

barwin raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views33 pages

Mobile Application Development Laboratory

The document describes steps to create an Android application in Eclipse that uses event handlers. It includes code for an Activity class with two buttons that change the text size of a text view when clicked. The buttons are assigned onClickListeners that call setTextSize() on the text view with different values, demonstrating how to handle button click events.

Uploaded by

barwin raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

MP5411 MOBILE APPLICATION DEVELOPMENT LABORATORY

LIST OF EXPERIMENTS
1. Develop an application that uses Layout Managers.
2. Develop an application that uses event listeners.
3. Develop an application that uses Adapters ,Toast.
4. Develop an application that makes use of database.
5. Develop an application that makes use of RSS Feed.
6. Implement an application that implements Multi threading.
7. Develop a native application that uses GPS location information.
8. Implement an application that writes data to the SD card.
9. Implement an application that creates an alert upon receiving a message.
10. Develop a game application.

Steps to open an Android Program :


1.Create a Folder in D or E or F drive in the name say “Mob Lab”.

2.Use this folder as your workspace. That is store all the programs in this folder. Double click the

Eclipse Icon. .

3.Select the created folder as your Workspace in the “Workspace Launcher Window”.

4.Now the “Eclipse IDE – Android Development Tool(ADT)” has opened.

5.Click File -> New -> Android Application Development.


6. A new Android Application window appears. In it give an application name, and the project
and package name are automatically created. Then press Next.

7.Then Configure Project window appears. In it, no need to change anything, press “Next”.

8.Then Configure Launcher Icon window appears. Press “Next”.

9.Then Create Activity window appears. Press “Next”.

10.Then New Blank Activity window appears. Press “Finish”.


11.A new project is created.
Exp.No : 1
Date : LAYOUT MANAGERS

My file name: layoutmanager


AIM:

To develop an android application for layout managers in eclipse.

PROCEDURE:

1. Open Eclipse IDE. File -> New -> Android Application Project. Give name as
LayoutManager. Select Minimum required SDK is “API 14:Android 4.0(Ice
CreamSandwich). Press “Next” for four times and then “Finish”.
2. Delete the default “Hello World” Text View. In the Outline Tab at Right side select
“Relative Layout” and right click it.
3. Choose “Change Layout” and in the Change Layout window, choose “Grid Layout”.
Insert 9 Text Views as given in output figure.
4. Modify MainActivity.java
5. Modify activity_main.xml
6. Run the program.

Note :In the Grid layout if system shows error “View requires API level 14 (current min is 8): "
change manifest code as :

<uses-sdk minSdkVersion="14" />

PROGRAM:

MainActivity.java
package com.example.gridlayout;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

TextView t1, t2, t3, t4, t5, t6, t7, t8, t9;
TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4 = (TextView) findViewById(R.id.textView4);
t5 = (TextView) findViewById(R.id.textView5);
t6 = (TextView) findViewById(R.id.textView6);
t7 = (TextView) findViewById(R.id.textView7);
t8 = (TextView) findViewById(R.id.textView8);
t9 = (TextView) findViewById(R.id.textView9);

t1.setClickable(true);
t2.setClickable(true);
t3.setClickable(true);
t4.setClickable(true);
t5.setClickable(true);
t6.setClickable(true);
t7.setClickable(true);
t8.setClickable(true);
t9.setClickable(true);
}

public void textClickListener(View v) {


text = (TextView)findViewById(v.getId());
Toast.makeText(getBaseContext(), text.getText() + "
Clicked",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

activity_main.xml
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal"
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" >

<Space
android:layout_column="1"
android:layout_gravity="fill_vertical"
android:layout_row="1" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="0"
android:text="[0,0]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="66dp"
android:layout_height="53dp"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="0"
android:text="[0,1]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView3"
android:layout_width="78dp"
android:layout_height="55dp"
android:layout_column="0"
android:layout_gravity="right|top"
android:layout_row="0"
android:text="[0,2]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="58dp"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="1"
android:text="[1,0]"
android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
android:id="@+id/textView5"
android:layout_width="66dp"
android:layout_height="54dp"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="1"
android:text="[1,1]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView6"
android:layout_width="74dp"
android:layout_height="46dp"
android:layout_column="0"
android:layout_gravity="right|top"
android:layout_row="1"
android:text="[1,2]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView7"
android:layout_width="63dp"
android:layout_height="54dp"
android:layout_column="0"
android:layout_gravity="left|top"
android:layout_row="2"
android:text="[2,0]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView8"
android:layout_width="62dp"
android:layout_height="57dp"
android:layout_column="0"
android:layout_gravity="center_horizontal|top"
android:layout_row="2"
android:text="[2,1]"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView9"
android:layout_width="74dp"
android:layout_height="61dp"
android:layout_column="0"
android:layout_gravity="right|top"
android:layout_row="2"
android:text="[2,2]"
android:textAppearance="?android:attr/textAppearanceMedium" />

</GridLayout>
OUTPUT :

CONCLUSION:

Thus the android program for layout managers using eclipse was executed successfully.
Exp.No : 2 EVENT HANDLER
Date :
My file name: Eventhandler
AIM:

To develop an application that displays use of Event Handlers.

PROCEDURE:

1. Open eclipse IDE.


2. In the Palette, choose Form Widgets -> Drag and drop two Buttons.
3. Modify MainActivity.java
4. Run the program.

ActivityMain.java

package com.example.eventhandlers;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.ProgressDialog;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.MenuItem;

public class MainActivity extends Activity {


private ProgressDialog progress;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progress = new ProgressDialog(this);

b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView = (TextView) findViewById(R.id.textView1);
txtView.setTextSize(25);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView txtView = (TextView) findViewById(R.id.textView1);
txtView.setTextSize(55);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

OUTPUT :

CONCLUSION:

Thus the android program that displays uses of Event Handlers was executed successfully.
Exp.No : 3
Date : CALCULATOR APPLICATION

My file name: Calculator


AIM:

To develop a native calculator application using eclipse.

PROCEDURE:

1. Open Eclipse IDE. File -> New -> Android Application Project. Give name as
Calculator. Press “Next” for two times. Press “Next” for two times.
2. Then in the Configure Launcher Icon window set Background Color as Black. Press Ok
and then press “Next” and then “Finish”.
3. Delete the default “Hello World” Text View. In the Outline Tab at Right side select
“Relative Layout” and right click it.
4. Choose “Change Layout” and in the Change Layout window, choose “Linear Layout
(Vertical)”.

5. Then drag and drop “Linear Layout (Horizontal)” at the top from Layout tab of Palette.
6. Insert a “EditText” (for Numbers (Decimal) (42.0)) from Text Fields.
7. Again drag and drop “Linear Layout (Horizontal)” next to Edit Text from Layout tab of
Palette.
8. Insert four buttons from Form Widgets into it. Repeat the steps for three more times.
9. Then insert a “Linear Layout (Horizontal)” with an Empty “TextView” to skip a line.
10. Again insert four “Linear Layout (Horizontal)” with four buttons in it. Then insert a
“Linear Layout (Horizontal)” with an Empty “TextView” to skip a line.
11. In “Linear Layout (Horizontal)” insert two buttons for “Exit” and “Reset”. Finally it’ll
look like this.

12. Modify MainActivity.java and activity_main.xml.


13. Run the program.

PROGRAM:

MainActivity.java
package com.example.cal;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.content.Context;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

EditText editText;

Button button, button0, button1, button2, button3, button4, button5,


button6, button7, button8, button9,
buttonPlus, buttonMinus, buttonMultiply, buttonDivide, buttonEqual,
button_del, buttonExit, buttonReset;
String sum="",res;
Float result=0f;
char press = ' ';
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

editText=(EditText)findViewById(R.id.editText1);

button7=(Button)findViewById(R.id.button1);
button8=(Button)findViewById(R.id.button2);
button9=(Button)findViewById(R.id.button3);
button_del=(Button)findViewById(R.id.button4);
button4=(Button)findViewById(R.id.button5);
button5=(Button)findViewById(R.id.button6);
button6=(Button)findViewById(R.id.button7);
buttonDivide=(Button)findViewById(R.id.button8);
button1=(Button)findViewById(R.id.button9);
button2=(Button)findViewById(R.id.button10);
button3=(Button)findViewById(R.id.button11);
buttonMultiply=(Button)findViewById(R.id.button12);
button0=(Button)findViewById(R.id.button13);
buttonEqual=(Button)findViewById(R.id.button14);
buttonPlus=(Button)findViewById(R.id.button15);
buttonMinus=(Button)findViewById(R.id.button16);

buttonExit=(Button)findViewById(R.id.button17);
buttonReset=(Button)findViewById(R.id.button18);

editText.setText(result.toString());

public void noClickListener(View v)


{
button = (Button)findViewById(v.getId());
if(sum != "")
{
sum=sum+(String) button.getText();;
editText.setText(sum);
}
else
{
sum=(String) button.getText();
editText.setText(sum);
}
}
public void operatorClickListener(View v) {
button = (Button)findViewById(v.getId());
if( button.getText().charAt(0) == '+' ) {
res = (String) editText.getText().toString();
press = '+';
}
else if( button.getText().charAt(0) == '-' ) {
res = editText.getText().toString();
press = '-';
}
else if( button.getText().charAt(0) == '*' ) {
res = editText.getText().toString();
press = '*';
}
else if( button.getText().charAt(0) == '/' ) {
res = editText.getText().toString();
press = '/';
}
else if( button.getText().charAt(0) == '=' ) {
if( press == '+' ) result = Float.parseFloat(res) +
Float.parseFloat(editText.getText().toString());
else if( press == '-' ) result = result -
Float.parseFloat(editText.getText().toString());
else if( press == '*' ) result = result *
Float.parseFloat(editText.getText().toString());
else if( press == '/' ) result = result /
Float.parseFloat(editText.getText().toString());
editText.setText(result.toString());
}
sum="";
}
public void exitClickListener(View v)
{
finish();
}

public void resetClickListener(View v)


{
sum="";
res="";
result=0f;
press=' ';

editText.setText("0.0");
}

public void delClickListener(View v)


{
if(sum !="")
{
StringBuilder stringBuilder=new StringBuilder(80);
stringBuilder.append(sum);
sum=stringBuilder.deleteCharAt(stringBuilder.length()-1)

.toString();
editText.setText(sum);
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="numberDecimal" >

<requestFocus />
</EditText>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:onClick="noClickListener"
android:text="7" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="8" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="9" />

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="delClickListener"
android:text="&lt;-" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="4" />

<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="5" />

<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="6" />

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="operatorClickListener"
android:text="/" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="1" />

<Button
android:id="@+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="2" />

<Button
android:id="@+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="3" />

<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="operatorClickListener"
android:text="*" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<Button
android:id="@+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="noClickListener"
android:text="0" />

<Button
android:id="@+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="operatorClickListener"
android:text="=" />

<Button
android:id="@+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="operatorClickListener"
android:text="+" />

<Button
android:id="@+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="operatorClickListener"
android:text="-" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/button17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.45"
android:onClick="exitClickListener"
android:text="Exit" />

<Button
android:id="@+id/button18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.52"
android:onClick="resetClickListener"
android:text="Reset" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>

</LinearLayout>

OUTPUT:

CONCLUSION :
Thus the Android program to develop a native calculator application using eclipse was
executed successfully.

Exp.No : 4
Date : DATABASE
My file name:Database
AIM:
To develop an android application that makes use of database in eclipse.

PROCEDURE:

1. Open Eclipse IDE. File -> New -> Android Application Project. Give name as Database.
Press “Next” for three times and then “Finish”.
2. Delete the default “Hello World” Text View. Drag and drop Plain Text from Text Fields.
Drag and drop a button and Spinner sub item from Form Widgets.
3. Modify MainActivity.java.
4. Right click MainActivity.java -> New -> Class. A window appears. In it give Name as
DatabaseHandler.java.
5. Modify DatabaseHandler.java.
6. Modify activity_main.xml
7. Run the program.

PROGRAM:

MainActivity.java

package com.example.sqlitespinner;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.util.List;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener{


Spinner spinner;
Button btnAdd;
EditText inputLabel;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

spinner = (Spinner) findViewById(R.id.spinner);


btnAdd = (Button) findViewById(R.id.btn_add);
inputLabel = (EditText) findViewById(R.id.input_label);

spinner.setOnItemSelectedListener(this);
loadSpinnerData();

btnAdd.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
String label = inputLabel.getText().toString();

if (label.trim().length() > 0) {
DatabaseHandler db = new
DatabaseHandler(getApplicationContext());
db.insertLabel(label);

inputLabel.setText("");

// Hiding the keyboard


InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputLabel.getWindowToken(),
0);
loadSpinnerData();
} else {
Toast.makeText(getApplicationContext(), "Please enter
label name",Toast.LENGTH_SHORT).show();
}

}
});
}

private void loadSpinnerData() {


DatabaseHandler db = new DatabaseHandler(getApplicationContext());
List<String> lables = db.getAllLabels();

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>


(this,android.R.layout.simple_spinner_item, lables);

dataAdapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);

// attaching data adapter to spinner


spinner.setAdapter(dataAdapter);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position,long id) {
String label = parent.getItemAtPosition(position).toString();

// Showing selected spinner item


Toast.makeText(parent.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();

@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

DatabaseHandler.java

package com.example.sqlitespinner;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHandler extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "spinnerExample";

private static final String TABLE_LABELS = "labels";

private static final String KEY_ID = "id";


private static final String KEY_NAME = "name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

String CREATE_CATEGORIES_TABLE = "CREATE TABLE " + TABLE_LABELS +


"(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT)";
db.execSQL(CREATE_CATEGORIES_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LABELS);

// Create tables again


onCreate(db);
}

public void insertLabel(String label){


SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();


values.put(KEY_NAME, label);

// Inserting Row
db.insert(TABLE_LABELS, null, values);
db.close(); // Closing database connection
}

public List<String> getAllLabels(){


List<String> labels = new ArrayList<String>();

String selectQuery = "SELECT * FROM " + TABLE_LABELS;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
} while (cursor.moveToNext());
}

cursor.close();
db.close();

return labels;
}
}
}

OUTPUT :

CONCLUSION:

Thus the android application that makes use of database using eclipse was executed
successfully.
Exp.No : 5
Date: MULTITHREADING

AIM:

To develop an android application that implements multithreading using eclipse.

PROCEDURE:

1. Open Eclipse IDE. In File -> New -> Android Application Project. Give name as
Multithreading. Press “Next” for three times and then “Finish”.
2. Delete the default “Hello World” Text View. Click and drag a text view and modify text
as “Button will appear after 10 seconds”. Click and drag another text view and a
button.
3. Modify MainActivity.java
4. Modify activity_main.xml
5. Run the program.

PROGRAM:

MainActivity.java

package com.example.multithread;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {


Handler hand = new Handler();
Button clickme;
TextView timer;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_main);
timer = (TextView) findViewById(R.id.timer);
clickme = (Button) findViewById(R.id.clickme);
hand.postDelayed(run, 1000);
}
Runnable run = new Runnable() {
@Override
public void run() {
updateTime();
}
};
public void updateTime() {
timer.setText("" + (Integer.parseInt(timer.getText().toString()) - 1));
if (Integer.parseInt(timer.getText().toString()) == 0) {
clickme.setVisibility(0);
} else {
hand.postDelayed(run, 1000);
}
}
}

activity_main.xml :

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<RelativeLayout
android:id="@+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="80dp">

<TextView
android:id="@+id/display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button will appear after 10 seconds"/>
</RelativeLayout>

<RelativeLayout
android:id="@+id/secondlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/firstlayout"
android:gravity="center">

<TextView
android:id="@+id/timer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="12"
android:layout_marginTop="80dp"
android:textSize="36dp"/>
</RelativeLayout>

<RelativeLayout
android:id="@+id/thirdlayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/secondlayout"
android:gravity="center">

<Button
android:id="@+id/clickme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click_me"
android:visibility="invisible"
android:layout_marginTop="100dp"/>
</RelativeLayout>

</RelativeLayout>

OUTPUT :

CONCLUSION:

Thus the android application that implements multithreading using eclipse was executed
successfully.
Exp.No : 6
Date : SD CARD

AIM:
To implement an android application that writes data to SD card using eclipse.

PROCEDURE:

1. Open Eclipse IDE. In File -> New -> Android Application Project. Give name as
SDCard. Press “Next” for three times and then “Finish”.
2. Delete the default “Hello World” Text View. Drag and drop Multi Line Text from Text
Fields. Drag and drop a button and Spinner sub item from Form Widgets.
3. Modify MainActivity.java.
4. Modify Andoid_manifest.xml
5. Run the program.
[ Note : The Emulator must have SD Card Size set to some value (say 20 MiB)
In all the buttons give
android:layout_width="fill_parent"
android:layout_height="wrap_content" ].

PROGRAM:

MainActivity.java

package com.example.sdcard;

import java.io.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {


EditText txtData;
Button btnWriteSDFile;
Button btnReadSDFile;
Button btnClearScreen;
Button btnClose;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// bind GUI elements with local controls
txtData = (EditText) findViewById(R.id.txtData);
txtData.setHint("Enter some lines of data here...");

btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);


btnWriteSDFile.setOnClickListener(new OnClickListener() {

public void onClick(View v) {


// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new
OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}// onClick
}); // btnWriteSDFile

btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);


btnReadSDFile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// write on SD card file data in the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
txtData.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),
"Done reading SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}// onClick
}); // btnReadSDFile

btnClearScreen = (Button) findViewById(R.id.btnClearScreen);


btnClearScreen.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// clear text box
txtData.setText("");
}
}); // btnClearScreen

btnClose = (Button) findViewById(R.id.btnClose);


btnClose.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// clear text box
finish();
}
}); // btnClose
}// onCreate
}// AndSDcard

AndoidManifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sdcard"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.example.sdcard.MainActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
<uses-permissionandroid:name= "android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
</manifest>

OUTPUT :

CONCLUSION:
Thus the android application that writes data to the SD card using eclipse was executed
successfully.
Exp.No : 7
Date : DIGITAL CLOCK
AIM:
To implement an android application that displays Digital Clock using eclipse.

PROCEDURE:

1. Open Eclipse IDE. In File -> New -> Android Application Project. Give name as
DigitalClock. Press “Next” for three times and then “Finish”.
2. Delete the default “Hello World”. Drag and drop six TextViews from Form Widgets.
3. Modify MainActivity.java.
4. Run the program.
[ Note : The Emulator must have SD Card Size set to some value (say 20 MiB)
In all the buttons give android:layout_width="fill_parent"
android:layout_height="wrap_content" ].

PROGRAM:

MainActivity.java

package com.example.binaryclock;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class MainActivity extends Activity implements Runnable {
Date currentTime;
Thread runner;
final Runnable updater = new Runnable(){
public void run(){
updateClockValues();
};
};
final Handler mHandler = new Handler();
TextView hours_dec;
TextView mins_dec;
TextView secs_dec;
SimpleDateFormat hours_sdf = new SimpleDateFormat("HH");
SimpleDateFormat mins_sdf = new SimpleDateFormat("mm");
SimpleDateFormat secs_sdf = new SimpleDateFormat("ss");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
currentTime = Calendar.getInstance().getTime();
hours_dec = (TextView) findViewById(R.id.textView1);
mins_dec = (TextView) findViewById(R.id.textView2);
secs_dec = (TextView) findViewById(R.id.textView3);
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
private void updateClockValues() {
currentTime = Calendar.getInstance().getTime();
hours_dec.setText(hours_sdf.format(currentTime.getTime()));
mins_dec.setText(mins_sdf.format(currentTime.getTime()));
secs_dec.setText(secs_sdf.format(currentTime.getTime()));
}
@Override
public void run() {
while (runner != null) {
try {
Thread.sleep(1000);
Log.i("Tick", "Tock");
} catch (InterruptedException e) { };
mHandler.post(updater);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

OUTPUT :

CONCLUSION:
Thus the android application that displays Digital Clock using eclipse was executed
successfully.

You might also like