Android Program - Edited

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

1) develop an application that uses guicomponents,font

and colors.

<?xml version=”1.0” encoding=”utf-8”?>


<Relativelayout
xmlns:android=http://schemas.android.com/apk/res/androi
d
Android:layout_width=”match_parent”
Android:layout_height=”match_parent”
tools:context=".MyActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:gravity="center"
android:text=”Hello World!"
android:textSize="25sp"
android:textStyle="bold" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change font size"
android:textSize="25sp" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:gravity="center"
android:text="Change color"
android:textSize="25sp" />
</Relativelayout>
MainActivity.java
package com.example.ex.no.1;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.graphics.Color;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity
{
float font=24;
int i=1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
Super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView
t1=(TextView)findViewById(R.id.textView1);
Button b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener()
{
Public void onClick(View view)
{
t1.setTextSize(font);
font=font+5;
If(font==40);
font=20;
}
});
Button b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener()
{
Public void onClick(View view)
{
Switch(i)
{
Case 1:
t1.setTextColor(Color.parseColor(“red”));
Break;
Case 2:
t1.setTextColor(Color.parseColor(“green”));
Break;
Case 3:
t1.setTextColor(Color.parseColor(“blue”));
Break;
Case 4:
t1.setTextColor(Color.parseColor(“yellow”));
Break;
}
i++;
If(i==5)
i=1;
}
});
}
}
2).Develope an app that uses Intent Activity

Activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
tools:ignore="HardcodedText" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Activity1" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:text="click"
android:onClick="myIntent"
/>

</RelativeLayout>

Activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="HardcodedText" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="92dp"
android:text="Activity2" />

</RelativeLayout>

MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class MainActivity extends Activity {


TextView tvOutput;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

tvOutput=(TextView)findViewById(R.id.textView1);
b1=(Button)findViewById(R.id.button1);
}
public void myIntent(View v){
Intent intent=new
Intent(this,MainActivity2.class);
startActivity(intent);
}
}

MainActivity2.java
import android.app.Activity;
import android.os.Bundle;

public class MainActivity2 extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest
xmlns:android="http://schemas.android.com/apk/res/andro
id"
package="com.example.intent2"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity

android:name="com.example.intent2.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action
android:name="android.intent.action.MAIN" />

<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity

android:name="com.example.intent2.MainActivity2"/>

</application>

</manifest>
3)Develop an application that uses layout manager

and event listeners

Activity_main. Xml

<?xml version=”1.0” encoding=”utf-8”?>


<Relativelayout xmlns:android=http://schemas.android.com/apk/res/android

Android:layout_width=”match_parent”

Android:layout_height=”match_parent”

tools:context=".MyActivity">

<TextView

android:id="@+id/textView1"

android:text="BASIC_EVENT" />

<TextView

android:id="@+id/textView2"

android:text="Enter_No.1" />

<TextView

android:id="@+id/textView3"

android:text="rEnter_No.2" />

<Button

android:id="@+id/button1"

android:text="Addition" />

<Button

android:id="@+id/button2"

android:text="Subtraction" />

<Button

android:id="@+id/button3"

android:text="Clear" />

<EditText

android:id="@+id/editText1"

android:inputType="number"/>
<EditText

android:id="@+id/editText2"

android:inputType="number" />

</RelativeLayout>

MainActivity.java

Package com.example.layoutmanager;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

Public class MainActivity extends Activity

EditText txtData1,txtData2;

float num1,num2,result1,result2;

@Override

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button add=(Button)findViewById(R.id.button1);

add.setOnClickListener(new OnClickListener()

{
Public void onClick(View v)

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

txtData2=(EditText)findViewById(R.id.editText2);

num1=Float.parseFloat(txtData1.getText().toString());

num2=Float.parseFloat(txtData2.getText().toString());

result1=num1+num2;

Toast.makeText(getBaseContext(),

“ANSWER”+result1,Toast.LENGTH_SHORT).show();

});

Button sub=(Button)findViewById(R.id.button2);

sub.setOnClickListener(new OnClickListener()

Public void onClick(View v)

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

txtData2=(EditText)findViewById(R.id.editText2);

num1=Float.parseFloat(txtData1.getText().toString());

num2=Float.parseFloat(txtData2.getText().toString());

result2=num1-num2;

Toast.makeText(getBaseContext(),

“ANSWER”+result2,Toast.LENGTH_SHORT).show();

});
Button clear=(Button)findViewById(R.id.button3);

clear.setOnClickListener(new OnClickListener()

public void onClick(View v)

txtData1.setText("");

txtData2.setText("");

});

}
4) Write an application that draws basic graphical

primitives on the screen

Activity. Xml

<?xml version=”1.0” encoding=”utf-8”?>

<Relativelayout xmlns:android=http://schemas.android.com/apk/res/android

Android:layout_width=”match_parent”

Android:layout_height=”match_parent”

tools:context=".MyActivity">

MainActivity.java

Package com.example.graphicalprimitives;

import android.os.Bundle;

import android.app.Activity;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.view.View;

public class MainActivity extends Activity

public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(new myview(this));

private class myview extends View


{

public myview(Context context)

super(context);

protected void onDraw(Canvas canvas)

super.onDraw(canvas);

Paint paint=new Paint();

paint.setTextSize(40);

paint.setColor(Color.GREEN);

canvas.drawText(“Circle”,55,30,paint);

paint.setColor(Color.RED);

canvas.drawCircle(100,150,100,paint);

paint.setColor(Color.GREEN);

canvas.drawText(“Rectangle”,255,30,paint);

paint.setColor(Color.YELLOW);

canvas.drawRect(250, 50,400,350,paint);

paint.setColor(Color.GREEN);

canvas.drawText(“SQUARE”,55,430,paint);

paint.setColor(Color.BLUE)

canvas.drawRect(50, 450,150,550,paint);

paint.setColor(Color.GREEN);

canvas.drawText("LINE",255,430, paint);

paint.setColor(Color.CYAN);
canvas.drawLine(250, 500, 350, 500,paint);

6). implement an application that implements multi-threading

Activity_main. Xml

<?xml version=”1.0” encoding=”utf-8”?>

<Relativelayout

xmlns:android=http://schemas.android.com/apk/r

es/android

android:layout_width=”match_parent”

android:layout_height=”match_parent”
tools:context=”.MyActivity”>

TextView

android:id=”@+id/textView1” />

<Button

android:id=”@+id/button1”

android:text=”START_MULTITHREADING” />

<TextView

android:id=”@+id/textView2”

android:text=”MULTITHREAD”

</RelativeLayout>

MainActivity.java

Package com.example.multithreading;

import android.os.Bundle;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.os.Handler;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

@SuppressLint(“HandlerLeak”)

public class MainActivity extends Activity

private TextView tvOutput;

private static final int t1=1;

private static final int t2=2;


private static final int t3=3;

@Override

protected void onCreate(Bundle

savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tvOutput=(TextView)findViewById(R.id.textView2);

Button GD=(Button)findViewById(R.id.button1);

GD.setOnClickListener(new View.OnClickListener()

@Override

public void onClick(View v)

tvOutput.setText(“CS”);

thread1.start();

thread2.start();

thread3.start();

});

Thread thread1= new Thread(new Runnable()

@Override public void run()

{
for(int i=0;i<5;i++)

try

Thread.sleep(1005);

catch(InterruptedException e)

e.printStackTrace();

handler.sendEmptyMessage(t1);

});

Thread thread2= new Thread(new Runnable()

@Override public void run()

for(int i=0;i<5;i++)

try

Thread.sleep(1002);

catch(InterruptedException e)
{

e.printStackTrace();

handler.sendEmptyMessage(t2);

});

Thread thread3= new Thread(new Runnable()

Override public void run()

for(int i=0;i<5;i++)

try

Thread.sleep(1000);

catch(InterruptedException e)

e.printStackTrace();

handler.sendEmptyMessage(t3);

});
Handler handler = new Handler()

Public void handleMessage(android.os.Message

msg)

if(msg.what == t1)

tvOutput.append(“\n In thread1“);

if(msg.what == t2)

tvOutput.append(“\n In thread2”);

if(msg.what == t3)

tvOutput.append(“\n In thread3”);

};

7) Write a mobile application that creates alarm

clock

Activity. Xml

<?xml version=”1.0” encoding=”utf-8”?>

<Relativelayout xmlns:android=http://schemas.android.com/apk/res/android
Android:layout_width=”match_parent”

Android:layout_height=”match_parent”

tools:context=".MyActivity">

<TimePicker

Android:id=”@+id/timePicker”

Android:layout_width=”wrap_content”

Android:layout_height=”wrap_content”

Android:layout_gravity=”center” />

<ToggleButton

Android:id=”@+id/toggleButton”

Android:layout_width=”wrap_content”

Android:layout_height=”wrap_content”

Android:layout_gravity=”center”

Android:layout_margin=”20dp”

Android:checked=”false”

Android:onClick=”OnToggleClicked” />

</Relativelayout>

MAINACTIVITY. JAVA

package com.example.exno11;

import android.app.AlarmManager;

import android.app.PendingIntent;

import android.content.Intent;

import android.os.Bundle;

import android.app.Activity;

import android.view.View;
import android.widget.TimePicker;

import android.widget.Toast;

import android.widget.ToggleButton;

import java.util.Calendar;

public class MainActivity extends Activity

TimePicker alarmTimePicker;

PendingIntent pendingIntent;

AlarmManager alarmManager;

@Override

protected void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

alarmTimePicker = (TimePicker) findViewById(R.id.timePicker);

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

public void OnToggleClicked(View view)

long time;

if (((ToggleButton) view).isChecked())

Toast.makeText(MainActivity.this, "ALARM ON",

Toast.LENGTH_SHORT).show();

Calendar calendar = Calendar.getInstance();


calendar.set(Calendar.HOUR_OF_DAY,

alarmTimePicker.getCurrentHour());

calendar.set(Calendar.MINUTE,

alarmTimePicker.getCurrentMinute());

Intent intent = new Intent(this, AlarmReceiver.class);

pendingIntent = PendingIntent.getBroadcast(this, 0, intent,

0);

time=(calendar.getTimeInMillis()-

(calendar.getTimeInMillis()%60000));

if(System.currentTimeMillis()>time)

if (calendar.AM_PM == 0)

time = time + (1000*60*60*12);

else

time = time + (1000*60*60*24);

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time,

10000, pendingIntent);

else

alarmManager.cancel(pendingIntent);

Toast.makeText(MainActivity.this, "ALARM OFF",

Toast.LENGTH_SHORT).show();

}
}

AlarmReceiver. Java

package com.example.exno11;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.media.Ringtone;

import android.media.RingtoneManager;

import android.net.Uri;

import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver

@Override

public void onReceive(Context context, Intent intent)

Toast.makeText(context, "Alarm! Wake up! Wake up!",

Toast.LENGTH_LONG).show();

Uri alarmUri =

RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

if (alarmUri == null)

alarmUri =

RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

}
Ringtone ringtone = RingtoneManager.getRingtone(context,

alarmUri);

ringtone.play();

Manifest. Xml

<?xml version=”1.0” encoding=”utf-8”?>

<manifest xmlns:android=http://schemas.android.com/apk/res/android

Package=”com.example.exno11” >

<application

Android:allowBackup=”true”

Android:icon=”@mipmap/ic_launcher”

Android:label=”@string/app_name”

Android:supportsRtl=”true”

Android:theme=”@style/AppTheme” >

<activity android:name=”.MainActivity” >

<intent-filter>

<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

<receiver android:name=”.AlarmReceiver” >

</receiver>

</application>
8.Develop an app using Widgets

Activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
tools:ignore="HardcodedText" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_below="@+id/datePicker1"
android:layout_marginTop="44dp"
android:text="SUBMIT" />

<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp" />

</RelativeLayout>

MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {


Button submit;
DatePicker simpleDatePicker;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleDatePicker=(DatePicker)findViewById(R.id.datePick
er1);
submit=(Button)findViewById(R.id.button1);
submit.setOnClickListener(new
View.OnClickListener() {

@Override
public void onClick(View v) {
String
day="Day="+simpleDatePicker.getDayOfMonth();
String
month="month="+simpleDatePicker.getMonth();
String
year="year="+simpleDatePicker.getYear();

Toast.makeText(getApplicationContext(),day+"\n"+mon
th+"\n"+year,Toast.LENGTH_LONG).show();
}
});
}
}
10.Develop an app creates an alert upon receiving a message

Activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/andro
id"
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"
tools:ignore="HardcodedText" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="154dp"
android:text="CSBCA" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp"
android:text="SHOW_ALERT_BOX" />

</RelativeLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;

public class MainActivity extends Activity {


Button mainBtn;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBtn=(Button)findViewById(R.id.button1);
mainBtn.setOnClickListener(new
View.OnClickListener() {

@Override
public void onClick(View v) {
openAlert(v);
}
});
}
private void openAlert(View v){
AlertDialog.Builder adb=new
AlertDialog.Builder(MainActivity.this);
adb.setTitle("CSBCA");
adb.setMessage("Are you sure?");
adb.setPositiveButton("yes",new
DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface
dialog, int id) {
Toast.makeText(getApplicationContext(),"WELOCME TO
CSBCA-android App...!",Toast.LENGTH_LONG).show();
}
});
adb.setNegativeButton("No",new
DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface
dialog, int id) {
dialog.cancel();

Toast.makeText(getApplicationContext(),"you choose
a Negative answer...!",Toast.LENGTH_LONG).show();

}
});
adb.setNeutralButton("Exit().?",new
DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface
dialog, int id) {
MainActivity.this.finish();

}
});
AlertDialog Ad=adb.create();
Ad.show();
}
}

You might also like