0% found this document useful (0 votes)
14 views28 pages

Sample Question Paper Programs

The document contains multiple Android application development examples, including sending and receiving SMS, capturing images using the camera, sending emails, locating the user's current location, and creating a simple calculator using a table layout. Each example includes XML layout files and corresponding Java code for the main activity and other components. The applications demonstrate various functionalities such as user input handling, permissions, and UI design.

Uploaded by

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

Sample Question Paper Programs

The document contains multiple Android application development examples, including sending and receiving SMS, capturing images using the camera, sending emails, locating the user's current location, and creating a simple calculator using a table layout. Each example includes XML layout files and corresponding Java code for the main activity and other components. The applications demonstrate various functionalities such as user input handling, permissions, and UI design.

Uploaded by

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

1. Develop an application to send and receive SMS.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:hint="Enter phone Number" />
<EditText
android:id="@+id/edt2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:hint="Enter Message here"
android:layout_below="@+id/edt1"/>
<ImageButton
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/msg_foreground"
android:layout_below="@+id/edt1"
android:layout_toRightOf="@+id/edt1"
android:layout_toEndOf="@+id/edt1"
android:onClick="smsSendMessage"
tools:ignore="onClick"/>

</RelativeLayout>

MainActivity.java

package com.example.message;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
EditText e1,e2;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.edt1);
e2=(EditText)findViewById(R.id.edt2);

}
public void smsSendMessage(View view)
{
String destinstionAddress=e1.getText().toString();
String smsMessage=e2.getText().toString();
SmsManager smsManager=SmsManager.getDefault();
smsManager.sendTextMessage(destinstionAddress,null,smsMessage,null,null);
Toast.makeText(this,"Message Send Successfully",Toast.LENGTH_LONG).show();
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.message">

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />

<activity
android:name=".MapsActivity"
android:label="@string/title_activity_maps"></activity>
<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=".MysmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

</manifest>
MysmsReceiver.java

package com.example.message;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class MysmsReceiver extends BroadcastReceiver


{
private static final String TAG=MysmsReceiver.class.getSimpleName();
public static final String pdu_type="pdus";
String strMessage=" ";
int i;
@TargetApi(Build.VERSION_CODES.M)
@Override

public void onReceive(Context context, Intent intent)


{
Bundle bundle=intent.getExtras();
SmsMessage[] msgs;
String format=(String)bundle.get("format");
Object[] pdus=(Object[])bundle.get(pdu_type);
if (pdus!=null)
{
boolean isVersion=(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M);
msgs=new SmsMessage[pdus.length];
for (i=0;i<msgs.length;i++)
{
if (isVersion)
{
msgs[i]=SmsMessage.createFromPdu((byte[])pdus[i],format);

}
else
{
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
strMessage+="SMS
from"+msgs[i].getOriginatingAddress()+"\n"+msgs[i].getMessageBody();
Log.d(TAG,"onReceive:"+strMessage);
Toast.makeText(context,strMessage,Toast.LENGTH_LONG).show();
}
}

}
}
2. Write a program to capture an image using camera and display it.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Picture" />
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</LinearLayout>

MainActivity.java
package example.javatpoint.com.analogdigital.captureimage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity


{
static final int CAMERA_REQUEST=123;
ImageView i1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i1=(ImageView)findViewById(R.id.img1);
b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraintent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraintent,CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestcode,int resultcode,Intent data)
{
super.onActivityResult(requestcode,resultcode,data);
if(requestcode==CAMERA_REQUEST && resultcode==RESULT_OK)
{
Bitmap photo=(Bitmap) data.getExtras().get("data");
i1.setImageBitmap(photo);
}
}
}

3. Develop a program to send an email

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="@+id/edt1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="subject"
android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailSubject"
android:id="@+id/edt2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="message"
android:textAppearance="@style/TextAppearance.AppCompat.Large"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:id="@+id/edt3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send-mail"
android:layout_gravity="center_horizontal"
android:id="@+id/btn1"/>

</LinearLayout>
MainActivity.java
package com.example.jagdish_email;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity


{
private EditText e1;
private EditText e2;
private EditText e3;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.edt1);
e2=(EditText)findViewById(R.id.edt2);
e3=(EditText)findViewById(R.id.edt3);
b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String recipietList=e1.getText().toString();
String[] recepients=recipietList.split(",");
String subject=e2.getText().toString();
String message=e3.getText().toString();
Intent i1=new Intent(Intent.ACTION_SEND);
i1.putExtra(Intent.EXTRA_EMAIL,recepients);
i1.putExtra(Intent.EXTRA_SUBJECT,subject);
i1.putExtra(Intent.EXTRA_TEXT,message);
i1.setType("message/rfc822");
startActivity(Intent.createChooser(i1,"choose an email Client"));
}
});

}
}

4. Write a program to locate user’s current location.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</FrameLayout>
MainActivity.java
package com.example.maps;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;

public class MainActivity extends FragmentActivity


{
private static final String MAPFRAGTAG="MAPFRAGTAG";
private MapsActivity myMap;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if
((myMap=(MapsActivity)getFragmentManager().findFragmentByTag(MAPFRAGTAG))==null)
{
myMap=MapsActivity.newInstance();

getFragmentManager().beginTransaction().add(R.id.container,myMap,MAPFRAGTAG).commit();
}

}
}

activitymaps.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" />

MapsActivity.java
package com.example.maps;

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends MapFragment implements OnMapReadyCallback

{
private GoogleMap mMap=null;
public static MapsActivity newInstance()
{
MapsActivity fragment=new MapsActivity();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getMapAsync(this);

}
@Override
public void onActivityCreated(Bundle savedInstanceState) {

super.onActivityCreated(savedInstanceState);
setRetainInstance(this);
}
@Override
public void onResume() {
super.onResume();
doWhenMapIsReady();
}
@Override
public void onPause() {

super.onPause();
if (mMap!=null)
mMap.setMyLocationEnabled(false);
}

@Override
public void onMapReady(GoogleMap googleMap)
{
mMap=googleMap;
doWhenMapIsReady();

public void doWhenMapIsReady() {


if (mMap!=null&&isResumed())
mMap.setMyLocationEnabled(true);
}

public void setRetainInstance() {


setRetainInstance();
}

public void setRetainInstance(MapsActivity mapsActivity) {


}

public void getMapAsync(MapsActivity onMapReadyCallback) {


}

}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.maps">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyBRqTh2lRGDh1075wIAm4_VXY7tcZQsI_4" />

<activity
android:name=".MapsActivity"
android:label="Map"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
</application>

</manifest>

5. Develop a simple calculator using table layout.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:weightSum="4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
tools:context=".MainActivity">
<TableRow>
<EditText
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_weight="4"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="+"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="4"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="5"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="6"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="-"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="7"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="8"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="9"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="*"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="0"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="."
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="="
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="/"
android:layout_weight="1"/>
</TableRow>
</TableLayout>

6. Write a program to display circular progress bar.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Download"
android:textSize="50sp"
android:onClick="download"
tools:ignore="OnClick" />
</RelativeLayout>

MainActivity.java
package example.javatpoint.com.analogdigital.circularprogessbar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity


{
ProgressDialog p1;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.btn1);
}
public void download(View view)
{
p1=new ProgressDialog(this);
p1.setMessage("Downloading");
p1.setIndeterminate(true);
p1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
p1.setProgress(0);
p1.show();
}
}

7. write simple a program of custom_toast

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
/>

</RelativeLayout>

Custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_sentiment_satisfied_black_24dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Toast Notification"
android:layout_marginTop="15dp"
android:textStyle="bold"
android:textSize="20sp"
android:textColor="#FF9800"/>
</LinearLayout>
MainActivity.java
package example.javatpoint.com.analogdigital.custom_toast;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater in=getLayoutInflater();
View
layout=in.inflate(R.layout.custom_toast,(ViewGroup)findViewById(R.id.custom1));
Toast t1=new Toast(getApplicationContext());
t1.setGravity(Gravity.CENTER,0,0);
t1.setDuration(Toast.LENGTH_LONG);
t1.setView(layout);
t1.show();
}
});
}
}

8. Write a program to display following output using suitable layout.


activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:weightSum="3"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Row1"
android:layout_weight="3"/>
</TableRow>
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Row2&#10;col1"
android:layout_weight="0.75"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Row2&#10;col2"
android:layout_weight="3"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Row3&#10;col1"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Row3&#10;col2"
android:layout_weight="1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Row3&#10;col3"
android:layout_weight="1"/>
</TableRow>
<TableRow>
<Button
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Row4"
android:layout_weight="3"/>

</TableRow>

</TableLayout>
9. Develop the registration form using the following GUI.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">

<ImageView
android:layout_width="202dp"
android:layout_height="127dp"
android:layout_marginTop="10dp"
android:scaleType="center"
app:srcCompat="@drawable/sanjivani" />

<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#DDD9D9"
android:drawableLeft="@drawable/name"
android:hint="@string/name" />
<EditText
android:id="@+id/edt2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:background="#DDD9D9"
android:drawableLeft="@drawable/email"
android:hint="@string/email_id"/>
<EditText
android:id="@+id/edt3"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#DDD9D9"
android:drawableLeft="@drawable/password"
android:hint="@string/password"/>
<EditText
android:id="@+id/edt4"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:background="#DDD9D9"
android:drawableLeft="@drawable/confirmpass"
android:hint="@string/confirm_password"/>

<EditText
android:id="@+id/edt5"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#DDD9D9"
android:drawableLeft="@drawable/mobile"
android:hint="@string/enter_mobile" />

<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="60dp"
android:orientation="horizontal">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Male"
android:textSize="20sp" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Female"
android:textSize="20sp" />
</RadioGroup>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_marginTop="20dp"
android:background="#03A9F4"
android:textSize="20sp"/>

</LinearLayout>

MainActivity.java
package example.javatpoint.com.analogdigital.registration;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
Button b1;
EditText e1,e2,e3,e4,e5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.edt1);
e2=(EditText)findViewById(R.id.edt3);
e3=(EditText)findViewById(R.id.edt3);
e4=(EditText)findViewById(R.id.edt4);
e5=(EditText)findViewById(R.id.edt5);
b1=(Button)findViewById(R.id.btn1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String regetration = e1.getText().toString() + e2.getText().toString()


+ e3.getText() + e4.getText().toString() + e5.getText().toString();
Toast.makeText(getApplicationContext(),"Information Fill
successfully",Toast.LENGTH_LONG).show();

}
});
}
}

Output
10. Develop an android application to calculate age (Use Date Picker) using following GUI

activitymain.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="calculate"
android:layout_below="@+id/tvage"
android:layout_centerInParent="true"
android:layout_marginTop="80dp"
/>

<TextView
android:id="@+id/tvdob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DateOfBirth"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<TextView
android:id="@+id/tvage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Current Date"
android:layout_centerInParent="true"
android:layout_below="@+id/tvdob"/>
<DatePicker
android:id="@+id/datePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tvdob"
android:layout_marginRight="20dp"
android:layout_marginTop="30dp"/>

</RelativeLayout>

MainActivity.java
package example.javatpoint.com.analogdigital.calculateage;

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

import java.util.Calendar;

public class MainActivity extends Activity{


//Create Instance
DatePicker picker;
TextView tvdob;
TextView tvage;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Allocate memory
Button btnsubmit=(Button)findViewById(R.id.button1);
picker =(DatePicker) findViewById(R.id.datePicker1);
tvdob=(TextView)findViewById(R.id.tvdob);
tvage=(TextView)findViewById(R.id.tvage);

btnsubmit.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
tvdob.setText(getDateofBirth());
tvage.setText(currentDate());

}
});

public String getDateofBirth(){


StringBuilder builder=new StringBuilder();
builder.append("Date of Birth: ");
builder.append((picker.getMonth() + 1)+"/");//month is 0 based
builder.append(picker.getDayOfMonth()+"/");
builder.append(picker.getYear());
return builder.toString();
}

public String currentDate(){


StringBuilder todaydate=new StringBuilder();
Calendar today=Calendar.getInstance();
int age=today.get(Calendar.YEAR)-picker.getYear();
if (today.get(Calendar.MONTH) < picker.getYear()) {
age--;
} else if (today.get(Calendar.MONTH) == picker.getYear()
&& today.get(Calendar.DAY_OF_MONTH) < picker.getYear()) {
age--;
}
todaydate.append("Your Age: ");
todaydate.append(String.valueOf(age));
return todaydate.toString();
}

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

11. Develop an application to store student details like roll no, name, branch,
marks,percentage and retrieve student information using roll no. in SQLite databases.

Answer:
NOTE: This program is related to question but all content are not taken by me.

activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name"
android:textSize="20sp"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/edt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv1"/>

<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Roll Number"
android:layout_marginTop="40dp"
android:textSize="20sp"/>
<EditText
android:id="@+id/edt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv2"/>

<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="Marks"
android:textSize="20sp" />

<EditText
android:id="@+id/edt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/tv3" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:onClick="display"
android:text="Display"
android:textSize="20sp"
tools:ignore="OnClick" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_marginTop="80dp"
android:onClick="add"
android:text="Add"
android:textSize="20sp"
tools:ignore="OnClick" />
</LinearLayout>

MainActivity.java
package com.example.database;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity


{
Databasehelper mydb;
EditText e1,e2,e3;
Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText)findViewById(R.id.edt1);
e2=(EditText)findViewById(R.id.edt2);
e3=(EditText)findViewById(R.id.edt3);
b1=(Button)findViewById(R.id.btn1);
b2=(Button)findViewById(R.id.btn2);

public void add(View view)


{
boolean isInserted;
isInserted =
mydb.isInseartData(e1.getText().toString(),e2.getText().toString(),e3.getText().toStri
ng());
if (isInserted)
{
Toast.makeText(MainActivity.this,"Data
Inseratd",Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this,"Data Not
Inserted",Toast.LENGTH_LONG).show();

}
}
public void display(View view)
{
Cursor res=mydb.getAllData();
if (res.getCount()==0)
{
showMessage("Error","No Data Found");
return;
}
StringBuilder buffer=new StringBuilder();
while (res.moveToNext())
{
buffer.append("Id:"+res.getString(0)+"\n");
buffer.append("Name:"+res.getString(1)+"\n");
buffer.append("Roll No:"+res.getString(2)+"\n");
buffer.append("Marks"+res.getString(3)+"\n");

}
showMessage("Data",buffer.toString());

public void showMessage(String title, String Message)


{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(Message);
builder.show();
}
}
Databasehelper.java
package com.example.database;

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

public class Databasehelper extends SQLiteOpenHelper


{

public static final String DATABASE_NAME = "student.db";


public static final String TABLE_NAME = "student.table";
public static final String COL_1 = "ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "ROLLNO";
public static final String COL_4 = "MARK";

public Databasehelper(Context context) {


super(context, DATABASE_NAME, null, 1);

@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL("CREATE TABLE " + TABLE_NAME + "("
+ COL_1 + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COL_2 + "TEXT,"
+ COL_3 + "INTEGER" + ");");

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);

public boolean isInseartData(String name, String roll, String marks) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name);
contentValues.put(COL_3, roll);
contentValues.put(COL_4, marks);

long result;
result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}

public Cursor getAllData() {


SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return res;
}

12. Observe the following GUI and write an XML file using relative layout to create the
same.

activitymain.xml

You might also like