Android Program - Edited
Android Program - Edited
Android Program - Edited
and colors.
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
<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;
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;
}
}
AndroidManifest.xml
<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
Activity_main. Xml
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;
EditText txtData1,txtData2;
float num1,num2,result1,result2;
@Override
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()
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()
txtData1.setText("");
txtData2.setText("");
});
}
4) Write an application that draws basic graphical
Activity. Xml
<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;
super.onCreate(savedInstanceState);
setContentView(new myview(this));
super(context);
super.onDraw(canvas);
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);
Activity_main. Xml
<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”)
@Override
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
tvOutput.setText(“CS”);
thread1.start();
thread2.start();
thread3.start();
});
{
for(int i=0;i<5;i++)
try
Thread.sleep(1005);
catch(InterruptedException e)
e.printStackTrace();
handler.sendEmptyMessage(t1);
});
for(int i=0;i<5;i++)
try
Thread.sleep(1002);
catch(InterruptedException e)
{
e.printStackTrace();
handler.sendEmptyMessage(t2);
});
for(int i=0;i<5;i++)
try
Thread.sleep(1000);
catch(InterruptedException e)
e.printStackTrace();
handler.sendEmptyMessage(t3);
});
Handler handler = new Handler()
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”);
};
clock
Activity. Xml
<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;
TimePicker alarmTimePicker;
PendingIntent pendingIntent;
AlarmManager alarmManager;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
long time;
if (((ToggleButton) view).isChecked())
Toast.LENGTH_SHORT).show();
alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE,
alarmTimePicker.getCurrentMinute());
0);
time=(calendar.getTimeInMillis()-
(calendar.getTimeInMillis()%60000));
if(System.currentTimeMillis()>time)
if (calendar.AM_PM == 0)
else
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time,
10000, pendingIntent);
else
alarmManager.cancel(pendingIntent);
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;
@Override
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
<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” >
<intent-filter>
</intent-filter>
</activity>
</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;
@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;
@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();
}
}