0% found this document useful (0 votes)
157 views35 pages

Mad Practical Codes

Uploaded by

Mayur Galani
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)
157 views35 pages

Mad Practical Codes

Uploaded by

Mayur Galani
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/ 35

MAD PRACTICAL EXAM

1. Develop a program to implement linear layout and absolute layout.

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

<!-- Linear Layout -->


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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView in a Linear Layout" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me" />
</LinearLayout>

<!-- Absolute Layout -->


<AbsoluteLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView in an Absolute Layout"
android:layout_x="100dp"
android:layout_y="100dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_x="200dp"
android:layout_y="200dp"/>
</AbsoluteLayout>

</LinearLayout>

2. Develop a program to implement frame layout, table layout and relative layout.

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

<!-- FrameLayout -->


<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a TextView in a FrameLayout"
android:layout_gravity="center"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:layout_gravity="bottom|end"/>
</FrameLayout>

<!-- TableLayout -->


<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name"/>
</TableRow>

<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your email"/>
</TableRow>
</TableLayout>

<!-- RelativeLayout -->


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
android:layout_alignParentStart="true"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
android:layout_centerHorizontal="true"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
android:layout_alignParentEnd="true"/>
</RelativeLayout>

</RelativeLayout>

3. Develop Program to implement Student Registration Form.

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

<TextView
android:id="@+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:layout_marginTop="20dp"/>

<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_below="@+id/heading"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="20dp"/>

<EditText
android:id="@+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_marginLeft="20dp"
android:layout_below="@id/heading"/>
<TextView
android:id="@+id/ageText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:layout_below="@+id/nameText"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>

<EditText
android:id="@+id/age"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_marginLeft="20dp"
android:layout_below="@id/name"
android:inputType="number"/>

<TextView
android:id="@+id/emailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:layout_below="@+id/ageText"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"/>

<EditText
android:id="@+id/email"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_marginLeft="20dp"
android:layout_below="@id/age"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/btn"
android:layout_below="@id/email"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="submit"
android:inputType="textEmailAddress"/>

</RelativeLayout>

JAVA:
package com.example.pr3_registrationform;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

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

public void submit(View v){


Toast.makeText(this, "Registered", Toast.LENGTH_SHORT).show();
}
}
4. Develop a program to implement Button, Image Button and Toggle Button.

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"
tools:context=".MainActivity"
android:orientation="vertical">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
android:onClick="btnClick"/>

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"
android:onClick="imgClick"
android:layout_marginTop="20dp"/>

<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="toggle"
android:textOff="Click to show image"
android:textOn="Click to hide image"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img"/>

</LinearLayout>
Java:
package com.example.pr4_btnimg_btntoggle_btn;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


ToggleButton tb;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb = findViewById(R.id.toggle);
iv = findViewById(R.id.img);
}

public void btnClick(View v){


Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show();
}

public void imgClick(View v){


Toast.makeText(this, "Image Button Clicked", Toast.LENGTH_SHORT).show();
}

public void toggle(View v){


if(!tb.isChecked()){
iv.setImageResource(0);
}
else{
iv.setImageResource(R.drawable.ic_launcher_background);
}
}
}
5. Develop a program to implement a login window using above UI controls.

Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical" >

<EditText
android:id="@+id/editTextUsername"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />

<EditText
android:id="@+id/editTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/buttonLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="send"/>

</LinearLayout>

JAVA:
package com.example.pr5_loginwindow;

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 {
EditText t1,t2;

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

t1=findViewById(R.id.editTextUsername);
t2=findViewById(R.id.editTextPassword);

}
public void send(View v){
String username = t1.getText().toString();
String password = t2.getText().toString();

if (username.equals("admin") && password.equals("password")) {


Toast.makeText(MainActivity.this, "Login successful",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Login failed. Please try again.",
Toast.LENGTH_SHORT).show();
}
}

}
6. Develop a program to implement Progress Bar.

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

<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:scaleX="4"
android:scaleY="4"
android:progress="0"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pb"
android:layout_marginTop="80dp"
android:layout_centerHorizontal="true"
android:text="Start"
android:onClick="start"/>

</RelativeLayout>

JAVA:
package com.example.pr6_progressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int progress = 0;
ProgressBar pb;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = findViewById(R.id.pb);
}

public void start(View v){


Thread t = new Thread(new Runnable() {
@Override
public void run() {
while(progress <= 100){
try {
progress += 10;
pb.setProgress(progress);
Thread.sleep(200);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
});
t.start();
Toast.makeText(this, "Starting", Toast.LENGTH_SHORT).show();
}
}
7. Develop a program to implement List View, Grid View, Image View and Scroll View

Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">

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

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" />

<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:src="@drawable/img" />

<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="952dp"
android:layout_marginTop="60dp"
android:entries="@array/cricketers" />

</LinearLayout>

</ScrollView>
btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>

strings.xml:
<resources>
<string name="app_name">pr7_list,grid,image_view</string>
<string-array name="cricketers">
<item>ROHIT</item>
<item>VIRAT</item>
<item>DHONI</item>
<item>ROHIT</item>
<item>VIRAT</item>
<item>DHONI</item>
</string-array>
</resources>

MainActivity.java:
package com.example.pr7_listgridimage_view;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


String[] buttonNames = new String[2]; // Modify array size to 2

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

// Populate buttonNames array with two elements


buttonNames[0] = "Grid 1";
buttonNames[1] = "Grid 2";

GridView gridView = findViewById(R.id.gridView);


ArrayAdapter<String> adapter = new ArrayAdapter<>(
this,
R.layout.btn,
R.id.btn,
buttonNames
);
gridView.setAdapter(adapter);
}
}

8. Develop a program to implement new activity using explicit intent and implicit intent.

Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/explicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Explicit Intent" />

<Button
android:id="@+id/implicitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Implicit Intent" />

</LinearLayout>

MainActivity.java:
package com.example.pr8_intents;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {

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

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


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

explicitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Explicit Intent: Start SecondActivity
Intent explicitIntent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(explicitIntent);
}
});

implicitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Implicit Intent: Open a web page
Intent implicitIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.google.com"));
startActivity(implicitIntent);
}
});
}
}

Activity_second.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:gravity="center"
android:backgroundTint="#039BE5">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello User...You have selected explicit intent"
android:textSize="20dp" />

</LinearLayout>

SecondActivity.java:
package com.example.pr8_intents;

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
9. Develop a program to implement sensors.

Activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical"
android:id="@+id/linearlayout" >

</LinearLayout>

MainActivity.java:
package com.example.pr9_sensors;

import androidx.appcompat.app.AppCompatActivity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = findViewById(R.id.linearlayout);
if(getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_PORTRAIT){
linearLayout.setBackgroundColor(Color.BLUE);
}
else{
linearLayout.setBackgroundColor(Color.CYAN);
}
}
}

10. Develop a program to build Camera.

Activity_main.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"
tools:context=".MainActivity"
android:orientation="vertical">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/image2"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:text="Capture Image"
android:onClick="capture"/>

</LinearLayout>
MainActivity.java:
package com.example.pr10_camera;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

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

public class MainActivity extends AppCompatActivity {


ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = findViewById(R.id.image2);
}

public void capture(View v){


Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, 10);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {


if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
100);
}
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 10 && resultCode == RESULT_OK){
Bitmap b = (Bitmap)data.getExtras().get("data");
iv.setImageBitmap(b);
}
}
}
11. Develop a program for providing Bluetooth connectivity.

Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/buttonOn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Bluetooth On"
android:onClick="on"/>
<Button
android:id="@+id/buttonOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn Bluetooth Off"
android:onClick="off"/>
</LinearLayout>

Java:
package com.example.pr11_bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b1, b2;
private BluetoothAdapter BA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.buttonOn);
b2 = findViewById(R.id.buttonOff);
BA = BluetoothAdapter.getDefaultAdapter();
}
public void on(View v) {
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turning on",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}
public void off(View v) {
if (BA.isEnabled()) {
BA.disable();
Toast.makeText(getApplicationContext(), "Turning off",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Already off",
Toast.LENGTH_LONG).show();
}
}
}

Manifest:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
13. Create sample application with login module. (Check username and password) On
successful login, Change Text View “Login Successful” and on login fail, alert user using Toast
“Login fail”.

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:id="@+id/unameText"/>

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/uname"
android:layout_toRightOf="@id/unameText"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:layout_marginLeft="20dp"
android:id="@+id/passText"
android:layout_below="@id/unameText"/>

<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/password"
android:layout_toRightOf="@id/unameText"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:layout_below="@id/uname"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_below="@id/password"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:onClick="submit"/>

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

</RelativeLayout>

Java:
package com.example.pr13;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


EditText e1, e2;
TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = findViewById(R.id.uname);
e2 = findViewById(R.id.password);
t1 = findViewById(R.id.text);
}

public void submit(View v){


if(e1.getText().toString().equals("admin") &&
e2.getText().toString().equals("admin123")){
t1.setText("Login Successful");
}
else{
Toast.makeText(this, "Login Failed", Toast.LENGTH_SHORT).show();
}
}
}
14. Create login application where you will have to validate username and password till the
username and password is not validated, login button should remain disabled.

Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/usernameInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />

<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_gravity="center_horizontal"
android:enabled="false"
android:onClick="login"/>

</LinearLayout>
Java:
package com.example.pr14;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText u, p;
private Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
u = findViewById(R.id.usernameInput);
p = findViewById(R.id.passwordInput);
b = findViewById(R.id.loginButton);
TextWatcher loginTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
checkFieldsForEmptyValues();
}
@Override
public void afterTextChanged(Editable s) {
}
};
u.addTextChangedListener(loginTextWatcher);
p.addTextChangedListener(loginTextWatcher);
}
private void checkFieldsForEmptyValues() {
String username = u.getText().toString();
String password = p.getText().toString();
b.setEnabled(!username.isEmpty() && !password.isEmpty());
}
public void login(View v){
String username = u.getText().toString();
String password = p.getText().toString();
if(username.equals("admin") && password.equals("123"))
Toast.makeText(this, "Login successful", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Invalid details", Toast.LENGTH_SHORT).show();
}
}

15. Develop a program to a) Send SMS b) Receive SMS

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

<EditText
android:id="@+id/mblTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Mobile No"
android:inputType="number"/>

<EditText
android:id="@+id/msgTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />

<Button
android:id="@+id/btnSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"
android:onClick="sendMessage"/>

</LinearLayout>
Java:
package com.example.pr15_sms;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


EditText phoneNo, message;
private static final int PERMISSION_REQUEST_SMS = 0;

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

phoneNo = findViewById(R.id.mblTxt);
message = findViewById(R.id.msgTxt);

if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new
String[]{Manifest.permission.SEND_SMS}, PERMISSION_REQUEST_SMS);
}
}

public void sendMessage(View v)


{
String phoneNumber = phoneNo.getText().toString();
String msg = message.getText().toString();

if(!phoneNumber.isEmpty() && !msg.isEmpty())


{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber,null,msg,null,null);
Toast.makeText(this, "Message Sent", Toast.LENGTH_SHORT).show();
}

else
{
Toast.makeText(this, "Please enter phone no. and message",
Toast.LENGTH_SHORT).show();
}
}
}

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

16. Develop a program to send and receive e-mail.

Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="To:" />

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subject:" />

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message:" />

<EditText
android:id="@+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:lines="10"/>

<Button
android:id="@+id/button_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:text="Executed By Sagar Chanchlani - 33"
android:textStyle="bold"
android:layout_marginLeft="30dp" />

</LinearLayout>

Java:
package com.example.pr16_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 mEditTextTo, mEditTextSubject, mEditTextMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditTextTo = findViewById(R.id.edit_text_to);
mEditTextSubject = findViewById(R.id.edit_text_subject);
mEditTextMessage = findViewById(R.id.edit_text_message);
Button buttonSend = findViewById(R.id.button_send);
buttonSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMail();
}
});
}
private void sendMail() {
String recipientList = mEditTextTo.getText().toString();
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipientList});
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
}

Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SEND_EMAIL" />
18. Deploy map-based application. Part II(Source To Destination)

Xml:
<LinearLayout
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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_source"
android:hint="Enter Source Location"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_destination"
android:hint="Enter Destination Location"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/bt_track"
android:text="Display Location"/>
</LinearLayout>

Java:
package com.example.pr18_sourcetodestination;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
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 {
EditText etSource, etDestination;
Button btTrack;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etSource = findViewById(R.id.et_source);
etDestination = findViewById(R.id.et_destination);
btTrack = findViewById(R.id.bt_track);
btTrack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String sSource = etSource.getText().toString().trim();
String sDestination = etDestination.getText().toString().trim();
if (sSource.isEmpty() || sDestination.isEmpty()) {
Toast.makeText(MainActivity.this, "Please enter both locations.",
Toast.LENGTH_SHORT).show();
} else {
displayTrack(sSource, sDestination);
}
}
});
}
private void displayTrack(String sSource, String sDestination) {
Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + sSource + "/" +
sDestination);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setPackage("com.google.android.apps.maps");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Uri playStoreUri =

Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
Intent playStoreIntent = new Intent(Intent.ACTION_VIEW, playStoreUri);
startActivity(playStoreIntent);
}
}
}
Manifest:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

You might also like