Professional Documents
Culture Documents
Android 280
Android 280
Practical-1
AIM: Installation of android SDK, android studio and creating android AVD.
CODE:
System requirements to install 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 4 : "Android Studio Setup" will appear on the screen and click "Next" to proceed.
Enroll-no: 202103103510280
Step 5 : Select the components that you want to install and click on the "Next" button.
Enroll-no: 202103103510280
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: 202103103510280
Step 8 : After the successful completion of the installation, click on the "Next" button.
Studio
Click on finish
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"
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
[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);
}
}
[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>
[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">
<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:
Practical-3
AIM: Create an android application that should be able to find entered quary
(searchview) from RecyclerView.
CODE:
activity_main.xml
</androidx.recyclerview.widget.RecyclerView>
list.xml
<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>
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: 202103103510280
@Override
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{ @Override
public booleanonQueryTextSubmit(String query) {
return false;
}
@Override
return true;
}
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: 202103103510280
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();
}
ImageViewimageView;
TextViewtextView;
modal.java:
package com.example.recyclerview;
this.Name = courseName;
return Name;}
this.Name = Name;
Output:
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"
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"
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
<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"
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"
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"
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
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;
@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){
SQLiteDatabase db = getWritableDatabase();
db.execSQL("insert into auth_tbl(name, pass, phone, email, dob, address)
values('"+name+"','"+pass+"', '"+phone+"', '"+email+"', '"+dob+"', '"+address+"')");
}
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;
signup.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
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;
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);
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;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
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);
}
});
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);
}
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;
@Override
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);
}
}
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;
import java.util.ArrayList;
import java.util.List;
ListView list1;
Communicator communicator;
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;
textView.setTextSize(50.0f);
//textView.setTextColor(5);
textView.setText(desc[i]);
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;
@Override
Output:
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 java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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());
@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;
@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();
}
@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"
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>
OUTPUT:
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
});
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();
}
}
});
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"
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:
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.
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>
<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;
@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;
OUTPUT:
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"
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
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" />
<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;
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
btnResetPassword.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
startActivity(new Intent(SignupActivity.this, ResetPasswordActivity.class));
}
});
btnSignIn.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
finish();
}
});
btnSignUp.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
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;
}
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"
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">
<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>
<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" />
<Button
android:id="@+id/btn_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
android:background="@null"
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
if (auth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
}
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) {
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 {
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" />
</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"
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
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"
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;
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
auth = FirebaseAuth.getInstance();
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);
remove.setVisibility(View.GONE);
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);
}
}
});
} 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();
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) {
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);
}
}
});
}
}
});
signOut.setOnClickListener(new View.OnClickListener()
{ @Override
public void onClick(View v) {
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);
}
}
OUTPUT:
SignUp Page (Create)
Login Page