FINAL Android Programming Lab 1 To 10 Programs

You might also like

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

SRI RAMAKRISHNA MISSION VIDYALAYA

COLLEGE OF ARTS AND SCIENCE


[AUTONOMOUS]

Accredited by NAAC with A+ Grade


Coimbatore – 641 020

April – 2024

DEPARTMENT OF COMPUTER APPLICATIONS

NAME :

REG. NO :

SEMESTER : VI

SUBJECT : Android Programming Lab

SUBJECT CODE : 20UCA6CP9

1
SRI RAMAKRISHNA MISSION VIDYALAYA
COLLEGE OF ARTS AND SCIENCE
[AUTONOMOUS]

Accredited by NAAC with A+ Grade


Coimbatore – 641 020

DEPARTMENT OF COMPUTER APPLICATIONS


BONAFIDE CERTIFICATE
REGISTER NO:______________
This is to certify that it is a bonafide record work done by
_________________________________in “Android Programming Lab”
(20UCA6CP9) for the VI Semester during the Academic year 2023 - 2024.
Submitted for the Semester Practical Examinations held on ______________.

Head of the Department Staff in Charge

Internal Examiner External Examiner

2
INDEX

S. No TITLE OF THE PROGRAM PAGE NO


ANDROID PROGRAM USING ECLIPSE
1 Edit Text and Text View

2 Web view Using Load Url


3 Passing Values from One Form to another Form
4 Addition of Two Number
5 Context Menu
6 Phone Call
7 Notification
8 Check Network Connection
9 Date Picker Control
10 Button with Color Text

3
Ex No. 1
Edit Text and Text View

AIM:

To create the android program to Enter the Text and Display the Text using Text and
Button Widget in ecplise android sdk package.

Algorithm:
1. Start the process.

2. Create the new android application with package com.web.

3. Create edit text for website url ,button and web view for load.

4. Type the code in MainActivity.java file to Enter the Text into Display below by Click

the button

5. Type the code in activity_main.xml file to display the text.

6. Stop the process.

4
MainActivity.java

public class MainActivity extends Activity implements OnClickListener {


{
Button b1;
EditText e1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstancedState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.button1);
e1=(EditText)findViewById(R.id.editText1);
t1=(TextView)findViewById(R.id.textView2);
b1.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String str=e1.getText().toString();
t1.setText(str);
}

activity_main.xml

<EditText
android:id=”@+id/editText1”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alighParentLeft=”true”
android:layout_alighParentTop=”true”
android:ems=”10” >

<requestFocus />
</EditText>

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

5
android:layout_alignLeft=”@id/editText1”
android:layout_below=”@id/editText1”
android:layout_marginLeft=”67dp”
android:layout_marginTop=”43dp”
android:text=”Go: />

<EditText
android:id=”@+id/editText2”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alighParentLeft=”true”
android:layout_below=”@+id/button1”
android:layout_marginTop=”37dp”
android:text=”Your enter text is:”
android:textAppearance=”?android:attr/textAppearanceMedium” />

<requestFocus />
</EditText>

</RelativeLayout>

Output:

Result
Thus the Program was Successfully Executed and Verified
6
Ex No. 2
WEBVIEW USING LOAD URL

AIM:

To create the android program to load the website in the web view using android sdk
package.

Algorithm:

7. Start the process.

8. Create the new android application with package com.web.

9. Create edit text for website url ,button and web view for load.

10. Create the onclicklistener for button.

11. Get the website url as input value from the edit text boxe.

12. Add the permission in manifest for android.permission.INTERNET.

13. Load the website url in the webview by calling loadUrl() method.

14. Stop the process.

7
MainActivity.java
package com.Mysite.Myweb;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class MainActivity extends Activity


{
private WebView MyWeb;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyWeb = (WebView) findViewById(R.id.WebView);
MyWeb.getSettings().setJavaScriptEnabled(true);
MyWeb.getSettings().setSaveFormData(true);
MyWeb.loadUrl("https://www.google.com/");
}
}
activity_main.xml
<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"
tools:context="com.Mysite.Myweb.MainActivity" >

<WebView
android:id="@+id/WebView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</RelativeLayout>

Android Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.Mysite.Myweb"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk

8
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</application>

</manifest>

Output:

Result:
Thus the Program was Successfully Executed and Verified

9
Ex No. 3
Passing values Over Pages

AIM:
To create the android program to pass the form values from one page to another page
using android sdk package.

Algorithm:

1. Start the process.

2. Create the new android application by clicking File ->new->Android application

project.

3. Create two edit text for user name and password and button for send.

4. Create the calllogin(onclick) for the button.

5. Get the username and password as input values from the edit text boxes.

6. Add the input values into the onCreate(Bundle savedInstanceState) .

7. Check whether the username and password was correct in calllogin()

8. Create a sub activity and second xml file for the second form.

9. Display “Welcome” in the second form after login.

10. Stop the process.

10
MainActivity.java

public class MainActivity extends Activity {


EditText uname,pwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname=(EditText) findViewById(R.id.edt_username);
pwd=(EditText) findViewById(R.id.edt_password);
}

public void calllogin(View v)


{
if((uname.getText().toString()).equalsIgnoreCase("Admin") &&
(pwd.getText().toString()).equalsIgnoreCase("admin"))
{
Intent i =new Intent(this,SubActivity.class);
startActivity(i);
}
else
{
Toast.makeText(getBaseContext(), "Username or Password is incorrect",
Toast.LENGTH_SHORT).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);

11
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Acitivity_main.xml

<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="com.example.passingvalueoneformtoanotherform.MainActivity" >

<EditText
android:id="@+id/edt_username"
android:layout_width="match_parent"

12
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="Username" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/edt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/edt_username"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edt_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="27dp"
android:onClick="calllogin"
android:text="Login" />

13
</RelativeLayout>

SubActivity.java

public class SubActivity extends MainActivity {


TextView username;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
username=(TextView) findViewById(R.id.textView1);
username.setText("WELCOME");
}
}

Activity_second.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" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="116dp"
android:layout_marginTop="70dp"

14
android:text="TextView" />

</RelativeLayout>

Output:

Result:
Thus the above program was Successfully Executed and Verified.

15
Ex No. 4
Addition of Two Numbers

AIM:
To create the addition of two numbers program using android sdk package.

Algorithm:

1. Start the process.

2. Create the new android application with package com.add.

3. Create two edit text, text view and button.

4. Create the onclicklistener for button.

5. Get the input values from the edit text boxes.

6. Add the two values.

7. Display the result using text view.

8. Stop the process.

16
MainActivity.java
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
final EditText et1 = (EditText) findViewById(R.id.editText1);
final EditText et2 = (EditText) findViewById(R.id.editText2);
final TextView result = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
int x = Integer.valueOf(et1.getText().toString());
int y = Integer.valueOf(et2.getText().toString());
int sum = x + y;
result.setText("The Sum of " + x + " and " + y + " is " + sum);
}
});
}
}

Acivity main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Addition of 2 number" />

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" >

<requestFocus />
</EditText>

17
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ADD" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

18
Output:

Result:
Thus the Program was Successfully Executed and Verified.

19
Ex No. 5
Context Menu

Aim
To create an android application program for Context Menu implementation
using Eclipse IDE

Algorithm
Step 1 : Start The Process
Step 2 : Create MainActivity.java for displaying the Context Menu
Button, and Click Long Press
Step 3 : Add a code to XML File for Display the Context Menu
Button, and Click Long Press
Step 4 : In MainActivity.java file add some block of code through source,
Override/Implement Methods as
OnCreateContextMenu(ContextMenu, View,
ContextMenuMethods)

Step 5 : Run this program by Right Click the Context Menu Android
Application Name in the Package Explorer.

Step 6 : Long Press the Context Menu Button it will display the Context
Menu in the Screen and By Clicked the button as Long Press it
will shows the Text SMS or Call by worked on Toast function.

Step 7 : Stop The Process

20
Main Activity.java

public class ContextActivity extends Activity {


Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_context);
{
btn=(Button) findViewById(R.id.button1);
//register button into context
registerForContextMenu(btn);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("Select the
Action");
menu.add("Call");
menu.add("SMS");
}

@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getTitle()=="Call")
{
Toast.makeText(getApplicationContext(), "Calling Code", 500).show();
}

21
else if(item.getTitle()=="SMS")
{
Toast.makeText(getApplicationContext(), "SMS Sending", 500).show();

}
else
{
return false;
return true;

activity_context.xml
<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=".ContextActivity" >

<Button
android:id="@+id/buttonl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" android:layout_alignParentTop="true"
android:layout_marginLeft="76dp"
android:layout_marginTop="80dp"
android:text="Context Menu" />

TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:text="Long Press in this buttont
android:textAppearance"?android:attr/textAppearance Small" />
</RelativeLayout>

22
Output

Result
Thus the Program was successfully Executed and Verified

23
Ex No. 6
Phone Call Using AVD

Aim :
To create the android program to make a phone call using AVD.
Algorithm :
Step 1 : Start The Process
Step 2 : Create Two Android Virtual Machines (AVD)
Step 3 : Start Two Android Virtual Machines Those Before , We Created
Step 4 : Start The Phone Call Application In First AVD
Step 5 : Dial The Second AVD’s Number in The Phone Call Application
Step 6 : Press The Call Button It Will Make a Call to second AVD
Step 7 : Stop The Process

MainActivity.java
package example.callprogram;

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

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {

24
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

25
Activity_main.xml
<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="example. callprogram.MainActivity" >
</RelativeLayout>

Output :

26
Result
Thus the Program was Successfully Executed and Verified Successfully.

27
Ex No. 7
Notification

Aim :
To Create The Android Program To Make The Notification Demo in Using
AVD
Algorithm :
Step 1 : Start The Process
Step 2 : Create The New Android Application With Package
com.notificationdemo
Step 3 : Adding Button To Screen With Using Palatte Window In
activity_main.xml
Step 4 : Type The Code In MainActivity.java File To Display
Notification By Touching The Button
Step 5 : Type The Code In Onclick() Method
NotificationManager notimanger =
(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder ncb = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("From John")
.setContentText("Welcome And Hi ....");
notimanger.notify(001,ncb.build());

Step 6 : Stop The Process

28
MainActivity.java
package example.notification_text;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {


Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button1);
t1 = (TextView)findViewById(R.id.textView1);
b1.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

29
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try{

NotificationManager notimanger = (NotificationManager)


getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder ncb = new
NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("From John")
.setContentText("Welcome And Hi ....");
notimanger.notify(001,ncb.build());
}
catch(Exception e){
t1.setText("Error"+e);
}
}
}

Activity_main.xml
<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"

30
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="example.notification_text.MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="106dp"
android:text="Simple Text Notification" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />

</RelativeLayout>

Output :

31
Result
Thus the Program was Successfully Executed and Verified.

32
Ex No. 8
Check Internet Connection

Aim :
To Create the android program to using Check the internet connected or not
by using Eclipse IDE

Algorithm :
Step 1 : Start The Process
Step 2 : Create The New Android Application With Package com.anytext
Step 3 : Create And Design The Screen Using EditText Using With Paletee
Window
Step 4 : Type The Code in MainActivity.java
ConnectivityManager conn = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = conn.getActiveNetworkInfo();
if(ni!=null && ni.isConnected()){
t1.setText("Connected");
}
else{
t1.setText("Not Connected");
}
Step 5 : Type The Code in AndroidManifest.xml
<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"></uses-
permission>
Step 6 : Press The Call Button It Will Make a Call to second AVD
Step 7 : Stop The Process
33
MainActivity.java
package com.example.checkinternet;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
TextView t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView)findViewById(R.id.textView1);
ConnectivityManager conn = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = conn.getActiveNetworkInfo();
if(ni!=null && ni.isConnected()){
t1.setText("Connected");
}
else{
t1.setText("Not Connected");
}
}
@Override

34
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Activity_main.xml
<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="com.example.checkinternet.MainActivity" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"

35
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="90dp"
android:layout_marginTop="106dp"
android:text="TextView" />

</RelativeLayout>

AnroidManifest.xml

<uses-permission
android:name="android.permission.INTERNET"></uses-permission>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"></us
es-permission>

Output :

Result
Thus the Program was Successfully Executed and Verified.

36
Ex No. 9
Date Picker

Aim :
To Create the android program to select the date and view in textview by
using eclipse ide

Algorithm :
Step 1 : Start The Process
Step 2 : Create The New Android Application With Package com.anytext
Step 3 : Create And Design The Screen Using TextView,Button,DatePicker
Using With Paletee Window
Step 4 : Type The Xml Code in activity_main.xml
<DatePicker
android:spinnersShown="true"
android:calendarViewShown="false"
android:layout_marginBottom="86dp" />
Step 5 : Type The Code in MainActivity.java
public void onClick(View v)
{
// TODO Auto-generated method stub
t1.setText(getdate());
}
public String getdate()
{
StringBuffer data = new StringBuffer();
data.append("Currect Date Is ");
data.append(d1.getDayOfMonth()+"/");
data.append((d1.getMonth()+1)+"/");
data.append(d1.getYear());
return data.toString();
}
Step 6 : Stop The Process

37
Activity_main.xml
<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="com.example.datetimepickerprogram.MainActivity" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="152dp"
android:text="Pick This Date" />

<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:spinnersShown="true"
android:calendarViewShown="false"
android:layout_marginBottom="86dp" />

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/datePicker1"
android:layout_below="@+id/button1"
android:layout_marginTop="54dp"
android:text="TextView" />
</RelativeLayout>

MainActivity.java

38
package com.example.datetimepickerprogram;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {


DatePicker d1;
Button b1;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button1);
d1 = (DatePicker)findViewById(R.id.datePicker1);
t1 = (TextView)findViewById(R.id.textView1);
b1.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.

39
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t1.setText(getdate());
}
public String getdate(){
StringBuffer data = new StringBuffer();
data.append("Currect Date Is ");
data.append((d1.getMonth()+1)+"/");
data.append(d1.getDayOfMonth()+"/");
data.append(d1.getYear());
return data.toString();
}
}

40
Output

Result
Thus the Program was Successfully Executed and Verified.

41
Ex No. 10
Button with Color Text

Aim :
To create the android program to select the button view in button name in
textview by using eclipse IDE.
Algorithm :
Step 1 : Start The Process

Step 2 : Create The New Android Application With Package com.anytext

Step 3 : Create And Design The Screen Using Three Button Using With
Paletee Window

Step 4 : Type The Code in MainActivity.java


b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);

Step 5 : Given The Variable and Values in Strings.xml


Step 6 : Given The Style in style.xml
Step 7 : Stop The Process

42
MainActivity.java
package com.example.buttonswithcolor;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {


Button b1,b2,b3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long

43
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.getId()==R.id.button1){
Toast t = Toast.makeText(MainActivity.this,
b1.getText()+"Clicked", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 20, 20);
t.show();
}
if(arg0.getId()==R.id.button2){
Toast t = Toast.makeText(MainActivity.this,
b2.getText()+"Clicked", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 20, 20);
t.show();
}
if(arg0.getId()==R.id.button3){
Toast t = Toast.makeText(MainActivity.this,
b3.getText()+"Clicked", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 20, 20);
t.show();
}

}
}

44
Activity_main.xml
<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="com.example.buttonswithcolor.MainActivity" >

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:layout_marginLeft="90dp"
android:layout_marginTop="31dp"
android:background="@color/bgcolor"
android:padding="10sp"
android:text="blue"
android:textColor="@color/bblue"
android:textSize="30sp"
android:textStyle="bold" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button3"
android:layout_centerVertical="true"
android:background="@color/bgcolor"
android:padding="10sp"
android:text="green"
android:textColor="@color/bgreen"
android:textSize="30sp"
android:textStyle="bold" />

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

45
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_alignLeft="@+id/button2"
android:layout_marginBottom="32dp"
android:background="@color/bgcolor"
android:padding="10sp"
android:text="red"
android:textColor="@color/bred"
android:textSize="30sp"
android:textStyle="bold" />

</RelativeLayout>

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

<string name="app_name">ButtonsWithColor</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="red">Red</string>
<string name="green">Green</string>
<string name="blue">Blue</string>

</resources>

Styles.xml
<color name="bred">#ff5733</color>
<color name="bgreen">#145A32</color>
<color name="bblue">#1B4F72</color>
<color name="bgcolor">#F7dc67</color>

46
Output :

Result
Thus the Program was Successfully Executed and Verified.

47

You might also like