Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 216

Practical No.

4
Exercise
1.write a program to display Hello World.

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


<androidx.constraintlayout.widget.ConstraintLayout
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="Hello World!"
android:textSize="34sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

Output:
2. Write a program to display student name and marks.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.134"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.133" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Marks"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.107"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.268" />

<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="28dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.926"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.117"
android:hint="name"/>

<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textSize="14sp"
android:hint="marks"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.935"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.254" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Practical No.5
Exercise
1.Write a program to place Name , Age and Mobile Number linearly
(Vertical) on the display screen using linear layout.
<?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/textView1"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginTop="25dp"
android:text=" Name"
android:textSize="30sp"
android:textStyle="bold" />

<EditText
android:id="@+id/edittext1"
android:layout_width="100dp"
android:layout_height="49dp"
android:layout_marginTop="35dp"
android:textSize="20sp"
android:hint="Name"/>
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="44dp"
android:layout_marginTop="25dp"
android:text=" Age"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/edittext"
android:layout_width="50dp"
android:layout_height="49dp"
android:layout_marginTop="35dp"
android:textSize="20sp"
android:hint="Age"/>

Output:
2.Write a program to place Name ,Age and Mobile number centrally
on the display screen using Absolute layout.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="35dp"
android:layout_y="60dp"
android:text="Name"
android:textSize="20dp"
android:textStyle="bold" />

<EditText
android:layout_width="152dp"
android:layout_height="55dp"
android:layout_x="202dp"
android:layout_y="62dp"
android:hint="Name"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="418dp"
android:text="Mob No."
android:textSize="20dp"
android:textStyle="bold" />

<EditText
android:layout_width="152dp"
android:layout_height="55dp"
android:layout_x="214dp"
android:layout_y="233dp"
android:hint="Age"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="43dp"
android:layout_y="232dp"
android:text="Age"
android:textSize="20dp"
android:textStyle="bold" />

<EditText
android:layout_width="152dp"
android:layout_height="55dp"
android:layout_x="218dp"
android:layout_y="417dp"
android:hint="Mob No"/>

</AbsoluteLayout>
Practical No.6
Exercise
1. Write a program to display 10 students basic information in a
table form using table layout.

<?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">
<TableRow android:background="#0079D6" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sr.No" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Roll No" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Niranjani" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="65" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="18" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pranav" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="67" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="18" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Smita" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="71" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="17" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="geeta" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="72" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="16" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="tara" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="74" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="17" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Aarya" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="75" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="15" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Tejashri" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="77" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="19" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Ashwini" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="78" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="19" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Akshada" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="80" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="19" />
</TableRow>
<TableRow android:background="#DAE8FC" android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="10" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Karishma" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="14" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="18" />
</TableRow>

</TableLayout>
Output:
Q.2. Write a program to display all the data types in object-oriented
programming using framelayout.

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


<androidx.constraintlayout.widget.ConstraintLayout
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:background="@color/purple_200">

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/purple_200"
android:layout_marginTop="150dp">

<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="90dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="150dp"
android:text="Boolean"
/>
<Button
android:id="@+id/button5"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="150dp"
android:text="Array" />
<Button
android:id="@+id/button6"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="150dp"
android:text="Pointer" />

<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="integer" />

<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="10dp"
android:text="Float" />
<Button
android:id="@+id/button3"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="10dp"
android:text="Char" />
<Button
android:id="@+id/button7"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="270dp"
android:layout_marginTop="280dp"
android:text="Enum" />
<Button
android:id="@+id/button8"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="150dp"
android:layout_marginTop="280dp"
android:text="Union" />

<Button
android:id="@+id/button9"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="280dp"
android:text="class" />

</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Practical No.7
Q.1. Write a program to accept username and password from
the end user using Text View and Edit Text.
<?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/textview1"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:text="Username"
android:textSize="30dp"
android:hint="@string/app_name"
/>
<EditText
android:id="@+id/usernameEdittext1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="20dp"
android:hint="Username"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:text="Password"
android:textSize="30dp"
android:hint="@string/app_name"
/>
<EditText
android:id="@+id/usernameEdittext2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:textSize="20dp"
android:hint="Password"
/>

<Button
android:id="@+id/button"
android:layout_width="100sp"
android:layout_height="49dp"
android:text="submit" />

</LinearLayout>

Output:
Q.2.Write a program to accept and display personal information of the
student.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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/textView9"
android:layout_width="85dp"
android:layout_height="33dp"
android:layout_x="69dp"
android:layout_y="172dp"
android:text="Name"
android:textColor="@color/purple_200"
android:textSize="20dp"/>

<TextView
android:id="@+id/textView10"
android:layout_width="84dp"
android:layout_height="31dp"
android:layout_x="69dp"
android:layout_y="231dp"
android:text="Roll No"
android:textColor="@color/purple_200"
android:textSize="20dp"/>
<TextView
android:id="@+id/textView11"
android:layout_width="82dp"
android:layout_height="32dp"
android:layout_x="69dp"
android:layout_y="282dp"
android:text="Branch"
android:textColor="@color/purple_200"
android:textSize="20dp"/>
<TextView
android:id="@+id/textView12"
android:layout_width="81dp"
android:layout_height="30dp"
android:layout_x="70dp"
android:layout_y="335dp"
android:text="Year"
android:textColor="@color/purple_200"
android:textSize="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="144dp"
android:layout_y="415dp"
android:text="submit" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_x="183dp"
android:layout_y="160dp"
android:textStyle="bold"
android:textSize="18dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_x="181dp"
android:layout_y="217dp"
android:ems="10"
android:hint="Roll No"
android:textStyle="bold"
android:textSize="18dp"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_x="180dp"
android:layout_y="274dp"
android:ems="10"
android:textStyle="bold"
android:textSize="18dp"
android:inputType="textPersonName"
android:hint="Branch" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_x="184dp"
android:layout_y="328dp"
android:ems="10"
android:textStyle="bold"
android:textSize="18dp"
android:inputType="textPersonName"
android:hint="Year" />
</AbsoluteLayout>

Java File
package com.example.p72displayinfostudent;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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 editText1;
EditText editText2;
EditText editText3;
EditText editText4;
Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1=(EditText)findViewById(R.id.editText1);
editText2=(EditText)findViewById(R.id.editText2);
editText3=(EditText)findViewById(R.id.editText3);
editText4=(EditText)findViewById(R.id.editText4);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name1=editText1.getText().toString();
Toast.makeText(MainActivity.this, "Name= "+name1, Toast.LENGTH_SHORT).show();
}
});

}
}

Output:
Pranav
Practical No.8
Q.1. Write a program to create a first display screen of any search
engine using Auto Complete Text View.

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


<androidx.constraintlayout.widget.ConstraintLayout
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">

<AutoCompleteTextView
android:id="@+id/auto1"
android:layout_width="160dp"
android:layout_height="52dp"
android:layout_marginBottom="522dp"
android:hint="Enter Branch"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Java file
package com.example.p81;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {

String[] branch=new String[]{"computer","information technology","electrical


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

AutoCompleteTextView editText=findViewById(R.id.auto1);
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,branch);

editText.setAdapter(adapter);

}
}
Output:
Q.2.Write a program to display all the subjects of sixth semester using
auto complete text view.
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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="wrap_content"
tools:context=".MainActivity">

<AutoCompleteTextView
android:id="@+id/AutoComplete1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_x="97dp"
android:layout_y="137dp"
android:hint="Enter subject name" />
</AbsoluteLayout>

Java file
package com.example.p82;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import com.example.p82.R;

public class MainActivity extends AppCompatActivity {


String[] semester=new String[]{"Wireless","Android","PHP","Managment"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView editText=findViewById(R.id.AutoComplete1);
ArrayAdapter<String> adapter=new
ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,semester);
editText.setAdapter(adapter);
}
}

Output:

Practical No.9
1. Write a program to create a toggle button to display ON/OFF Bluetooth
on the display screen.
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bluetooth is OFF"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.196" />
<ToggleButton
android:id="@+id/toggleButton"
android:layout_width="90dp"
android:layout_height="51dp"
android:layout_gravity="center"
android:layout_marginStart="144dp"
android:layout_marginTop="248dp"
android:text="ToggleButton"
android:textOff="OFF"
android:textOn="ON"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HarcodedText" />

</androidx.constraintlayout.widget.ConstraintLayout>

Java File
package com.example.p91togglebutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {


private ToggleButton toggleButton;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

toggleButton = findViewById(R.id.toggleButton);
textView = findViewById(R.id.txtView);

toggleButton.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton CompoundButton, boolean b) {

boolean isChecked;
if(isChecked = true) {
textView.setText("Bluetooth is "+ toggleButton.getTextOn());
}
else {
textView.setText("Bluetooth is "+ toggleButton.getTextOff());
}
}
2. Write a program to create a simple calculator.
Xml file:
<?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/t1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="First Number : "
android:gravity="center"
style="@android:style/TextAppearance.Large"
/>

<EditText
android:id="@+id/ETNO1"
android:layout_width="200dp"
android:layout_height="50dp"
android:ems="10"
android:hint="Enter Number 1 : "
android:layout_marginLeft="30dp"
android:layout_toRightOf="@id/t1"/>

<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="Second Number : "
android:gravity="center"
android:layout_below="@id/t1"
style="@android:style/TextAppearance.Large"
/>

<EditText
android:id="@+id/ETNO2"
android:layout_width="200dp"
android:layout_height="50dp"
android:ems="10"
android:hint="Enter Number 2 : "
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/t2"
android:layout_below="@id/ETNO1"/>

<Button
android:id="@+id/add"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="+"
android:textSize="26sp"
android:layout_below="@id/t2"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
android:onClick="Additon"
/>
<Button
android:id="@+id/sub"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="-"
android:textSize="26sp"
android:layout_below="@id/t2"
android:layout_toRightOf="@id/add"
android:layout_marginTop="30dp"
android:onClick="Subtraction"
/>
<Button
android:id="@+id/mul"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="*"
android:textSize="26sp"
android:layout_below="@id/t2"
android:layout_toRightOf="@id/sub"
android:layout_marginTop="30dp"
android:onClick="Multiplication"
/>
<Button
android:id="@+id/div"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="/"
android:textSize="26sp"
android:layout_below="@id/t2"
android:layout_toRightOf="@id/mul"
android:layout_marginTop="30dp"
android:onClick="Division"
/>
<TextView
android:id="@+id/Result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
android:textSize="26sp"
android:layout_marginTop="50dp"/>

</RelativeLayout>
Java file:
package com.example.pra_9_2;

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

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

EditText ETNO1,ETNO2;
Button add,sub,mul,div;
TextView Result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ETNO1 = findViewById(R.id.ETNO1);
ETNO2 = findViewById(R.id.ETNO2);
add = findViewById(R.id.add);
sub = findViewById(R.id.sub);
mul = findViewById(R.id.mul);
div = findViewById(R.id.div);
Result = findViewById(R.id.Result);
}
public void Additon(View view){
double n1 = Double.parseDouble(ETNO1.getText().toString());
double n2 = Double.parseDouble(ETNO2.getText().toString());
double ans = n1 + n2;
Result.setText("Additon is : "+ans);
}
public void Subtraction(View view){
double n1 = Double.parseDouble(ETNO1.getText().toString());
double n2 = Double.parseDouble(ETNO2.getText().toString());
double ans = n1 - n2;
Result.setText("Subtraction is : "+ans);
}

public void Division(View view){


double n1 = Double.parseDouble(ETNO1.getText().toString());
double n2 = Double.parseDouble(ETNO2.getText().toString());
double ans = n1 / n2;
);}

Practical N0.10.
1.Write a program to create a login form for social networking site.
Xml File:
<?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:background="#F6F1F5"
android:orientation="vertical"
tools:context=".MainActivity">

<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center"
android:layout_marginTop="80dp"
app:srcCompat="@drawable/logo1" />
<EditText
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="80dp"
android:backgroundTint="#FA4646"
android:hint="Username or EmailAddress"
android:textColor="#201F20"
android:textColorHint="#d3d3d3" />
<EditText
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:backgroundTint="#FA4646"
android:hint="Password"
android:inputType="textPassword"
android:textColor="#201F20"
android:textColorHint="#d3d3d3" />

<Button
android:id="@+id/butlogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="35dp"
android:layout_marginRight="20dp"
android:backgroundTint="#C956F3"
android:padding="10dp"

android:text="Log in"
android:textStyle="bold"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">

<TextView
android:id="@+id/txtSignUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Sign Up for Instagram"
android:textColor="#000000"/>
<TextView
android:id="@+id/txtForgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:text="Forgot Password?"
android:textColor="#171616"/>

</LinearLayout>
</RelativeLayout>
</LinearLayout>

Pranavjadhavpj2021@gmail.com

2. Write a program to create a login form for student registration System.


XML File:
<?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/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:textSize="20dp"
/>
<EditText
android:id="@+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="49dp"
android:hint="Name"
android:ems="10"
android:inputType="textPersonName"
android:layout_below="@+id/Name"
android:layout_marginTop="20dp"
/>
<TextView
android:id="@+id/Age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:layout_marginTop="40dp"
android:layout_centerHorizontal=
"true"
android:layout_below="@+id/Name_ET"
android:textSize="20dp"
/>
<EditText
android:id="@+id/Age_ET"
android:layout_width="match_parent"
android:layout_height="49dp"
android:ems="10"
android:hint="Age_ET"
android:inputType="textPersonName"
android:layout_marginTop="20dp"
android:layout_below="@+id/Age"/>
<TextView
android:id="@+id/Branch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Branch"
android:layout_marginTop="40dp"
android:layout_centerHorizontal=
"true"
android:layout_below="@+id/Age_ET"
android:textSize="20dp"
/>
<EditText
android:id="@+id/Branch_ET"
android:layout_width="match_parent"
android:layout_height="49dp"
android:ems="10"
android:hint="Branch"
android:inputType="textPersonName"
android:layout_marginTop="20dp"
android:layout_below="@+id/Branch" />
<TextView
android:id="@+id/Semester"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Semester"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/Branch_ET"
android:textSize="20dp"
/>
<EditText
android:id="@+id/Semester_ET"
android:layout_width="match_parent"
android:layout_height="49dp"
android:ems="10"
android:hint="Semester"
android:inputType="textPersonName"
android:layout_marginTop="20dp"
android:layout_below="@+id/Semester"
/>
<Button
android:id="@+id/Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:layout_below="@+id/Semester_ET"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Login"
android:textSize="40dp"
android:layout_centerHorizontal="true"
android:textColor="@color/teal_200"
tools:ignore="DuplicateIds" />
</RelativeLayout>

Java File:

package com.example.pra_10_2;
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;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
EditText name_et,age_et,branch_et,sem_et;
String Username,Age,Branch,Semester;
Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name_et=findViewById(R.id.Name_ET);
age_et=findViewById(R.id.Age_ET);
branch_et=findViewById(R.id.Branch_ET);
sem_et=findViewById(R.id.Semester_ET);
login =findViewById(R.id.Login);
login.setOnClickListener(new View.OnClickListener()
{
class Empty_Activity {
}

@Override
public void onClick(View v) {
Username=name_et.getText().toString();
Age=age_et.getText().toString();
Branch=branch_et.getText().toString();
Semester=sem_et.getText().toString();
Toast.makeText(MainActivity.this,"Welcome
"+Username,Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,"Age
"+Age,Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,"Branch
"+Branch,Toast.LENGTH_SHORT).show();
Toast.makeText(MainActivity.this,"Semester
"+Semester,Toast.LENGTH_SHORT).show();
Intent intent=new Intent(MainActivity.this,Empty_Activity.class);
//
intent.putExtra("username",Username);
startActivity(intent);
}
});
}
}
Practical No.11

1.write a program to show five checkboxes and toast selected checkbox.


Xml file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#D8DEE3">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Kindly Select Food Items from below"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.434"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.054"
android:textStyle="bold"
android:textColor="#4A9FEA"
/>

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginTop="68dp"
android:text="Pizza"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pasta"
android:layout_marginStart="144dp"
android:layout_marginTop="124dp"

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginTop="180dp"
android:text="Coffee"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginTop="230dp"
android:text="Tea"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="144dp"
android:layout_marginTop="290dp"
android:text="Noodles"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="116dp"
android:text="Order Selected Food"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/checkBox5"
/>

</androidx.constraintlayout.widget.ConstraintLayout>

Java File:
package com.example.checkbox;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {


CheckBox pizza,pasta,coffee,tea,noodles;
Button order;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();
}

public void addListenerOnButtonClick() {


pizza=(CheckBox)findViewById(R.id.checkBox);
pasta=(CheckBox)findViewById(R.id.checkBox2);
coffee=(CheckBox)findViewById(R.id.checkBox3);
tea=(CheckBox)findViewById(R.id.checkBox4);
noodles=(CheckBox)findViewById(R.id.checkBox5);
order=(Button)findViewById(R.id.button);
order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int total=0;
StringBuilder result=new StringBuilder();
result.append("Selected Items:");
if(pizza.isChecked())
{
result.append("\nPizza 150Rs");
total=total+150;
}
if(pasta.isChecked())
{
result.append("\nPasta: 130Rs");
total=total+130;
}
if(coffee.isChecked())
{
result.append("\nCoffee: 30Rs");
total=total+30;
}
if(tea.isChecked())
{
result.append("\nTea: 10Rs");
total=total+10;
}
if(noodles.isChecked())
{
result.append("\nNoodles: 100Rs");
total=total+100;
}
result.append("\nTotal:"+total+"Rs");

Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_LONG).show();
}
});
}
}
Practical No.12
1. Write a program to show the following output.
Xml file:
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Single radio Buttons" />
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_marginTop="160dp"
android:text="Radio Button Inside RadioGroup" />
<RadioButton
android:id="@+id/rb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Radio Button 1"
android:layout_marginTop="20dp"
/>

<RadioButton
android:id="@+id/rb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Radio Button 2"
android:layout_marginTop="50dp"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/black"
android:layout_marginTop="100dp"/>
<RadioGroup
android:id="@+id/radiogrp1"
android:layout_width="match_parent"
android:layout_height="wrap_content"

android:layout_below="@+id/tv"
android:layout_marginTop="20dp"
android:orientation="vertical">

<RadioButton
android:id="@+id/rb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />

<RadioButton
android:id="@+id/rb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female">

</RadioButton>
</RadioGroup>

<Button
android:id="@+id/button"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/radiogrp1"
android:layout_marginTop="1dp"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="center"
android:text="Show Selected" />

</RelativeLayout>
Java file:
package com.example.radiobutton;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.RadioGroup;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {


RadioGroup rgb;
RadioButton rb;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.button);
rgb=(RadioGroup)findViewById(R.id.radiogrp1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
rb=(RadioButton)findViewById(rgb.getCheckedRadioButtonId());
String a=rb.getText().toString();
Toast.makeText(MainActivity.this,"Selected Radio
Button:"+a,Toast.LENGTH_LONG).show();
}
});
}
}

Practical No.13
1.write a program to display circular progress bar.
Xml file:
<?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:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity">

<RelativeLayout
android:id="@+id/progress_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginStart="100dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="100dp"
android:layout_marginBottom="100dp" />

<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/circular_shape"
android:indeterminate="false"
android:progressDrawable="@drawable/circular_progress"
android:textAlignment="center" />

<TextView
android:id="@+id/progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="---"
android:textColor="@color/design_default_color_primary"
android:textSize="28sp"
android:textStyle="bold" />

</LinearLayout>

Java file:

package com.example.circularprogressbar;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private ProgressBar progressBar;
private TextView text;
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar=findViewById(R.id.progress_bar);
text=findViewById(R.id.progress_text);
final Handler handler=new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if(i<=100)
{
text.setText(""+i);
progressBar.setProgress(i);
i++;
handler.postDelayed(this,200);

}
else {
handler.removeCallbacks(this);
}
}
},200);

}
}
2.write a program to show following output.
Xml file:

<?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:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
tools:context=".MainActivity">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="download file"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"/>

</RelativeLayout>
Java code:

package com.example.progressbaroffile;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.app.ProgressDialog;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
ProgressDialog progressBar;
private int progressBarStatus=0;
private Handler progressBarHandler=new Handler();
private long fileSize=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButtonClick();

}
public void addListenerOnButtonClick(){
btn=findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar=new ProgressDialog(view.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("File downloading...");
progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressBar.setProgress(0);
progressBar.setMax(100);
progressBar.show();
progressBarStatus = 0;
fileSize = 0;

new Thread(new Runnable() {


public void run() {
while (progressBarStatus < 100) {
// performing operation
progressBarStatus = doOperation();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Updating the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}

if (progressBarStatus >= 100) {

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

progressBar.dismiss();
}
}
}).start();
}
});
}
public int doOperation() {

while (fileSize <= 10000) {


fileSize++;
if (fileSize == 1000) {
return 10;
} else if (fileSize == 2000) {
return 20;
} else if (fileSize == 3000) {
return 30;
} else if (fileSize == 4000) {
return 40;
}}
return 100;}}
Practical No.14.
1.write a program to show the following output.
Xml file:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity"
tools:ignore="ResAuto">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:entries="@array/Language_list" />
</RelativeLayout>

String.xml
<resources>
<string name="app_name">P14.1</string>
<array name="Language_list">
<item>Android</item>
<item>Java</item>
<item>PHP</item>
<item>Hadoop</item>
<item>Sap</item>
<item>Phython</item>
<item>Ajax</item>
<item>C++</item>
<item>Puby</item>
<item>Rails</item>
</array>
</resources>
2.write a program to display an image using image view and a button named as
“change image” .once you click on button another image get displayed.

Xml file:
<?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="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image1"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
tools:srcCompat="@drawable/img1" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image"
android:layout_below="@+id/image1"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

Java file:

package com.example.imageview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


Button b1;
ImageView v1;
boolean flag;
int images[]={R.drawable.img2,R.drawable.img3,R.drawable.img4};
int i=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
v1=(ImageView)findViewById(R.id.image1);
b1=(Button)findViewById(R.id.button);
flag=true;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
v1.setImageResource(images[i]);
i++;
if(i==3) i=0;}
});}}
2.write a program to display 15 buttons using grid view.
listView.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>

Xml file:
<?xml version="1.0" encoding="utf-8"?>
<GridView 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="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity"
android:id="@+id/gridview"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/ >

Java file:
package com.example.gridviewbuttons;

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

public class MainActivity extends AppCompatActivity {


GridView gridview;
String arr[] = new String[15];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = findViewById(R.id.gridview);
for (int i = 0; i < 15; i++) {
arr[i] = Integer.toString(i + 1);

}
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.activity_listview,
R.id.btn, arr);
gridview.setAdapter(ad);
}
}
4.write a program to display a text view using vertical scroll view.
Xml file:
<?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">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">
<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Vertical Scroll example\n
_______________________________________________________\n
___________________________________________________________\n
_________________________________________________\n
_______________________________________________\n
___________________________________________________\n
______________________________________________\n
____________________________________________\n

_\n______________________________________________________
\n___________________
\n__________________________________________
\
n__________________________________________________________________________
_______________
\n________________________________________--
_______________________________________
\n_____________________________________\
n__________________________________________________________________________
______________________________________________________
\n_______________________________----\n\n_\n_
\n_____________________________________________

\n+________________________________________
\n____________________________________________
\n________________________________________
\n___________________

\n______________________________________
\n__________________________________________________
\n____________________________________________
\n____________________________________--
\n\n_\n_____________________________________________________\
n__________________________________\n______________________________
\n__________________________________\n____\
n___________________________________---
"/></ScrollView>
</RelativeLayout>
Practical No.15.

1.Write a program to display following toast message.


Xml file:
<?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">

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World, Toast Example"
android:textSize="30sp"
/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="show"
android:text="show Toast"
android:textSize="30sp"/>
</LinearLayout>
Java file: package com.example.toastmessage;

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 show(View v)
{
Toast.makeText(this,"Message for you\n You have got mail",
Toast.LENGTH_LONG).show();
}
}
2.Write a program to display three checkboxes and one button named “Order”
as shown below. Once you click on button it should toast different selected
checkboxes along with items individual and total prize.

XMLFile:

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


<AbsoluteLayout 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">
<CheckBox
android:id="@+id/cPizza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="180dp"
android:layout_y="116dp"
android:text="Pizza" />
<CheckBox
android:id="@+id/cBurger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="180dp"
android:layout_y="255dp"
android:text="Burger" />
<CheckBox
android:id="@+id/cCoffee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="180dp"
android:layout_y="183dp"
android:text="Coffee" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="168dp"
android:layout_y="386dp"
android:text="order" />
</AbsoluteLayout>

Java File:
package com.example.pra_15_2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int total, priceP = 0, priceC = 0, priceB = 0;
boolean p = false,c = false,b = false;
String str = "Selected Items:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CheckBox pizza = (CheckBox)findViewById(R.id.cPizza);
final CheckBox coffee = (CheckBox)findViewById(R.id.cCoffee);
final CheckBox burger = (CheckBox)findViewById(R.id.cBurger);
Button btn = (Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(pizza.isChecked()){
priceP = 100;
str += "\nPizza : "+priceP+" RS";
}
if(coffee.isChecked()){
priceC = 50;
str += "\nCoffee : "+priceC+" RS";
}
if(burger.isChecked()){
priceB = 120;
str += "\nBurger : "+priceB+" RS";
}
total = priceB + priceC + priceP;
str += "\n\nTotal "+total;
Toast.makeText(getApplicationContext(),str,Toast.LENGTH_LONG).show();
priceP = priceC = priceB = 0;
str = "Selected Items :";
}
});
}
Practical No.16.
1.write a program to display following output. Use timepicker with example.

Xml file:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:timePickerMode="spinner"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AM/PM"
android:textSize="36sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
B Xml file:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

<TimePicker
android:id="@+id/tpp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:timePickerMode="spinner"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24 Hours"
android:textSize="36sp"
android:layout_centerHorizontal="true"/>
</RelativeLayout>

Java file:
package com.example.p16timepickerb;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TimePicker;
public class MainActivity extends AppCompatActivity {
TimePicker tp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tp=(TimePicker)findViewById(R.id.tpp);
tp.setIs24HourView(true);
}
}
C Xml file:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

<TimePicker
android:id="@+id/tpp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:timePickerMode="clock"
android:layout_marginTop="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24 Hours"
android:textSize="36sp"
android:layout_centerHorizontal="true” />
</RelativeLayout>
2.Write a program to display following output. Select and display date amd time
on click of “select date”, “select time” buttons respectively.

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

<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:paddingTop="80dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/et1"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:hint="Date Here" />

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@id/et1"
android:layout_alignParentEnd="true"
android:text="SELECT DATE" />

<EditText
android:id="@+id/et2"
android:layout_width="wrap_content"
android:layout_height="49dp"
android:layout_below="@+id/et1"
android:hint="Time Here" />
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btn1"
android:layout_alignEnd="@id/et2"
android:layout_alignParentEnd="true"
android:text="SELECT TIME" />
</RelativeLayout>

Java File:
package com.example.pra_16_2;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;

import android.widget.TimePicker;
import java.util.Calendar;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {

Button btn1, btn2;


EditText et1, et2;
private int mYear, mMonth, mDay, mHour, mMinute;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
et1 = findViewById(R.id.et1);
et2 = findViewById(R.id.et2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);

@Override

public void onClick(View v) {


if (v == btn1) {
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dp = new DatePickerDialog(this, new


DatePickerDialog.OnDateSetListener() {

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth){
et1.setText(dayOfMonth + "-" + (month + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dp.show();

}
if (v == btn2) {
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);

TimePickerDialog tp = new TimePickerDialog(this, new


TimePickerDialog.OnTimeSetListener() {

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
et2.setText(hourOfDay + ":" + minute);
}
}, mHour, mMinute, false);
tp.show();
}

AndriodManifest.xml:

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


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pra_16_2">
<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/Theme.AppCompat">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>
</activity>
</application>
</manifest>
Practical No.17.

1. Write a program to create a HelloWorld Activity using all lifecycles methods


to display messages using Log.d.
XML File:
<?xml version= "1.0" encoding= "utf-8" ?>
<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:paddingTop="80dp"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/>

</RelativeLayout>

Java File:
package com.example.pra_17;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.util.Log;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}

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

<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/Theme.AppCompat">
<activity android:name=".MainActivity" android:exported="true">

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

</activity>
</application>
</manifest>
Practical No.18.
1.write a program to create a text field and a button “Navigate” . When you
enter www.google.com and press navigate button it should open google page.
Xml file:
<?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"
android:padding="16dp">

<EditText
android:id="@+id/url_edit_text"
android:layout_width="match_parent"
android:layout_height="49dp"
android:inputType="textUri"
android:hint="Enter a URL"/>

<Button
android:id="@+id/navigate_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"/>
</LinearLayout>
Java file:
package com.example.p181googlecom;
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 androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private EditText urlEditText;


private Button navigateButton;

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

urlEditText = findViewById(R.id.url_edit_text);
navigateButton = findViewById(R.id.navigate_button);

navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = urlEditText.getText().toString();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});}}

2.write a program to create button “start dialer” . when u click on this button it should open
the phone dialer.
Xml file:
<?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"
android:padding="16dp">

<Button
android:id="@+id/start_dialer_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer"/>
</LinearLayout>

Java file:
package com.example.p182startdialer;

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 {

private Button startDialerButton;

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

startDialerButton = findViewById(R.id.start_dialer_button);
startDialerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
startActivity(intent);
}});}}

1.Write a program to create two screens. First screen will take one number input from user.
After click on Factorial button, second screen will open and it should display factorial of the
same number. Also specify which type of intent you will use in this case.
XML File:
<?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:padding="20dp">
<EditText android:id="@+id/etNumber"
android:layout_width="match_parent"
android:layout_height="49dp"
android:hint="Enter a number"
android:inputType="number"
android:textAlignment="center"
android:layout_marginTop="50dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Factorial"
android:layout_marginTop="10dp"
android:onClick="displayFactorial"/>
</LinearLayout>

Java File:
package com.example.pra_18_3;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.pra_18_3.R;

public class MainActivity extends AppCompatActivity {


EditText etNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNumber = findViewById(R.id.etNumber);
}
public void displayFactorial(View view) {
Intent i = new Intent(MainActivity.this, EmptyActivity.class);
i.putExtra("number", etNumber.getText().toString());
startActivity(i);
Toast.makeText(MainActivity.this,"Explicit Intent is
used..",Toast.LENGTH_SHORT).show();
}
}
Empty_activity.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=".EmptyActivity"
android:padding="20dp"
android:gravity="center">
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Factorial of number is:"
android:textColor="@color/black"
android:textSize="25sp"
android:textAlignment="center"/>
</RelativeLayout>

EmptyActivity.java
package com.example.pra_18_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class EmptyActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.empty_activity);
Bundle b = getIntent().getExtras();
int no = Integer.parseInt(b.getString("number"));
long f=1;
for(int i=no; i>0; i--) {
f = f * i;
}
tv = findViewById(R.id.tv);
tv.setText("Factorial of no is " + f +".");
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Pra_18_3"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</intent-filter>
</activity>
<activity android:name=".EmptyActivity" android:exported="true"></activity>
</application>
</manifest>
Practical No.19
1.Write a program to create your own content provider to insert and access data in android
application.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<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"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Content Provider"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="40dp"
android:textStyle="bold"/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:hint="Name"
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView1"/>
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content"
android:id="@+id/editText3"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:hint="Grade"
android:layout_marginTop="40dp"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:layout_marginTop="40dp"
android:text="Add Name"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true"
android:onClick="onClickAddName"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Retrive student"
android:layout_centerHorizontal="true"
android:id="@+id/button"
android:layout_below="@+id/button2"
android:onClick="onClickRetrieveStudents"/>
</RelativeLayout>

Java File:
package com.example.pra_19;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
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 {
Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.button2);
btn2=findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ContentValues values = new ContentValues();
values.put(MyContentProvider.NAME,
((EditText)findViewById(R.id.editText2)).getText().toString());
values.put(MyContentProvider.GRADE,
((EditText)findViewById(R.id.editText3)).getText().toString());
Uri uri = getContentResolver().insert(
MyContentProvider.CONTENT_URI, values);
Toast.makeText(getBaseContext(),
uri.toString(), Toast.LENGTH_LONG).show();
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@SuppressLint("Range")
@Override
public void onClick(View view) {
String URL = "content://com.example.MyApplication.MyContentProvider";
Uri students = Uri.parse(URL);
Cursor c = managedQuery(students, null, null, null, "name");
if (c.moveToFirst()) {
do{
Toast.makeText(MainActivity.this,
c.getString(c.getColumnIndex(MyContentProvider._ID)) + ", " +
c.getString(c.getColumnIndex( MyContentProvider.NAME)) +
", " + c.getString(c.getColumnIndex( MyContentProvider.GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
});
}
}

MyContentProvider.java
package com.example.pra_19;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import java.util.HashMap;
public class MyContentProvider extends ContentProvider {
static final String PROVIDER_NAME =
"com.example.MyApplication.StudentsProvider";
static final String URL = "content://" + PROVIDER_NAME + "/students";
static final Uri CONTENT_URI = Uri.parse(URL);
static final String _ID = "_id";
static final String NAME = "name";
static final String GRADE = "grade";
private static HashMap<String, String> STUDENTS_PROJECTION_MAP;
static final int STUDENTS = 1;
static final int STUDENT_ID = 2;
static final UriMatcher uriMatcher;
static{
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "students", STUDENTS);
uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID);
}
private SQLiteDatabase db;
static final String DATABASE_NAME = "College";
static final String STUDENTS_TABLE_NAME = "students";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE =
" CREATE TABLE " + STUDENTS_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT NOT NULL, " +
" grade TEXT NOT NULL);";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + STUDENTS_TABLE_NAME);
onCreate(db);
}
}
public MyContentProvider() {
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// Implement this to handle requests to delete one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public String getType(Uri uri) {
// TODO: Implement this to handle requests for the MIME type of the data
// at the given URI.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = db.insert( STUDENTS_TABLE_NAME, "", values);
if (rowID > 0) {
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
throw new SQLException("Failed to add a record into " + uri);
}
@Override
public boolean onCreate() { Context context = getContext();
DatabaseHelper dbHelper = new DatabaseHelper(context);
db = dbHelper.getWritableDatabase();
return (db == null)? false:true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(STUDENTS_TABLE_NAME);
switch (uriMatcher.match(uri)) {
case STUDENTS:
qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
break;
case STUDENT_ID:
qb.appendWhere( _ID + "=" + uri.getPathSegments().get(1));
break;
default:
}
if (sortOrder == null || sortOrder == "")
{
sortOrder = NAME;
}
Cursor c = qb.query(db, projection, selection,
selectionArgs,null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// TODO: Implement this to handle requests to update one or more rows.
throw new UnsupportedOperationException("Not yet implemented");
}
}

Practical No.20
1.Write a program to start a Wi-Fi using service.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WIFI SERVICE"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enable Wifi"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Disable Wifi"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
/>
</RelativeLayout>
Java File:
package com.example.pra_20;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Button enableButton,disableButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enableButton=(Button)findViewById(R.id.button1);
disableButton=(Button)findViewById(R.id.button2);
enableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WifiManager wifi = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(true);
Toast.makeText(MainActivity.this, "WIFI ON",
Toast.LENGTH_SHORT).show();
}
});
disableButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WifiManager wifi = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
wifi.setWifiEnabled(false);
Toast.makeText(MainActivity.this, "WIFI OFF",
Toast.LENGTH_SHORT).show();
}
});
}
}

2.Write a program to display the following output.


XML File:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<LinearLayout
android:layout_width="283dp"
android:layout_height="617dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="67dp"
android:layout_marginRight="67dp"
android:layout_marginBottom="69dp"
android:orientation="vertical">
<Button
android:id="@+id/button2"
android:layout_width="222dp"
android:layout_height="62dp"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:layout_marginEnd="36dp"
android:layout_marginRight="36dp"
android:onClick="startService"
android:text="Start Services" />
<Button
android:id="@+id/button"
android:layout_width="219dp"
android:layout_height="62dp"
android:layout_marginStart="38dp"
android:layout_marginLeft="38dp"
android:layout_marginTop="186dp"
android:layout_marginEnd="45dp"
android:layout_marginRight="45dp"
android:layout_marginBottom="387dp"
android:onClick="stopService"
android:text="Stop Services" />
</LinearLayout>
</RelativeLayout>

Java File:
package com.example.prac_20;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
String msg = "Android : ";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(msg, "The onCreate() event");
}
public void startService(View view) {
startService(new Intent(getBaseContext(), MyService.class));
}
// Method to stop the service
public void stopService(View view) {
stopService(new Intent(getBaseContext(), MyService.class));
}
}

MyService.java:
package com.example.prac_20;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class MyService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Let it continue running until it is stopped.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}

AndriodManifest.xml:

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


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

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Prac_20"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyService" />
</application>

</manifest>
Practical No.21
1. Write a program to demonstrate all the system broadcast messages.

XML File:
<?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">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="97dp"
android:layout_marginTop="341dp"
android:text="Broadcasting button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
Java File:
package com.example.pra_21_1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity
{Button button;
MyReceiver myReceiver = new MyReceiver();
IntentFilter filter = new IntentFilter();
@Override
protected void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{ MainActivity.this.registerReceiver(myReceiver,filter);
}
});
}
@Override
protected void onDestroy()
{ super.onDestroy();
MainActivity.this.
unregisterReceiver(myReceiver);
}
}

AndriodManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.pra_21_1">
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
<uses-permission
android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>

<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/Theme.AppCompat">
<activity android:name=".MainActivity" android:exported="true"
tools:ignore="MissingClass">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyReceiver" android:exported="true">
</receiver>
</application>
</manifest>

MyReceiver.java:
package com.example.pra_21_1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver
{public MyReceiver(){
super();
}
@Override
public void onReceive(Context context, Intent intent)
{String intentAction = intent.getAction();
if(intentAction != null){
String toastMessage = "unknown intent action";

switch(intentAction){
case Intent.ACTION_AIRPLANE_MODE_CHANGED:
boolean status = intent.getBooleanExtra("state",true);
if(status){
toastMessage = "Airplane Mode turned on";
}else {
toastMessage = "Airplane Mode turned off";
}
break;
}
Toast.makeText(context,toastMessage,Toast.LENGTH_SHORT).show();
}
}
}
Practical No.22
1.Write a program to changes the background color when device is shuffled.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
/>

Java File:
package com.example.pra_22_1;
import android.graphics.Color;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorListener;
import android.hardware.SensorManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import java.util.Random;

public class MainActivity extends AppCompatActivity implements SensorEventListener {


ConstraintLayout lay;
SensorManager sm;
Sensor mysenser;
long lastUpdate;
float x,y,z,last_x,last_y,last_z;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lay = (ConstraintLayout)findViewById(R.id.layout);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
mysenser = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, mysenser, SensorManager.SENSOR_DELAY_GAME);
}
public void onSensorChanged(SensorEvent event) {
long curTime = System.currentTimeMillis();
if((curTime - lastUpdate) > 100){
long diffTime = (curTime - lastUpdate);
lastUpdate = curTime;
x = event.values[0];
y = event.values[1];
z = event.values[2];
float speed = Math.abs(x+y+z - last_x - last_y - last_z) / diffTime * 10000;
if(speed > 1200){
Random rnd = new Random();
int color = Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
lay.setBackgroundColor(color);
}
last_x = x;
last_y = y;
last_z = z;
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
}
2. Write a program to display the list of sensors supported by the mobile device.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity" >

<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="92dp"
android:layout_marginTop="114dp"
android:text="TextView" />
</RelativeLayout>

Java File:
package com.example.pra_22_2;

import androidx.appcompat.app.AppCompatActivity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

public class MainActivity extends AppCompatActivity {


SensorManager sm = null;
TextView textView1 = null;
List list;

SensorEventListener sel = new SensorEventListener(){


public void onAccuracyChanged(Sensor sensor, int accuracy) {}
public void onSensorChanged(SensorEvent event) {
float[] values = event.values;
textView1.setText("x: "+values[0]+"\ny: "+values[1]+"\nz: "+values[2]);
}
};

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

/* Get a SensorManager instance */


sm = (SensorManager)getSystemService(SENSOR_SERVICE);

textView1 = (TextView)findViewById(R.id.textView1);

list = sm.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(list.size()>0){
sm.registerListener(sel, (Sensor) list.get(0),
SensorManager.SENSOR_DELAY_NORMAL);
}else{
Toast.makeText(getBaseContext(), "Error: No Accelerometer.",
Toast.LENGTH_LONG).show();
}
}

@Override
protected void onStop() {
if(list.size()>0){
sm.unregisterListener(sel);
}
super.onStop();
}
}

strings.xml
<resources>
<string name="app_name">Sensor</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
</resources>
Practical No.23
1. Write a program to capture an image and display it using image view.
XML File:
<?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="fill_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:id="@+id/imageView" />
<Button
android:text="Click here to capture image using camera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_gravity="center_horizontal"/>
</LinearLayout>

Java File:
package com.example.pra_23_1;
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.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.pra_23_1.R;

public class MainActivity extends AppCompatActivity {


Button button ;
ImageView imageView ;
Intent intent ;
public static final int RequestPermissionCode = 1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
imageView = (ImageView)findViewById(R.id.imageView);
EnableRuntimePermission();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
intent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == 7 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
}
public void EnableRuntimePermission(){
if
(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CAMERA))
{
Toast.makeText(MainActivity.this,"CAMERA permission allows us to Access
CAMERA app", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this,new
String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int RC, String per[], int[]
PResult) {
switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this,"Permission Granted, Now your application
can access CAMERA.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"Permission Canceled, Now your application
cannot access CAMERA.", Toast.LENGTH_LONG).show();
}
break;
}
}
}

AndriodManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Pra_23_1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

</manifest>
2. Write a program to record a video using various camera methods.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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/button"
android:layout_width="196dp"
android:layout_height="59dp"
android:layout_marginTop="44dp"
android:onClick="startCamera"
android:text="Record Video"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<VideoView
android:id="@+id/videoView"
android:layout_width="299dp"
android:layout_height="463dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.187" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java File:
package com.example.prac23_2videoview;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;
public class MainActivity extends Activity {
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 10;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startCamera(View view)
{
//create Intent to record video and return it to the calling application
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//start the video capture Intent
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//display the video in VideoView
VideoView videoView=(VideoView)findViewById(R.id.videoView);
//get video uri
Uri videoUri=data.getData();
//set the video uri to VideoView
videoView.setVideoURI(videoUri);
//set media controller allowing you to play, pause, and move the video forward and
backward
videoView.setMediaController(new MediaController(this));
//play the video
videoView.start();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// video capture failed
}
}
}
}
Practical No.24
1.Write a program to turn on, get visible, list devices and turnoff Bluetooth with the help of
following GUI.
XML File:
<?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"
android:transitionGroup="true">

<TextView android:text="Bluetooth Example"


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/white"
android:id="@+id/textview"
android:textSize="35dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Turn On"
android:id="@+id/button"
android:clickable="true"
android:onClick="on" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get visible"
android:onClick="visible"
android:id="@+id/button2"
android:layout_alignBottom="@+id/button"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List devices"
android:onClick="list"
android:id="@+id/button3"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="turn off"
android:onClick="off"
android:id="@+id/button4"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_below="@+id/textView2" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Paired devices:"
android:id="@+id/textView2"
android:textColor="#ff34ff06"
android:textSize="25dp"
android:layout_below="@+id/button4"
android:layout_alignLeft="@+id/listView"
android:layout_alignStart="@+id/listView" />

</LinearLayout>

Java File:
package com.example.pra_24_1;
import androidx.appcompat.app.AppCompatActivity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Set;
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4;
private BluetoothAdapter BA;
private Set<BluetoothDevice> pairedDevices;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button) findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
b4=(Button)findViewById(R.id.button4);
BA = BluetoothAdapter.getDefaultAdapter();
lv = (ListView)findViewById(R.id.listView);
}
public void on(View v){
if (!BA.isEnabled()) {
Intent turnOn = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(turnOn, 0);
Toast.makeText(getApplicationContext(), "Turned on",Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Already on", Toast.LENGTH_LONG).show();
}
}
public void off(View v){
BA.disable();
Toast.makeText(getApplicationContext(), "Turned off" ,Toast.LENGTH_LONG).show();
}
public void visible(View v){
Intent getVisible = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivityForResult(getVisible, 0);
}
public void list(View v){
pairedDevices = BA.getBondedDevices();
ArrayList list = new ArrayList();
for(BluetoothDevice bt : pairedDevices) list.add(bt.getName());
Toast.makeText(getApplicationContext(), "Showing Paired
Devices",Toast.LENGTH_SHORT).show();
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pra_24_1" >
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat" >

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:exported="true">

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

</activity>
</application>
</manifest>
Practical No.25
1.Write a program to rotate the image in clockwise/anticlockwise, Zoom IN/Zoom out,Fade
IN/Fade OUT by using the following GUI.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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">
<ImageView
android:id="@+id/imageView"
android:layout_width="286dp"
android:layout_height="240dp"
android:layout_x="63dp"
android:layout_y="79dp"
android:src="@drawable/marks" />
<Button
android:id="@+id/zoomin"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_x="42dp"
android:layout_y="454dp"
android:text="Zoom In" />
<Button
android:id="@+id/zoomout"
android:layout_width="158dp"
android:layout_height="wrap_content"
android:layout_x="218dp"
android:layout_y="454dp"
android:text="Zoom Out" />
<Button
android:id="@+id/rotateC"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_x="43dp"
android:layout_y="516dp"
android:text="Clockvise" />
<Button
android:id="@+id/fadein"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_x="45dp"
android:layout_y="581dp"
android:text="Fade In" />
<Button
android:id="@+id/fadeout"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_x="221dp"
android:layout_y="581dp"
android:text="Fade OUt" />
<Button
android:id="@+id/rotateA"
android:layout_width="153dp"
android:layout_height="wrap_content"
android:layout_x="222dp"
android:layout_y="516dp"
android:text="Anti clockvise" />
</AbsoluteLayout>

Java File:
package com.example.pra_25_1;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button zoomIn, zoomOut, fadeIn, fadeOut, rotateClock, rotateAnti;
ImageView imageView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zoomIn = (Button)findViewById(R.id.zoomin);
zoomOut = (Button)findViewById(R.id.zoomout);
fadeIn = (Button)findViewById(R.id.fadein);
fadeOut = (Button)findViewById(R.id.fadeout);
rotateAnti = (Button)findViewById(R.id.rotateA);
rotateClock = (Button)findViewById(R.id.rotateC);
imageView = (ImageView)findViewById(R.id.imageView);
zoomIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setScaleX(imageView.getScaleX() + (float)0.1);
imageView.setScaleY(imageView.getScaleY() + (float)0.1);
}
});
zoomOut.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setScaleX(imageView.getScaleX() - (float)0.1);
imageView.setScaleY(imageView.getScaleY() - (float)0.1);
}
});
rotateClock.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setRotation((float)imageView.getRotation() + 20);
}
});
rotateAnti.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageView.setRotation((float)imageView.getRotation() - 20);
}
});
fadeIn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Animation aniFadein =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);
imageView.startAnimation(aniFadein);
}
});
fadeOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation aniFadeout =
AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out);
imageView.startAnimation(aniFadeout);
}
});
}
}

fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:toAlpha="1.0" />

</set>

fade_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />

</set>
Practical No.26
1.Write a program to insert data in SQLite database using AsyncTask
XML File:
<?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/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INSERT DATA"
android:textStyle="bold"
android:textSize="30dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/et"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/tv"
android:textSize="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et"
android:text="Insert Data"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="addData"
/>
</RelativeLayout>
Java File:

package com.example.pra_26;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class MainActivity<BackgroundTask> extends AppCompatActivity {
EditText et;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et);
}
public void addData(View view)
{
name=et.getText().toString();
backgroundtask backgroundTask=new backgroundtask(this);
backgroundTask.execute("add_info",name);
}
}

Database.java
package com.example.pra_26;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public final class Database extends SQLiteOpenHelper {
private static final int db_version = 1;
private static final String DB_NAME = "name_data.db";
private static final String create_query = "create table"+ data.dataname.table_name+"("+
data.dataname.NAME+"text);";
Database(Context ctx)
{
super(ctx,DB_NAME,null,db_version);
Log.d("database operations","Database created....");
}
public void onCreate(SQLiteDatabase db){
db.execSQL(create_query);
Log.d("database operations","table created....");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
public void addinformation(SQLiteDatabase db,String name)
{
ContentValues contentValues=new ContentValues();
contentValues.put(data.dataname.NAME,name);
db.insert(data.dataname.table_name,null,contentValues);
Log.d("database operations","One row inserted....");
}
}

backgroundtask.java
package com.example.pra_26;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.widget.Toast;
public class backgroundtask extends AsyncTask <String,Void,String> {
Context ctx;
backgroundtask(Context ctx)
{
this.ctx=ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params)
{
String method = params[0];
Database dbOperations=new Database(ctx);
if(method.equals("add_info")){
String Name= params[1];
SQLiteDatabase db;
db = dbOperations.getWritableDatabase();
dbOperations.addinformation(db,Name);
return "One Data Inserted";
}
return null;
}
@Override
protected void onProgressUpdate(Void... values)
{
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
}
}
data.java
package com.example.pra_26;
public final class data {
data(){}
public static abstract class dataname
{
public static final String NAME ="name";
public static final String table_name="student_data";
}
}

Practical No.27
1. Write a program to create the login form and display login successful/ Unsuccessful toast
message.
Xml File:
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authentication Required"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="@color/black"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Username:"
android:textColor="@color/black"
android:textSize="20sp" />

<EditText
android:id="@+id/usernameET"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="20sp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:text="Password:" />

<EditText
android:id="@+id/passwordET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@color/black"
android:hint="password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginBtn"
android:layout_width="118dp"
android:layout_height="66dp"
android:layout_below="@+id/passwordET"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:onClick="authenticateLogin"
android:text="Login"
android:textSize="15sp"
tools:ignore="OnClick" />
</LinearLayout>
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authentication Required"
android:textSize="25sp"
android:textStyle="bold"
android:layout_gravity="center"
android:textColor="@color/black"/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="Username:"
android:textColor="@color/black"
android:textSize="20sp" />

<EditText
android:id="@+id/usernameET"
android:layout_width="119dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="@color/black"
android:textSize="20sp" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@color/black"
android:layout_marginTop="20dp"
android:text="Password:" />

<EditText
android:id="@+id/passwordET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="20sp"
android:textColor="@color/black"
android:hint="password"
android:inputType="textPassword" />

<Button
android:id="@+id/loginBtn"
android:layout_width="118dp"
android:layout_height="66dp"
android:layout_below="@+id/passwordET"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:layout_gravity="center"
android:onClick="authenticateLogin"
android:text="Login"
android:textSize="15sp"
tools:ignore="OnClick" />
</LinearLayout>

Java File:
package com.example.pra_27_1;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


private EditText username;
private EditText password;
private Button login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupVariables();
}
public void authenticateLogin(View view) {
if (username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin123")) {
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Login Unsuccessful!",
Toast.LENGTH_SHORT).show();
}
}
private void setupVariables() {
username = (EditText) findViewById(R.id.usernameET);
password = (EditText) findViewById(R.id.passwordET);
login = (Button) findViewById(R.id.loginBtn);
}
}

AndriodManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Pra_27_1"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


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

</manifest>
Practical No.28
1. Write a program to create the login form with necessary validations like length of
username and password, empty text fields, count of unsuccessful login attempts. Display the
login successful/unsuccessful toast message.
XML File:
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Authentication Required"/>

<TextView
android:id="@+id/usernameET"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:text="username:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="49dp"
android:hint="username" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:hint="Password:" />
<EditText
android:layout_width="wrap_content"
android:layout_height="49dp"
android:hint="Password:" />
<TextView
android:id="@+id/attemptsLeftTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:" />

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

<TextView
android:id="@+id/numberOfRemainingLoginAttemptsTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/loginLockedTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>

Java file: package com.example.prac_281;


import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


private TextView username;
private EditText password;
private Button login;
private TextView loginLockedTV;
private TextView attemptsLeftTV;
private TextView numberOfRemainingLoginAttemptsTV;
int numberOfRemainingLoginAttempts = 3;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (TextView) findViewById(R.id.usernameET);
password = this.<EditText>findViewById(R.id.passwordET);
login = (Button) findViewById(R.id.loginBtn);
loginLockedTV = (TextView) findViewById(R.id.loginLockedTV);
attemptsLeftTV = (TextView) findViewById(R.id.attemptsLeftTV);
numberOfRemainingLoginAttemptsTV = (TextView)
findViewById(R.id.numberOfRemainingLoginAttemptsTV);

numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAtte
mpts));
}
public void authenticateLogin(View view) {
if (username.getText().toString().equals("admin") &&
password.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(), "Login Successful!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Login Unsuccessful!",
Toast.LENGTH_SHORT).show();
numberOfRemainingLoginAttempts--;
attemptsLeftTV.setVisibility(View.VISIBLE);
numberOfRemainingLoginAttemptsTV.setVisibility(View.VISIBLE);

numberOfRemainingLoginAttemptsTV.setText(Integer.toString(numberOfRemainingLoginAtte
mpts));
if (numberOfRemainingLoginAttempts == 0) {
login.setEnabled(true);
loginLockedTV.setVisibility(View.VISIBLE);
loginLockedTV.setBackgroundColor(Color.RED);
loginLockedTV.setText("LOGIN LOCKED!!!");
}
}
}
}
Practical No.29
1.Write a program to send and receive SMS, make use of following GUI.
XML File:
<?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:layout_margin="12dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed-medium" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_number"
android:textStyle="normal"
android:textColorHint="@color/black"
android:inputType="number"
android:textSize="16dp"
android:fontFamily="sans-serif-condensed-medium"
android:textColor="@color/black"
android:layout_marginTop="5dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SMS Message"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="@color/black"
android:layout_marginTop="15dp"
android:fontFamily="sans-serif-condensed-medium" />

<EditText
android:layout_width="match_parent"
android:layout_height="70dp"
android:id="@+id/et_msg"
android:textStyle="normal"
android:textColorHint="@color/black"
android:inputType="text"
android:textSize="16dp"
android:fontFamily="sans-serif-condensed-medium"
android:textColor="@color/black"
android:layout_marginTop="5dp" />

<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="@+id/btn_send"
android:text="Send SMS"
android:textSize="16dp"
android:textStyle="bold"
android:textColor="@color/white"
android:fontFamily="sans-serif-condensed-medium"
android:layout_gravity="center"
android:padding="4dp"
android:layout_margin="2dp"
android:layout_marginTop="15dp"/>
</LinearLayout>

Java File:
package com.example.pra_29_1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText et_mobileno,et_msg;
Button btn_send;
String number,msg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("Android SMS");
et_mobileno = findViewById(R.id.et_number);
et_msg = findViewById(R.id.et_msg);
btn_send = findViewById(R.id.btn_send);
//checking permission for sending message
ActivityCompat.requestPermissions(this,new
String[]{Manifest.permission.SEND_SMS},1);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
number = et_mobileno.getText().toString();
msg = et_msg.getText().toString();
//getting intent and pending intent
Intent i = new Intent(MainActivity.this,ReceiveSMS.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(),0,i,0);
// get SMSmessagner to send the message
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number,null,msg,pi,null);
Toast.makeText(MainActivity.this,"Your Message sent
Successfully",Toast.LENGTH_SHORT).show();
}
});
}
}
ReceiveSMS.java:
package com.example.pra_29_1;

public class ReceiveSMS {


}

AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.coexp29">
<uses-permission android:name="android.permission.SEND_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/Theme.AppCompat">
<activity android:name="com.example.pra_29_1.MainActivity"
android:exported="true">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Practical No.30
1.Write a program to send email.
Xml file: <?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:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="49dp"
android:text="To:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/edit_text_to"
android:layout_width="match_parent"
android:layout_height="49dp"
android:hint="email address"
android:inputType="textEmailAddress" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Subject:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/edit_text_subject"
android:layout_width="match_parent"
android:layout_height="49dp"
android:hint="subject"
android:inputType="textEmailSubject" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message:"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<EditText
android:id="@+id/edit_text_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start|top"
android:hint="message"
android:lines="10" />
<Button
android:id="@+id/button_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send" />
</LinearLayout>

Java file:
package com.example.prac_30;

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[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
}
Practical No.31
1.Write a program to locate user’s current location.
activity_maps.xml
<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="com.example.mya.MapsActivity" />

MapsActivity.java:
package com.example.mya;
import android.os.Bundle;

import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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 FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

LatLng manchar = new LatLng(19.0047, 73.9469);


mMap.addMarker(new MarkerOptions().position(manchar).title("I am Manchar"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(manchar));
}
}

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

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


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

<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/Theme.AppCompat">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<!--suppress AndroidDomInspection -->
<activity
android:name="com.example.mya.MapsActivity" android:exported="true"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Google_maps_api.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="google_maps_key" templateMergeStrategy="preserve"
translatable="false">AIzaSyBxyOKaBAs3s_l4grHJILcnDYbhXLXJJOw</string>

</resources>
Practical No.32
1. Write a program to locate user’s current location.
XML File:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:padding="16dp"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_source"
android:layout_width="317dp"
android:layout_height="56dp"
android:layout_marginTop="220dp"
android:background="@android:drawable/editbox_background"
android:hint="Enter Source Location"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_destination"
android:layout_width="317dp"
android:layout_height="52dp"
android:layout_marginTop="36dp"
android:background="@android:drawable/editbox_background"
android:hint="Enter Destination Location"
android:padding="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_source" />
<Button
android:id="@+id/bt_track"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="56dp"
android:text="Display Location"
app:backgroundTint="#3F51B5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_destination" />
</androidx.constraintlayout.widget.ConstraintLayout>

Java File:
package com.example.pra_32;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Display;
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.equals("") && sDestination.equals("")){
Toast.makeText(MainActivity.this, "Enter both location",
Toast.LENGTH_SHORT).show();
}else{
DisplayTrack(sSource,sDestination); }}}); }
private void DisplayTrack(String sSource, String sDestination) {
try{
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");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}catch (ActivityNotFoundException e){
Uri uri =
Uri.parse("https://play.google.com/store/apps/details?
id=com.google.android.apps.maps");
Intent intent= new Intent(Intent.ACTION_VIEW,uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}}}

You might also like