You are on page 1of 87

Enroll-no: 202103103510185

Practical-1
AIM: Installation of android SDK, android studio and creating android AVD.
CODE:
System requirements to install Android Studio:

• 64-bit Microsoft® Windows® 8/10.


• x86_64 CPU architecture; 2nd generation Intel Core or newer, or AMD CPU with
support for a Windows Hypervisor.
• 8 GB RAM or more.
• 8 GB of available disk space minimum (IDE + Android SDK + Android Emulator)
• 1280 x 800 minimum screen resolution.

Installation of Android Studio:

Step 1 : To download the Android Studio, visit the official Android Studio website in your web
browser. : https://developer.android.com/studio

Step 2 : Click on the "Download Android Studio" option

Step 3 : Double click on the downloaded "Android Studio-ide.exe" file.

Step 4 : "Android Studio Setup" will appear on the screen and click "Next" to proceed.
Enroll-no: 202103103510185

Step 5 : Select the components that you want to install and click on the "Next" button.
Enroll-no: 202103103510185

Step 6 : Now, browse the location where you want to install the Android Studio and click "Next" to
proceed.

Step 7 : Choose a start menu folder for the "Android Studio" shortcut and click the "Install" button to
proceed.
Enroll-no: 202103103510185

Step 8 : After the successful completion of the installation, click on the "Next" button.

Step 9 : Click on the "Finish" button to proceed.


Enroll-no: 202103103510185

Now, your Android studio welcome screen will


appear on the screen. Search and open Android

Studio

Installing of Android SDK:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT ( ANDROID )


Enroll-no: 202103103510185

Creation of Android AVD :

Click on “Create Virtual Device…”

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT ( ANDROID )


Enroll-no: 202103103510185

Select Model and click on “Next” Button

Click on finish

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT ( ANDROID )


Enroll-no: 202103103510185

Practical-2
Aim: Create an android application that will demonstrate the use
ofuser interface elements and layouts.
Code: (activity_main.xml)
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_height="match_parent" android:layout_width="match_parent"
android:background="@color/purple_200"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/cons" android:backgroundTint="@color/black"
android:text="Constraint Layout" android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/grid"
android:backgroundTint="@color/black" android:text="Grid
Layout" android:layout_width="match_parent"
android:layout_below="@+id/cons"
android:layout_height="wrap_content" />
<Button
android:id="@+id/frame" android:text="Frame Layout"
android:backgroundTint="@color/black"
android:layout_below="@+id/grid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/linear" android:text="Linear Layout"
android:backgroundTint="@color/black"
android:layout_below="@+id/frame"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/table"
android:backgroundTint="@color/black"
android:text="Table Layout"
android:layout_below="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<RadioButton
android:id="@+id/radioButton"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_below="@id/table"
android:layout_marginTop="-22dp"
android:text="RadioButton" />

<Switch
android:id="@+id/switch1"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Switch"
android:layout_below="@id/radioButton"/>

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:background="@drawable/vvc"
android:layout_below="@id/switch1"/>

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="This
is TextView \n Copyright-Bs"
android:layout_centerHorizontal="true"
android:layout_below="@id/imageView"/>
</RelativeLayout>

[MainActivity.java]
package com.example.explore;
import androidx.appcompat.app.AppCompatActivity;import
android.content.Intent;
import android.view.View; import
android.widget.Button;import
android.os.Bundle;
public class MainActivity extends AppCompatActivity {Button constraint,
grid, frame, linear, table; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
constraint = (Button)findViewById(R.id.cons);grid =
(Button)findViewById(R.id.grid); frame =
(Button)findViewById(R.id.frame); linear =
(Button)findViewById(R.id.linear); table =
(Button)findViewById(R.id.table);
constraint.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Constraint.class));
}
});
frame.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Frame.class));
}
});
grid.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Grid.class));
}
});
linear.setOnClickListener(new View.OnClickListener() {@Override

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

public void onClick(View view) {


startActivity(new Intent(MainActivity.this,Linear.class));
}
});
table.setOnClickListener(new View.OnClickListener() {@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,Table.class));
}
});
}
}

[Constraint.java]
package sh.shyam.second; import
android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity; public
class Constraint extends AppCompatActivity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_constraint);
}
}
[Frame.java]
package sh.shyam.second; import
android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;public
class Frame extends AppCompatActivity { @Override protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame);
}
}

[Grid.java]
package sh.shyam.second; import
android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;public class Grid
extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.activity_grid);
}
}

[Linear.java]
package sh.shyam.second; import
android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;public
class Linear extends AppCompatActivity { @Override protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_linear);
}
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

[Table.java]
package sh.shyam.second; import
android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;public class
Table extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
}
}

[activity_constraint.xml]
<?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="sh.shyam.second.Constraint">
<TextView android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/textView2" />
<TextView android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="337dp"
android:text="Good Morning"

app:layout_constraintBottom_toTopOf="@+id/textView1" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

[activity_frame.xml]
<FrameLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="Hello"
android:gravity="center"
android:textSize="50dp"
android:textColor="@color/black" android:layout_width="match_parent"
android:layout_height="match_parent" />

</FrameLayout>

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

[activity_grid.xml]
<GridLayout android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:rowCount="2" android:columnCount="2"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:text="Hello"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="0"
android:layout_margin="10dp" />
<TextView android:text="Helllo1"
android:textSize="20dp"
android:layout_row="0"
android:layout_column="1"
android:layout_margin="10dp" />
<TextView android:text="hello 2"
android:textSize="20dp"
android:layout_row="1"

android:layout_column="0"
android:layout_margin="10dp" />
<TextView android:text="hello
noob"android:textSize="20dp"
android:layout_row="1"
android:layout_column="1"
android:layout_margin="10dp" />
</GridLayout>

[activity_linear.xml]
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="Hello Good morning" android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView android:src="@mipmap/ic_launcher_round"
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

[activity_table.xml]
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
xmlns:android="http://schemas.android.com/apk/res/android">

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

<TableRow>
<TextView android:text="Hello"
android:padding="5dip"
android:textSize="50dp"
/>
<TextView android:text="hello1"
android:padding="5dip"
android:textSize="50dp"/>
</TableRow>
<TableRow>

<TextView android:text="hello"
android:padding="5dip"
android:textSize="50dp"
/>
<TextView android:text="hello
noob"android:padding="5dip"
android:textSize="50dp"/>
</TableRow>
</TableLayout>

Output:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical-3
AIM: Create an android application that should be able to find entered quary
(searchview) from RecyclerView.
CODE:
activity_main.xml

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


<androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerView"
xmlns:android="http://schemas.android.com/apk/res/android">

</androidx.recyclerview.widget.RecyclerView>

list.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"

app:srcCompat="@android:drawable/btn_star_big_on" />

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:padding="16dp"
android:text="TextView"
android:textSize="24dp" />
</LinearLayout>

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

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

<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<item
android:id="@+id/actionSearch"
android:icon="@drawable/search_icon"
android:title="Search"
app:actionViewClass="android.widget.SearchView"
app:showAsAction="ifRoom|collapseActionView"/>

</menu>

MainActivity.java :

package com.example.recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView subject;
private ArrayList<modal>ModalArrayList;
private adapter adapter;
CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)
Enroll-no: 202103103510185

@Override

protected void onCreate(Bundle savedInstanceState)


{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subject=(RecyclerView)
findViewById(R.id.recyclerView);
subject.setLayoutManager(new LinearLayoutManager(this));
ModalArrayList = new ArrayList<>(); ModalArrayList.add(new
modal("java")); ModalArrayList.add(new modal("java script"));
ModalArrayList.add(new modal("java advance"));
ModalArrayList.add(new modal("php"));
ModalArrayList.add(new modal("html"));
ModalArrayList.add(new modal("C")); ModalArrayList.add(new
modal("C++")); ModalArrayList.add(new modal("sql"));
ModalArrayList.add(new modal("python"));
ModalArrayList.add(new modal("swift"));
ModalArrayList.add(new modal("react"));
ModalArrayList.add(new modal("ios"));

adapter = new adapter(ModalArrayList, MainActivity.this);


LinearLayoutManager manager = new LinearLayoutManager(this);
subject.setHasFixedSize(true);
subject.setLayoutManager(manager);
subject.setAdapter(adapter);

public booleanonCreateOptionsMenu(Menu menu) { MenuInflater


inflater = getMenuInflater(); inflater.inflate(R.menu.search_menu,
menu); MenuItemsearchItem = menu.findItem(R.id.actionSearch);
SearchViewsearchView = (SearchView) searchItem.getActionView();

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{ @Override
public booleanonQueryTextSubmit(String query) {
return false;
}

@Override

public booleanonQueryTextChange(String newText) {


filter(newText);
return false;
}
});

return true;
}

public void filter(String text)


{ ArrayList<modal>filteredlist = new
ArrayList<>();
for (modal item :ModalArrayList) {
if (item.getName().toLowerCase().contains(text.toLowerCase())) {
filteredlist.add(item);
}
}

if (filteredlist.isEmpty()) {
Toast.makeText(this, "No Data Found..", Toast.LENGTH_SHORT).show();
} else
{ adapter.filterList(filteredlist)
;
}

}
CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)
Enroll-no: 202103103510185

Adapter.java:
package com.example.recyclerview;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class adapter extends RecyclerView.Adapter<adapter.subjectHolder> {
private ArrayList data;
public adapter(ArrayList data1, MainActivitymainActivity)
{ this.data= data1;
}

public adapter() {
}

@NonNull
@Override
public subjectHolderonCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflaterlayoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.list,parent,false);
return new subjectHolder(view);

}
@Override
public void onBindViewHolder(@NonNull subjectHolder holder, int position)
{ modal modal = (com.example.recyclerview.modal) data.get(position);
holder.textView.setText(modal.getName());
}
@Override
public int getItemCount() {
return data.size();
}

public void filterList(ArrayListfilterllist) {


data = filterllist;
}

public class subjectHolder extends RecyclerView.ViewHolder {


CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)
Enroll-no: 202103103510185

ImageViewimageView;
TextViewtextView;

public subjectHolder(@NonNull View itemView)


{ super(itemView);
imageView=(ImageView) itemView.findViewById(R.id.imageView);
textView=(TextView) itemView.findViewById(R.id.textView);
}
}
}

modal.java:
package com.example.recyclerview;

public class modal extends adapter {

private String Name;

public modal(String courseName) {

this.Name = courseName;

public String getName() {

return Name;}

public void setName(String Name) {

this.Name = Name;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Output:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Item not present in list :

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical-4
AIM: Create an application that have two activities: - Registration of student
– Login. Login activity check for user id and password. On successful login go
to home screen and display user data on home page. Registration of student
activity have “Registration” button. If user clicks on “Registration” button
alert dialog will be display. If user click on yes, registration details will be
stored in database by using SQLite. If user click on “no”, registration details
will be display only in toast notification. Registrations of students contain
student name, address, and contact number, emailed, date of birth
CODE:
activity_main.xml
<?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:layout_marginStart="172dp"
android:layout_marginTop="160dp"
android:text="Login"
android:textColor="@color/black"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:ems="10"
android:hint="Name"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />

<EditText
android:id="@+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/name"
app:layout_constraintTop_toBottomOf="@+id/name" />

<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Sign-In"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pass" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="New User ?"
android:textSize="18dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/login" />

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

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Sign-Up"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
</androidx.constraintlayout.widget.ConstraintLayout>

activity_home.xml

<?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=".Home">

<TextView
android:id="@+id/l_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="262dp"
android:text="Hello"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/l_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Hello"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="@+id/l_name"
app:layout_constraintTop_toBottomOf="@+id/l_name" />

<TextView
android:id="@+id/l_dob"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Hello"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="@+id/l_phone"
app:layout_constraintTop_toBottomOf="@+id/l_phone" />

<TextView
android:id="@+id/l_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Hello"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="@+id/l_dob"
app:layout_constraintTop_toBottomOf="@+id/l_dob" />

<TextView
android:id="@+id/l_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Hello"
android:textSize="25dp"
app:layout_constraintStart_toStartOf="@+id/l_email"
app:layout_constraintTop_toBottomOf="@+id/l_email" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_register.xml
<?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=".register">

<TextView
android:id="@+id/textView3"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:text="Register"
android:textColor="@color/black"
android:textSize="25dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<EditText
android:id="@+id/pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/name"
app:layout_constraintTop_toBottomOf="@+id/name" />

<EditText
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Phone No"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:inputType="phone"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/pass"
app:layout_constraintTop_toBottomOf="@+id/pass" />

<EditText
android:id="@+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="DOB"
android:inputType="date"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/email"
app:layout_constraintTop_toBottomOf="@+id/email" />

<Button
android:id="@+id/reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="112dp"
android:text="Sign-Up"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/address"
app:layout_constraintVertical_bias="1.0" />

<EditText
android:id="@+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Address"
android:inputType="textPostalAddress"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/dob"
app:layout_constraintTop_toBottomOf="@+id/dob" />

<EditText

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="1dp"
android:layout_marginTop="76dp"
android:ems="10"
android:hint="E-mail"
android:inputType="textEmailAddress"
android:minHeight="48dp"
app:layout_constraintStart_toStartOf="@+id/phone"
app:layout_constraintTop_toTopOf="@+id/phone" />

</androidx.constraintlayout.widget.ConstraintLayout>

DatabaseHelper.java

package com.example.prac3database;

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

import androidx.annotation.Nullable;

public class DatabaseHelper extends SQLiteOpenHelper {


public DatabaseHelper(Context context) {
super(context, "prac3_db", null, 1);
}

@Override
public void onCreate(SQLiteDatabase d) {
d.execSQL("create table auth_tbl(id integer primary key autoincrement, name text,
pass text, phone integer, email text, dob date, address text)");
}

@Override
public void onUpgrade(SQLiteDatabase d, int i, int i1)
{ d.execSQL("drop table if exists auth_tbl");
}

void insert(String name, String pass, String phone, String email, String dob, String address){

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

SQLiteDatabase db = getWritableDatabase();
db.execSQL("insert into auth_tbl(name, pass, phone, email, dob, address)
values('"+name+"','"+pass+"', '"+phone+"', '"+email+"', '"+dob+"', '"+address+"')");
}

public Cursor fetch() {


SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from auth_tbl",
null); return cursor;
}

MainActivity.java

package com.example.prac3database;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity


{ EditText name,pass;
Button signup,login;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.name);
pass = (EditText)findViewById(R.id.pass);
login = (Button)findViewById(R.id.login);
signup = (Button)findViewById(R.id.signup);

signup.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View view) {

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Intent i = new Intent(MainActivity.this,register.class);


startActivity(i);
}
});
login.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View View){
String name2 = name.getText().toString();
String pass2 = pass.getText().toString();

DatabaseHelper db = new DatabaseHelper(MainActivity.this);


Cursor c = db.fetch();
if(c.moveToFirst()){
do{
if(c.getString(1).equals(name2) && c.getString(2).equals(pass2))
{ Intent i = new Intent(MainActivity.this,Home.class);
i.putExtra("username", name2);
startActivity(i);
}
}while (c.moveToNext());
}
}
});
}
}

Home.java

package com.example.prac3database;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends AppCompatActivity


{ TextView name,pass,phone,email,dob,address;
@Override
protected void onCreate(Bundle savedInstanceState) {

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
name = (TextView)findViewById(R.id.l_name);
phone = (TextView)findViewById(R.id.l_phone);
email = (TextView)findViewById(R.id.l_email);
dob = (TextView)findViewById(R.id.l_dob);
address = (TextView)findViewById(R.id.l_address);
DatabaseHelper db = new DatabaseHelper(Home.this);
Cursor cur = db.fetch();
Intent i = getIntent();
String name3 = i.getStringExtra("username");

if(cur.moveToFirst()){
do{
if(cur.getString(1).equals(name3)) {
String name4 = cur.getString(1);
String phone4 = cur.getString(3);
String email4 = cur.getString(4);
String dob4 = cur.getString(5);
String address4 = cur.getString(6);

name.setText("Name: " + name4);


phone.setText("Phone: " + phone4);
email.setText("Email: " + email4);
dob.setText("DOB: " + dob4);
address.setText("Address: " + address4);
break;
}
}while(cur.moveToNext());
}
}
}

Register.java
package com.example.prac3database;

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

import android.content.DialogInterface;
import android.os.Bundle;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

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

public class register extends AppCompatActivity {


EditText name,pass,phone,email,dob,address;
Button reg;
@Override
protected void onCreate(Bundle savedInstanceState) {
ActionBar actionbar = getSupportActionBar();
actionbar.hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
name = (EditText)findViewById(R.id.name);
pass = (EditText)findViewById(R.id.pass);
phone = (EditText)findViewById(R.id.phone);
email = (EditText)findViewById(R.id.email);
dob = (EditText)findViewById(R.id.dob);
address = (EditText)findViewById(R.id.address);
reg = (Button)findViewById(R.id.reg);

reg.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View view) {
String name1 = name.getText().toString();
String pass1 = pass.getText().toString();
String phone1 = phone.getText().toString();
String email1 = email.getText().toString();
String dob1 = dob.getText().toString();
String address1 = address.getText().toString();
AlertDialog.Builder ad = new AlertDialog.Builder(register.this);
ad.setTitle("Insert Data"); ad.setMessage("Do you
want to Registered");
ad.setIcon(android.R.drawable.stat_sys_warning);
ad.setCancelable(true);
ad.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{ @Override
public void onClick(DialogInterface dialogInterface, int i) {
DatabaseHelper obj = new DatabaseHelper(register.this);
obj.insert(name1, pass1, phone1, email1, dob1, address1);
}
});

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

ad.setNegativeButton("No", new DialogInterface.OnClickListener()


{ @Override
public void onClick(DialogInterface dialogInterface, int i)
{ Toast.makeText(register.this, "name " + name1 + "\n" + "pass " + pass1 + "\n"
+
"phone " + phone1 + "\n" + "email " + email1 + "\n" + "dob " + dob1 + "\n" + "address "
+ address1, Toast.LENGTH_SHORT).show();
}
});
ad.show();
}
});
}
}

OUTPUT: on Click Sign-up Button

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical 5
AIM: Design an activity which contains three fragments. First fragment
contains image of the institute, second fragment contains available courses. If
user selects a particular course from the list, then third Fragment displays the
description of selected course from the second fragment.

CODE:

1. Interface File

package com.hbproduction.fragment;
public interface Communicator {
public void respond(int i);
}

2. Main .java File

package com.hbproduction.fragment;
import android.app.Fragment;

import android.support.v4.app.FragmentManager;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.app.FragmentTransaction;
//import android.app.FragmentManager;

public class MainActivity extends AppCompatActivity implements Communicator{

@Override

protected void onCreate(Bundle savedInstanceState)


{ super.onCreate(savedInstanceState);

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

setContentView(R.layout.activity_main);
}
@Override
public void respond(int i) {
FragmentManager manager=getSupportFragmentManager();

f2 f=(f2)manager.findFragmentById(R.id.fragment3);
f.changedata(i);
}
}

3. Fragment1 .java File

package com.hbproduction.fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.lang.reflect.Array;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

import java.util.ArrayList;
import java.util.List;

public class f1 extends Fragment implements AdapterView.OnItemClickListener {


/ TODO: Rename parameter arguments, choose names that match
/ the fragment initialization parameters, e.g. ARG_ITEM_NUMBER

ListView list1;
Communicator communicator;

public View onCreateView(LayoutInflater inflater, ViewGroup


container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_f1, container, false);
}
@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState)


{ super.onActivityCreated(savedInstanceState);
communicator=(Communicator)getActivity();
list1=(ListView)getActivity().findViewById(R.id.l1);
ArrayAdapter
adapter=ArrayAdapter.createFromResource(getActivity(),R.array.course,android.R.layout.simpl
e_list_item_1);
list1.setAdapter(adapter);
list1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
communicator.respond(position);
}
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

4. Fragment2 .java File

package com.hbproduction.fragment;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class f2 extends Fragment {


TextView textView;
@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,


Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_f2, container, false);
}
@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState)


{ super.onActivityCreated(savedInstanceState);
textView=(TextView) getActivity().findViewById(R.id.t1);
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

public void changedata(int i)


{
Resources res=getResources();
String[] desc=res.getStringArray(R.array.Desc1);

textView.setTextSize(50.0f);

//textView.setTextColor(5);

textView.setText(desc[i]);

5. Image Fragment .java File

package com.hbproduction.fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class image extends Fragment {


/ TODO: Rename parameter arguments, choose names that match

/ the fragment initialization parameters, e.g.


ARG_ITEM_NUMBER ImageView t;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup


container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_image, container, false);
}
@Override

public void onActivityCreated(@Nullable Bundle savedInstanceState)


{ super.onActivityCreated(savedInstanceState);
t=(ImageView) getActivity().findViewById(R.id.img1);
}
}

Output:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical-6
AIM: Create an application that will play a media file from the memory card
and phone storage.
CODE:
MainActivity.class
package com.example.musicplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity; import


androidx.recyclerview.widget.LinearLayoutManager; import
androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements


PlaylistAdapter.OnItemClickListener {
String path;
MediaPlayer mediaPlayer;
ImageButton play;
Boolean isPlaying = false;
int pos = -1;

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

mediaPlayer = new MediaPlayer();


play = findViewById(R.id.play);
path = getExternalFilesDir(Environment.DIRECTORY_MUSIC).getPath();

ImageButton prev = findViewById(R.id.prev);


ImageButton next = findViewById(R.id.next);

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

RecyclerView recyclerView = findViewById(R.id.rview);


recyclerView.setLayoutManager(new LinearLayoutManager(this));

List<String> songsList = new ArrayList<>();


File directory = new File(path);
File[] files = directory.listFiles();
if (files != null) {
for (File file : files) {
songsList.add(file.getName());
}
}

PlaylistAdapter adapter = new PlaylistAdapter(this,


songsList); adapter.setOnItemClickListener(this);
recyclerView.setAdapter(adapter);

play.setOnClickListener(view -> {
if (isPlaying) {
mediaPlayer.pause();
isPlaying = false;
play.setImageResource(R.drawable.ic_baseline_play_arrow_24);
} else { mediaPlayer.start(); isPlaying = true;

play.setImageResource(R.drawable.ic_baseline_pause_24);

}
});

prev.setOnClickListener(view -> {
if (files != null && pos != -1 && pos != 0) {
pos--;
try {
playSong(files[pos].getPath());
Toast.makeText(this, "Now Playing: " + files[pos].getName(),
Toast.LENGTH_SHORT).show();
} catch (IOException e)
{ e.printStackTrace();
}
}
});

next.setOnClickListener(view -> {
if (files != null && pos != -1 && pos < files.length - 1)
{ pos++;
try {
playSong(files[pos].getPath());

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Toast.makeText(this, "Now Playing: " + files[pos].getName(),


Toast.LENGTH_SHORT).show();
} catch (IOException e)
{ e.printStackTrace();
}
}
});

private void playSong(String path) throws IOException {


mediaPlayer.reset();
mediaPlayer.setDataSource(path);
mediaPlayer.prepare();
mediaPlayer.start();
isPlaying = true;
}

@Override
public void onItemClick(View v, int pos) { TextView textView
= v.findViewById(R.id.song);
play.setImageResource(R.drawable.ic_baseline_pause_24);
this.pos = pos;
try {
playSong(path + "/" + textView.getText().toString());
Toast.makeText(this, "Now Playing: " + textView.getText().toString(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
}

PlaylistAdapter.class
package com.example.musicplayer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

public class PlaylistAdapter extends RecyclerView.Adapter<PlaylistAdapter.ViewHolder>


{ private final List<String> songs;
private final LayoutInflater mInflater;
private OnItemClickListener listener;

PlaylistAdapter(Context context, List<String> songs) {


this.mInflater = LayoutInflater.from(context);
this.songs = songs;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{ View view = mInflater.inflate(R.layout.row, parent, false);
return new ViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position)
{ holder.textView.setText(songs.get(position));
}

@Override
public int getItemCount() {
return songs.size();
}

public void setOnItemClickListener(OnItemClickListener listener)


{ this.listener = listener;
}

public interface OnItemClickListener {


void onItemClick(View v, int position);
}

public class ViewHolder extends RecyclerView.ViewHolder


implements View.OnClickListener {
public TextView textView;

public ViewHolder(@NonNull View itemView)


{ super(itemView);
textView = itemView.findViewById(R.id.song);
itemView.setOnClickListener(this);
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

@Override
public void onClick(View view)
{ listener.onItemClick(view, getAdapterPosition());
}
}
}

activity_main.xml
<?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">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rview"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.85"
app:layout_constraintTop_toTopOf="parent"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="16dp"
android:weightSum="3"
app:layout_constraintHeight_percent="0.15"
app:layout_constraintTop_toBottomOf="@+id/rview">
<ImageButton
android:id="@+id/prev"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/ic_baseline_skip_previous_24"
android:scaleType="fitCenter"
android:layout_height="match_parent"
android:backgroundTint="@color/black"
app:tint="@color/white" />
<ImageButton
android:id="@+id/play"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/ic_baseline_play_arrow_24"
android:scaleType="fitCenter"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:layout_height="match_parent"
android:backgroundTint="@color/purple_500"
app:tint="@color/white"/>
<ImageButton
android:id="@+id/next"
android:layout_width="0dp"
android:layout_weight="1"
android:src="@drawable/ic_baseline_skip_next_24"
android:scaleType="fitCenter"
android:layout_height="match_parent"
android:backgroundTint="@color/black"
app:tint="@color/white"/>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/song"
android:layout_width="match_parent"
android:textAppearance="?android:textAppearanceMedium"
android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

OUTPUT:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical-7
AIM: Create an application to take picture and using native application.
CODE:
MainActivity.class
package com.potato.caprec;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.VideoView;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity


{ Uri videoUri;

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

ImageView imageView = findViewById(R.id.imgview);


VideoView videoView = findViewById(R.id.vidview);
ActivityResultLauncher<Intent> captureResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK) {
Intent data = result.getData();
Bitmap image = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(image);

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

File path = new


File(getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath());
String newfile = System.currentTimeMillis() + ".jpeg";
File file = new File(path, newfile); try {

FileOutputStream fos = new FileOutputStream(file);


image.compress(Bitmap.CompressFormat.JPEG,90,fos);
fos.close();
} catch (FileNotFoundException e)
{ e.printStackTrace();
} catch (IOException e)
{ e.printStackTrace();
}

}
});
ActivityResultLauncher<Intent> videoResult = registerForActivityResult( new
ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == RESULT_OK) {
videoUri = result.getData().getData();
File path = new
File(getExternalFilesDir(Environment.DIRECTORY_DCIM).getAbsolutePath());
String newfile = System.currentTimeMillis() + ".mp4";
try {
OutputStream out = new FileOutputStream(newfile);
} catch (FileNotFoundException e)
{ e.printStackTrace();
}

}
});

Button capImg = findViewById(R.id.captureImg);


capImg.setOnClickListener(view -> {
Intent cam = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureResult.launch(cam);
});

Button recordVid = findViewById(R.id.recordVideo);


recordVid.setOnClickListener(view -> {
Intent recVid = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
videoResult.launch(recVid);
});

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

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

playVideo.setOnClickListener(view -> {
videoView.setVideoURI(videoUri);
videoView.start();
});

}
}

activity_main.xml
<?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">

<ImageView
android:id="@+id/imgview"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_width="200dp"
android:layout_height="300dp" />

<VideoView
android:id="@+id/vidview"
android:layout_width="200dp"
android:layout_height="300dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/imgview"/>

<Button
android:id="@+id/captureImg"
android:text="capture image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/imgview"
android:backgroundTint="@color/black"/>

<Button
android:id="@+id/recordVideo"
android:text="Record Video"
android:layout_width="match_parent"
android:layout_height="wrap_content"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

app:layout_constraintTop_toBottomOf="@+id/captureImg"
android:backgroundTint="@color/black"/>

<Button
android:id="@+id/playVideo"
android:text="Play Video"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/recordVideo"
android:backgroundTint="@color/black"/>

</androidx.constraintlayout.widget.ConstraintLayout>

OUTPUT:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical-8
AIM: Create an application to demonstrate the use of Google Map.
CODE:
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.googlemap">
<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.GoogleMap">

<!--
TODO: Before you run your application, you need a Google Maps API key.

To get one, follow the directions here:

https://developers.google.com/maps/documentation/android-sdk/get-api-key

Once you have your API key (it starts with "AIza"), define a new property in
your project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace
the "YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyACZB4Bo2y2LLFNGIhkkTTINV8Rtfa-vmo" />

<activity
android:name=".MapsActivity"
android:exported="true"
android:label="@string/title_activity_maps">
<intent-filter>

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

<action android:name="android.intent.action.MAIN" />

<category
android:name="android.intent.category.LAUNCHER" /> </intent-
filter>
</activity>
</application>

</manifest>

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

MapActivity.java
package com.example.googlemap;

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.example.googlemap.databinding.ActivityMapsBinding;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

private GoogleMap mMap;


private ActivityMapsBinding binding;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);

binding = ActivityMapsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());

/ 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;

// Add a marker in Gandevi and move the camera


LatLng gandevi = new LatLng(20.80766767, 72.999203);
mMap.addMarker(new MarkerOptions().position(gandevi).title("Marker in
Gandevi")); mMap.moveCamera(CameraUpdateFactory.newLatLng(gandevi));
}
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

OUTPUT:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Practical-9
AIM: Design and develop an activity for login and registration from to
insert, update, delete and search using firebase.
CODE:
activity_signup.xml
<?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=".SignupActivity"> <LinearLayout

android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
tools:ignore="MissingConstraints">

<ImageView
android:layout_width="@dimen/logo_w_h"
android:layout_height="@dimen/logo_w_h"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:src="@mipmap/ic_launcher" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/white" />

</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/hint_password"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
android:textColor="@android:color/white"
tools:ignore="InvalidImeActionId" />

</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/sign_up_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorAccent"
android:text="@string/action_sign_in_short"
android:textColor="@android:color/black"
android:textStyle="bold" />

<Button

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:id="@+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_forgot_password"
android:textAllCaps="false"
android:textColor="@color/colorAccent" />

<!-- Link to Login Screen -->

<Button
android:id="@+id/sign_in_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_link_to_login"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="15dp" />
</LinearLayout>

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone"
tools:ignore="MissingConstraints" />

</androidx.constraintlayout.widget.ConstraintLayout>

SignupActivity.class
package com.example.firelogin;

import android.content.Intent;
import android.os.Bundle;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity; import
com.google.android.gms.tasks.OnCompleteListener; import
com.google.android.gms.tasks.Task; import
com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class SignupActivity extends AppCompatActivity


{ private EditText inputEmail, inputPassword;
private Button btnSignIn, btnSignUp,
btnResetPassword; private ProgressBar progressBar;
private FirebaseAuth auth;

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

//Get Firebase auth instance


auth = FirebaseAuth.getInstance();

btnSignIn = (Button) findViewById(R.id.sign_in_button); btnSignUp


= (Button) findViewById(R.id.sign_up_button); inputEmail =
(EditText) findViewById(R.id.email); inputPassword = (EditText)
findViewById(R.id.password); progressBar = (ProgressBar)
findViewById(R.id.progressBar); btnResetPassword = (Button)
findViewById(R.id.btn_reset_password);

btnResetPassword.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

});

btnSignIn.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
finish();
}
});

btnSignUp.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {

String email = inputEmail.getText().toString().trim();


String password = inputPassword.getText().toString().trim();

if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!",
Toast.LENGTH_SHORT).show();
return;
}

if(email.endsWith("@gmail.com")){
Toast.makeText(getApplicationContext(), "Enter email address which ends with
@gmail.com !", Toast.LENGTH_SHORT).show();
return;
}

if (TextUtils.isEmpty(password))
{ Toast.makeText(getApplicationContext(), "Enter password!",
Toast.LENGTH_SHORT).show();
return;
}

if (password.length() < 6) {
Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6
characters!", Toast.LENGTH_SHORT).show();
return;
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

progressBar.setVisibility(View.VISIBLE);
//create user
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(SignupActivity.this, new
OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task)
{ Toast.makeText(SignupActivity.this, "createUserWithEmail:onComplete:" +
task.isSuccessful(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
/ If sign in fails, display a message to the user. If sign in succeeds
/ the auth state listener will be notified and logic to handle the
/ signed in user can be handled in the listener.
if (!task.isSuccessful()) {
Toast.makeText(SignupActivity.this, "Authentication failed." +
task.getException(),
Toast.LENGTH_SHORT).show();
} else {
startActivity(new Intent(SignupActivity.this, MainActivity.class));
finish();
}
}
});

}
});
}

@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}
}

activity_login.xml
<?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"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="vertical"
android:padding="@dimen/activity_horizontal_margin"
tools:ignore="MissingConstraints">

<ImageView
android:layout_width="@dimen/logo_w_h"
android:layout_height="@dimen/logo_w_h"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="30dp"
android:src="@mipmap/ic_launcher" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:textColor="@android:color/white"
android:textColorHint="@android:color/white" />
</com.google.android.material.textfield.TextInputLayout>

<!-- Login Button -->

<Button
android:id="@+id/btn_login"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@color/colorAccent"
android:text="@string/btn_login"
android:textColor="@android:color/black" />

<Button
android:id="@+id/btn_reset_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
android:text="@string/btn_forgot_password"
android:textAllCaps="false"
android:textColor="@color/colorAccent" />

<!-- Link to Login Screen -->

<Button
android:id="@+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:text="@string/btn_link_to_register"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="15dp" />
</LinearLayout>

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center|bottom"
android:layout_marginBottom="20dp"
android:visibility="gone"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

LoginActivity.class
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; import
com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

public class LoginActivity extends AppCompatActivity {


private EditText inputEmail, inputPassword;
private FirebaseAuth auth;
private ProgressBar progressBar;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

private Button btnSignup, btnLogin, btnReset;

@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);

//Get Firebase auth instance


auth = FirebaseAuth.getInstance();

if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}

/ set the view now


setContentView(R.layout.activity_login);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


setSupportActionBar(toolbar);

inputEmail = (EditText) findViewById(R.id.email);


inputPassword = (EditText) findViewById(R.id.password);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
btnSignup = (Button) findViewById(R.id.btn_signup);
btnLogin = (Button) findViewById(R.id.btn_login);
btnReset = (Button) findViewById(R.id.btn_reset_password);

//Get Firebase auth instance


auth = FirebaseAuth.getInstance();

btnSignup.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, SignupActivity.class));
}
});

btnReset.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));


}
});

btnLogin.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
String email = inputEmail.getText().toString();
final String password = inputPassword.getText().toString();

if (TextUtils.isEmpty(email)) {
Toast.makeText(getApplicationContext(), "Enter email address!",
Toast.LENGTH_SHORT).show();
return;
}

if (TextUtils.isEmpty(password))
{ Toast.makeText(getApplicationContext(), "Enter password!",
Toast.LENGTH_SHORT).show();
return;
}

progressBar.setVisibility(View.VISIBLE);

//authenticate user
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(LoginActivity.this,
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
/ If sign in fails, display a message to the user. If sign in succeeds
/ the auth state listener will be notified and logic to handle the
/ signed in user can be handled in the listener.
progressBar.setVisibility(View.GONE);
if (!task.isSuccessful()) {
/ there was an error
if (password.length() < 6) {
inputPassword.setError(getString(R.string.minimum_password));
} else {

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Toast.makeText(LoginActivity.this,
getString(R.string.auth_failed), Toast.LENGTH_LONG).show();
}
} else {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
}

activity_main.xml
<?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">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:elevation="0dp"
tools:ignore="MissingConstraints">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay" />

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

</com.google.android.material.appbar.AppBarLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:ignore="MissingConstraints">

<EditText
android:id="@+id/old_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />

<EditText
android:id="@+id/new_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_new_email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true" />

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/hint_password"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:maxLines="1"
android:singleLine="true"
tools:ignore="InvalidImeActionId" />

<EditText
android:id="@+id/newPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="@string/new_pass"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="textPassword"
android:maxLines="1"
android:singleLine="true"
tools:ignore="InvalidImeActionId" />

<Button
android:id="@+id/changeEmail" style="?
android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:text="@string/btn_change"
android:textColor="@android:color/white"
android:textStyle="bold" />

<Button
android:id="@+id/changePass" style="?
android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:text="@string/btn_change"
android:textColor="@android:color/white"
android:textStyle="bold" />

<Button

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:id="@+id/send"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:text="@string/btn_send"
android:textColor="@android:color/white"
android:textStyle="bold" />

<ProgressBar
android:id="@+id/progressBar"
android:layout_width="30dp"
android:layout_height="30dp"
android:visibility="gone" />

<Button
android:id="@+id/remove" style="?
android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimaryDark"
android:text="@string/btn_remove"
android:textColor="@android:color/white"
android:textStyle="bold" />

<Button
android:id="@+id/change_email_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/change_email"
android:textStyle="bold" />

<Button
android:id="@+id/change_password_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/change_password"
android:textStyle="bold" />

<Button
android:id="@+id/sending_pass_reset_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/send_password_reset_email"
android:textStyle="bold" />

<Button
android:id="@+id/remove_user_button"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/remove_user"
android:textStyle="bold" />

<Button
android:id="@+id/sign_out"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@color/colorPrimary"
android:text="@string/btn_sign_out"
android:textColor="@android:color/white"
android:textStyle="bold" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.class
package com.example.firelogin;

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

package com.example.firelogin;

import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull; import
androidx.appcompat.widget.Toolbar;

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

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task; import
com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class MainActivity extends AppCompatActivity {

private Button btnChangeEmail, btnChangePassword, btnSendResetEmail,


btnRemoveUser, changeEmail, changePassword, sendEmail, remove, signOut;

private EditText oldEmail, newEmail, password,


newPassword; private ProgressBar progressBar;
private FirebaseAuth.AuthStateListener
authListener; private FirebaseAuth auth;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);


toolbar.setTitle(getString(R.string.app_name));
setSupportActionBar(toolbar);

//get firebase auth instance

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

auth = FirebaseAuth.getInstance();

//get current user


final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

authListener = new FirebaseAuth.AuthStateListener()


{ @Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth)
{ FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
/ user auth state is changed - user is null
/ launch login activity
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}
}
};

btnChangeEmail = (Button) findViewById(R.id.change_email_button);


btnChangePassword = (Button) findViewById(R.id.change_password_button);
btnSendResetEmail = (Button) findViewById(R.id.sending_pass_reset_button);
btnRemoveUser = (Button) findViewById(R.id.remove_user_button);
changeEmail = (Button) findViewById(R.id.changeEmail); changePassword =
(Button) findViewById(R.id.changePass);
sendEmail = (Button) findViewById(R.id.send);
remove = (Button) findViewById(R.id.remove);
signOut = (Button) findViewById(R.id.sign_out);

oldEmail = (EditText) findViewById(R.id.old_email);


newEmail = (EditText) findViewById(R.id.new_email);
password = (EditText) findViewById(R.id.password);
newPassword = (EditText) findViewById(R.id.newPassword);

oldEmail.setVisibility(View.GONE);
newEmail.setVisibility(View.GONE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.GONE);
changeEmail.setVisibility(View.GONE);
changePassword.setVisibility(View.GONE);
sendEmail.setVisibility(View.GONE);

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

remove.setVisibility(View.GONE);

progressBar = (ProgressBar) findViewById(R.id.progressBar);

if (progressBar != null) {
progressBar.setVisibility(View.GONE);
}

btnChangeEmail.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
oldEmail.setVisibility(View.GONE);
newEmail.setVisibility(View.VISIBLE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.GONE);
changeEmail.setVisibility(View.VISIBLE);
changePassword.setVisibility(View.GONE);
sendEmail.setVisibility(View.GONE);
remove.setVisibility(View.GONE);
}
});

changeEmail.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (user != null && !newEmail.getText().toString().trim().equals(""))
{ user.updateEmail(newEmail.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{ if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Email address is updated. Please
sign in with new email id!", Toast.LENGTH_LONG).show();
signOut();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to update
email!", Toast.LENGTH_LONG).show();
progressBar.setVisibility(View.GONE);

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

}
}
});
} else if (newEmail.getText().toString().trim().equals("")) {
newEmail.setError("Enter email");
progressBar.setVisibility(View.GONE);
}
}
});

btnChangePassword.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v)
{ oldEmail.setVisibility(View.GONE);
newEmail.setVisibility(View.GONE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.VISIBLE);
changeEmail.setVisibility(View.GONE);
changePassword.setVisibility(View.VISIBLE);
sendEmail.setVisibility(View.GONE);
remove.setVisibility(View.GONE);
}
});

changePassword.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (user != null && !newPassword.getText().toString().trim().equals("")) {
if (newPassword.getText().toString().trim().length() < 6) {
newPassword.setError("Password too short, enter minimum 6
characters"); progressBar.setVisibility(View.GONE);
} else {
user.updatePassword(newPassword.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{ if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Password is updated, sign in
with new password!", Toast.LENGTH_SHORT).show();

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

signOut();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to update
password!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
} else if (newPassword.getText().toString().trim().equals(""))
{ newPassword.setError("Enter password");
progressBar.setVisibility(View.GONE);
}
}
});

btnSendResetEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
oldEmail.setVisibility(View.VISIBLE);
newEmail.setVisibility(View.GONE);
password.setVisibility(View.GONE);
newPassword.setVisibility(View.GONE);
changeEmail.setVisibility(View.GONE);
changePassword.setVisibility(View.GONE);
sendEmail.setVisibility(View.VISIBLE);
remove.setVisibility(View.GONE);
}
});

sendEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (!oldEmail.getText().toString().trim().equals(""))
{ auth.sendPasswordResetEmail(oldEmail.getText().toString().trim())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Reset password email is
sent!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to send reset
email!", Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
} else {
oldEmail.setError("Enter email");
progressBar.setVisibility(View.GONE);
}
}
});

btnRemoveUser.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
if (user != null) {
user.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task)
{ if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, "Your profile is deleted:( Create a
account now!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, SignupActivity.class));
finish();
progressBar.setVisibility(View.GONE);
} else {
Toast.makeText(MainActivity.this, "Failed to delete your account!",
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

}
}
});

signOut.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
signOut();
}
});

//sign out method


public void signOut() {
auth.signOut();
}

@Override
protected void onResume() {
super.onResume();
progressBar.setVisibility(View.GONE);
}

@Override
public void onStart() {
super.onStart();
auth.addAuthStateListener(authListener);
}

@Override
public void onStop() {
super.onStop();
if (authListener != null) {
auth.removeAuthStateListener(authListener);
}
}

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

OUTPUT:
SignUp Page (Create)

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Login Page

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Update the Email:

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)


Enroll-no: 202103103510185

Remove the User

CGPIT/CE/SEM-6/MOBILE APPLICATION DEVELOPMENT (ANDROID)

You might also like