You are on page 1of 73

Sushant Poojary MAD 2067

Sushant Poojary
2067
MAD-Journal
SYBSc CS-A
Sushant Poojary MAD 2067

CERTIFICATE

This is here to certify that Mr.Sushant Vijay Poojary, Seat


Number 2067 of S.Y B.Sc SEM IV Computer Science,
hassatisfactorily completed the required number of experiments
prescribedby the UNIVERSITY OF MUMBAI during the
academic year 2020 2021.

Date:30/03/21
Place: Mumbai

Teacher In-Charge Head of Department External

Examiner

Sushant Poojary MAD 2067 INDEX

DATE TOPIC
SR
NO.

1 15/01/21 Install Android Studio and Run Hello World Program

2 22/01/21 Create an android app with Interactive User Interface using Relative
Layouts. (e.g. simple calculator)

3 29/01/21 Create an android app that demonstrates Activity Lifecycle and


Instance State.

4 05/02/21 Create an android app that demonstrates the use of Keyboards, Input
Controls, Alerts, and Pickers.

5 12/02/21 Create an android app that demonstrates the use of an Options Menu.

6 26/02/21 Create an android app that demonstrate Screen Navigation Using the
App Bar and Tabs.

7 5/03/21 Create an android app to Connect to the Internet and use


BroadcastReceiver.

8 12/03/21 Create an android app to show Notifications and Alarm manager.

9 19/03/21 Create an android app to save user data in a database and use of
different queries.
10 26/03/21 Create an android app that uses MySQL Server using suitable
server side scripting programs.

15/01/21

Practical 1
Sushant Poojary MAD 2067 Aim: Install Android Studio and Run Hello World Program.

INSTALLATION:

1:
2:

3:
Sushant Poojary MAD 2067
4:

5:
Sushant Poojary MAD 2067
6:

7:
Sushant Poojary MAD 2067
8:

9:
Sushant Poojary MAD 2067
10:

11:
Sushant Poojary MAD 2067
12:

13:
Sushant Poojary MAD 2067
14:

15:
Sushant Poojary MAD 2067
16:

CREATING “HELLO WORLD” PROJECT:


Sushant Poojary MAD 2067
1:

2:

3:
Sushant Poojary MAD 2067
Conclusion:

Installation of Android Studio is complete and Hello World Program has


been Executed.

22/01/21
Sushant Poojary MAD 2067 Practical 2

Aim: Create an android app with Interactive User Interface using Relative Layouts.
(e.g. simple calculator)

Code:

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

<EditText
android:id="@+id/etNum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.252" />

<EditText
android:id="@+id/etNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.362" />

<Button
android:id="@+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addNumber"
android:text="Addition"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.169"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Sushant Poojary MAD 2067

app:layout_constraintVertical_bias="0.512" />

<Button
android:id="@+id/btnSub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="subNumber"
android:text="Subtration"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.183"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.632" />

<Button
android:id="@+id/btnMul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mulNumber"
android:text="Multiplication"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.75"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.512" />

<Button
android:id="@+id/btnDiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="divNumber"
android:text="Division"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.759"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.642" />

<EditText
android:id="@+id/editTextTextPersonName2"
android:layout_width="163dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/developer_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.846"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.951" />

<TextView
android:id="@+id/tVResult"
android:layout_width="81dp"
android:layout_height="39dp"
Sushant Poojary MAD 2067

android:text="Ans"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.545"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.744" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java :
package com.srs.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tVResult;
EditText etNum1,etNum2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tVResult = findViewById(R.id.tVResult);
etNum1 = findViewById(R.id.etNum1);
etNum2 = findViewById(R.id.etNum2);
}
public void addNumber(android.view.View view) {
double n1,n2,result;
n1 = Double.parseDouble(etNum1.getText().toString()); n2 =
Double.parseDouble(etNum2.getText().toString());

result = n1+n2;
tVResult.setText(String.valueOf(result));
}
public void subNumber(android.view.View view) {
double n1,n2,result;
n1 = Double.parseDouble(etNum1.getText().toString()); n2 =
Double.parseDouble(etNum2.getText().toString()); result = n1-n2;
tVResult.setText(String.valueOf(result));
}
public void mulNumber(android.view.View view) {

double n1,n2,result;
n1 = Double.parseDouble(etNum1.getText().toString()); n2 =
Double.parseDouble(etNum2.getText().toString()); result = n1*n2;
tVResult.setText(String.valueOf(result));
}
public void divNumber(android.view.View view) {
double n1,n2,result;
n1 = Double.parseDouble(etNum1.getText().toString()); n2 =
Double.parseDouble(etNum2.getText().toString()); result = n1/n2;
tVResult.setText(String.valueOf(result));
Sushant Poojary MAD 2067

}
}

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

<resources>

<color name="purple_200">#FFBB86FC</color>

<color name="purple_500">#FF6200EE</color>

<color name="purple_700">#FF3700B3</color>

<color name="teal_200">#FF03DAC5</color>

<color name="teal_700">#FF018786</color>

<color name="black">#FF000000</color>

<color name="white">#FFFFFFFF</color>

</resources>

String.xml :
<resources>

<string name="app_name">Calculator</string>

<string name="developer_name">By Saurav Singh</string>

</resources>

Themes.xml :
<resources xmlns:tools="http://schemas.android.com/tools">

<!-- Base application theme. -->

<style name="Theme.Calculator"
parent="Theme.MaterialComponents.DayNight.DarkActionBar">

<!-- Primary brand color. -->

<item name="colorPrimary">@color/purple_200</item>
Sushant Poojary MAD 2067

<item name="colorPrimaryVariant">@color/purple_700</item> <item name="colorOnPrimary">@color/black</item> <!--

Secondary brand color. -->

<item name="colorSecondary">@color/teal_200</item> <item

name="colorSecondaryVariant">@color/teal_200</item> <item name="colorOnSecondary">@color/black</item> <!--

Status bar color. -->

<item name="android:statusBarColor"
tools:targetApi="l">?attr/colorPrimaryVariant</item> <!-- Customize your theme here. --> </style>

</resources>

Output:

Outputs :
Android Studio View :

Sushant Poojary MAD 2067

Mobile View :
Sushant Poojary MAD 2067

Addition : Subtraction :
Sushant Poojary MAD 2067

Multiplication : Division:
Conclusion:
Therefore , an android app with Interactive User Interface using Relative Layouts is Created.
Sushant Poojary MAD 2067

29/01/21

Practical 3
Aim: Create an android app that demonstrates Activity Lifecycle and Instance State.
Code:

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

MainActivity.java :
package com.srs.myapplication22;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
Sushant Poojary MAD 2067

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Log.d("lifecycle","onCreate invoked");

@Override

protected void onStart() {

super.onStart();

Log.d("lifecycle","onStart invoked");
}

@Override

protected void onResume(){

super.onResume();

Log.d("lifecycle","onResume invoked");

@Override

protected void onPause() {

super.onPause();

Log.d("lifecycle","onPause invoked");

@Override

protected void onStop() {

super.onStop();

Log.d("lifecycle","onStop invoked");

@Override

protected void onRestart() {

super.onRestart();

Log.d("lifecycle","onRestart invoked");
Sushant Poojary MAD 2067

@Override

protected void onDestroy() {

super.onDestroy();

Log.d("lifecycle","onDestroy invoked"); }

Output:
Coclusion:

Android app that demonstrates Activity Lifecycle and Instance State is


created
Sushant Poojary MAD 2067

05/02/21

Practical 4
Aim: Create an android app that demonstrates the use of Keyboards, Input Controls,
Alerts, and Pickers.

Code:
ACTIVITY_MAIN.XML

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:layout_margin="20dp">

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:textSize="20sp"
android:hint="Enter Name"/>

<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="20dp"
android:hint="Enter Phone Number"/>

<EditText
android:id="@+id/email"
android:layout_width="match_parent"
Sushant Poojary MAD 2067

android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:textSize="20dp"
android:hint=" Enter E-mail Address"/>

<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:textSize="20dp"
android:hint="Password"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D.O.B. :"
android:textSize="20dp" />
<EditText
android:id="@+id/dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:hint="Double-Click to select"/>

</LinearLayout>

<RadioGroup
android:id="@+id/rg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:textSize="20dp" />

<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:textSize="20dp" />

<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:textSize="20dp" />
Sushant Poojary MAD 2067

</RadioGroup>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I AM "
android:textSize="20dp"/>

<Spinner
android:id="@+id/sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Spinner>

</LinearLayout>

<Button
android:id="@+id/bb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Submit"
android:textSize="30dp" />

</LinearLayout>

MAINACTIVITY.JAVA

package com.srs.myapplication22;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Calendar;
Sushant Poojary MAD 2067

public class MainActivity extends AppCompatActivity implements


AdapterView.OnItemSelectedListener {

String TAG = "MainActivity";


String date,mspinner;
String data[]={"Student","Working","Senior Citizen"};
EditText name,email,phone,dob;
Spinner sp;
RadioGroup rg;
RadioButton r;
String rb;
int rr;
Button b1;
DatePickerDialog.OnDateSetListener mDataListner;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText) findViewById(R.id.name);
phone = (EditText) findViewById(R.id.number);
email = (EditText) findViewById(R.id.email);
dob = (EditText) findViewById(R.id.dob);
sp = (Spinner) findViewById(R.id.sp);

rg = (RadioGroup) findViewById(R.id.rg);

b1 = (Button) findViewById(R.id.bb);

dob.setOnClickListener(v -> {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH );
//we need third parameter as on click listener

DatePickerDialog dialog = new


DatePickerDialog(MainActivity.this,R.style.Theme_AppCompat_DayNight_Dialog_MinWidt
h,mDataListner,
year,month,day);
});
//we need to implement the listener class
mDataListner = new DatePickerDialog.OnDateSetListener() { @Override
public void onDateSet(DatePicker datePicker, int year, int month, int dayOfMonth)
{
month = month + 1;
date = dayOfMonth + "/" + month + "/" + year;
dob.setText(date);
}
};

sp.setOnItemSelectedListener(this);
ArrayAdapter aa = new ArrayAdapter(
this,R.layout.support_simple_spinner_dropdown_item,data);
aa.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
sp.setAdapter(aa);
Sushant Poojary MAD 2067

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rr = rg.getCheckedRadioButtonId();
if(rr != -1);
{
r = (RadioButton) findViewById(rr);
rb = r.getText().toString();
}

AlertDialog.Builder myalert = new


AlertDialog.Builder(MainActivity.this);
myalert.setTitle("Alert");
myalert.setMessage("Click Ok to continue or else click cancel");

myalert.setPositiveButton("ok", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String Display = "name" + name.getText().toString() + "\n phone"+phone.getText().toString()+
"\n email"+email.getText().toString()+"\n gender"+rb+"\n dob "+date+"\n Who am
I"+mspinner;

Toast.makeText(MainActivity.this,Display,Toast.LENGTH_LONG).show(); }
});
myalert.setPositiveButton("cancel", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "YOU
CANCELLED",Toast.LENGTH_LONG).show();
}
});
myalert.show();
}
});
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
mspinner = sp.getSelectedItem().toString();

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
}
Sushant Poojary MAD 2067

OUTPUT
MOBILE VIEW:
Sushant Poojary MAD 2067
Sushant Poojary MAD 2067
Conclusion:

Android app that demonstrates the use of Keyboards, Input Controls,


Alerts, and Pickers is made.

12/02/21
Sushant Poojary MAD 2067 Practical 5
Aim: Create an android app that demonstrates the use of an Options Menu.

Code:
My_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">
<item
android:title="Search"
android:id="@+id/search_icon"
android:icon="@drawable/ic_baseline_search_24"
app:showAsAction="ifRoom"/>

<item
android:title="Settings"
android:label="ic_baseline_settings_24"
android:id="@+id/settings"
app:showAsAction="never"/>
<item
android:title="Bluetooth"
android:label="ic_baseline_bluetooth_24"
android:id="@+id/bluetooth"
app:showAsAction="never"/>

<item
android:title="Refresh"
android:label="ic_baseline_refresh_24"
android:id="@+id/refresh"
app:showAsAction="never"/>

</menu>

MAINACTIVITY.JAVA

package com.srs.myapplication22;

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

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
Sushant Poojary MAD 2067

import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {

switch (item.getItemId())
{
case R.id.search_icon:
Toast.makeText(this,"Search", Toast.LENGTH_SHORT).show(); return true;

case R.id.settings:
Toast.makeText(this,"Settings", Toast.LENGTH_SHORT).show(); return true;

case R.id.bluetooth:
Toast.makeText(this,"Bluetooth", Toast.LENGTH_SHORT).show(); return true;

case R.id.refresh:
Toast.makeText(this,"Refresh", Toast.LENGTH_SHORT).show(); return true;

default:
return super.onOptionsItemSelected(item);
}

}
}
OUTPUT:
Sushant Poojary MAD 2067
MOBILE VIEW:
Sushant Poojary MAD 2067
Sushant Poojary MAD 2067
Conclusion:

Android app that demonstrates the use of an Options Menu


has been executed.
Sushant Poojary MAD 2067
26/02/21

Practical 6
Aim: Create an android app that demonstrate Screen Navigation Using the App Bar

and Tabs. Code:

MainActivity.java
package com.srs.practical6;

import android.os.Bundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton; import


com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.srs.practical6.ui.main.SectionsPagerAdapter;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this,
getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
FloatingActionButton fab = findViewById(R.id.fab);
Sushant Poojary MAD 2067
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}

PageViewModel.java
package com.srs.practical6.ui.main;

import androidx.arch.core.util.Function;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.ViewModel;

public class PageViewModel extends ViewModel {

private MutableLiveData<Integer> mIndex = new MutableLiveData<>(); private


LiveData<String> mText = Transformations.map(mIndex, new Function<Integer,
String>() {
@Override
public String apply(Integer input) {
return "TAB : " + input;
}
});

public void setIndex(int index) {


mIndex.setValue(index);
}

public LiveData<String> getText() {


return mText;
}
}

PlaceHolderFragment.java

package com.srs.practical6.ui.main;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
Sushant Poojary MAD 2067
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.srs.practical6.R;

/**
* A placeholder fragment containing a simple view.
*/
public class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number"; private

PageViewModel pageViewModel;

public static PlaceholderFragment newInstance(int index) { PlaceholderFragment


fragment = new PlaceholderFragment(); Bundle bundle = new Bundle();
bundle.putInt(ARG_SECTION_NUMBER, index);
fragment.setArguments(bundle);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pageViewModel = new ViewModelProvider(this).get(PageViewModel.class); int index = 1;
if (getArguments() != null) {
index = getArguments().getInt(ARG_SECTION_NUMBER); }
pageViewModel.setIndex(index);
}

@Override
public View onCreateView(
@NonNull LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_main, container, false); final TextView textView =
root.findViewById(R.id.section_label); pageViewModel.getText().observe(this, new
Observer<String>() { @Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}

SectionsPageAdapter.java

package com.srs.practical6.ui.main;

import android.content.Context;
Sushant Poojary MAD 2067
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.srs.practical6.R;

/**
* A [FragmentPagerAdapter] that returns a fragment corresponding to * one of the
sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

@StringRes
private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2};
private final Context mContext;

public SectionsPagerAdapter(Context context, FragmentManager fm) { super(fm);


mContext = context;
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page. // Return a PlaceholderFragment
(defined as a static inner class below). return PlaceholderFragment.newInstance(position + 1); }

@Nullable
@Override
public CharSequence getPageTitle(int position) {
return mContext.getResources().getString(TAB_TITLES[position]); }

@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
}

activity_main.xml:

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


<androidx.coordinatorlayout.widget.CoordinatorLayout
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">
Sushant Poojary MAD 2067

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/Theme.Practical6.AppBarOverlay">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:minHeight="?actionBarSize"
android:padding="@dimen/appbar_padding"
android:text="@string/app_name"

android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="63dp"
android:background="?attr/colorPrimary"
app:tabTextColor="#FFFFFF" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.viewpager.widget.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Fragment_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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.PlaceholderFragment">

<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:layout_marginEnd="@dimen/activity_horizontal_margin"
Sushant Poojary MAD 2067

android:layout_marginBottom="@dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/constraintLayout"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

Output:

Sushant Poojary MAD 2067 Conclusion:

Android app that demonstrate Screen Navigation Using the App


Bar and Tabs is created.
5/03/21

Practical 7
Aim: Create an android app to Connect to the Internet and use

BroadcastReceiver. Code:

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.srs.practical7">
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission
android:name="android.permission.ACCESS_WIFI_STATE"/>

<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.Practical7">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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


</activity>
</application>

</manifest>

MainActivity.java
Sushant Poojary MAD 2067

package com.srs.practical7;

import androidx.appcompat.app.AppCompatActivity;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

checkConnection();
}

public void checkConnection(){


ConnectivityManager manager = (ConnectivityManager)
getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = manager.getActiveNetworkInfo();

if(null!=activeNetwork){
if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI){ Toast.makeText(this,
"Internet Connection Established ", Toast.LENGTH_SHORT).show();
}

if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){ Toast.makeText(this,


"Internet Connection Established ", Toast.LENGTH_SHORT).show();
}

}
else {
Toast.makeText(this, "Please Check Internet Connection",
Toast.LENGTH_SHORT).show();
}
}
}

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:layout_width="wrap_content"
Sushant Poojary MAD 2067

android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Output:

INTERNET OFF INTERNET ONN

Sushant Poojary MAD 2067


Conclusion:
Android app to Connect to the Internet and use
BroadcastReceiver is created and executed.

12/03/21

Practical 8
Sushant Poojary MAD 2067 Aim: Create an android app to show Notifications and

Alarm manager.

Code:

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

<Button
android:id="@+id/notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/notify_me"
app:layout_constraintBottom_toTopOf="@+id/update"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/update_me"
app:layout_constraintBottom_toTopOf="@+id/cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/notify" />

<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancel_me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/update" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java :
package com.srs.myapplication32;

import androidx.appcompat.app.AppCompatActivity;
Sushant Poojary MAD 2067

import androidx.core.app.NotificationCompat;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private static final String ACTION_UPDATE_NOTIFICATION =


"com.android.example.notifyme.ACTION_UPDATE_NOTIFICATION";
private static final String PRIMARY_CHANNEL_ID =
"primary_notification_channel";

private static final int NOTIFICATION_ID = 0;

private Button button_notify;


private Button button_cancel;
private Button button_update;

private NotificationManager mNotifyManager;

private NotificationReceiver mReceiver = new NotificationReceiver();

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

createNotificationChannel();

registerReceiver(mReceiver, new IntentFilter(ACTION_UPDATE_NOTIFICATION));

button_notify = findViewById(R.id.notify);
button_notify.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
sendNotification();
}
});

button_update = (Button) findViewById(R.id.update);


button_update.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
Sushant Poojary MAD 2067

updateNotification();
}
});

button_cancel = (Button) findViewById(R.id.cancel);


button_cancel.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
cancelNotification(); // Cancel the notification. }
});

setNotificationButtonState(true, false, false);


}

@Override
protected void onDestroy() {
unregisterReceiver(mReceiver);
super.onDestroy();
}

public void createNotificationChannel() {

mNotifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.O) {

NotificationChannel notificationChannel = new NotificationChannel (PRIMARY_CHANNEL_ID,


getString(R.string.notification_channel_name),
NotificationManager.IMPORTANCE_HIGH);

notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);

notificationChannel.setDescription(getString(R.string.notification_channel_descrip tion));

mNotifyManager.createNotificationChannel(notificationChannel); }
}

public void sendNotification() {

Intent updateIntent = new Intent(ACTION_UPDATE_NOTIFICATION); PendingIntent


updatePendingIntent = PendingIntent.getBroadcast(this, NOTIFICATION_ID, updateIntent,
PendingIntent.FLAG_ONE_SHOT);

NotificationCompat.Builder notifyBuilder = getNotificationBuilder();

notifyBuilder.addAction(R.drawable.ic_update, getString(R.string.update),
updatePendingIntent);
Sushant Poojary MAD 2067
mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());

setNotificationButtonState(false, true, true);


}

private NotificationCompat.Builder getNotificationBuilder() {

Intent notificationIntent = new Intent(this, MainActivity.class); PendingIntent


notificationPendingIntent = PendingIntent.getActivity (this, NOTIFICATION_ID,
notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notifyBuilder = new NotificationCompat .Builder(this,


PRIMARY_CHANNEL_ID)
.setContentTitle(getString(R.string.notification_title))
.setContentText(getString(R.string.notification_text)) .setSmallIcon(R.drawable.ic_android)
.setAutoCancel(true).setContentIntent(notificationPendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH) .setDefaults(NotificationCompat.DEFAULT_ALL);
return notifyBuilder;
}

public void updateNotification() {

Bitmap androidImage = BitmapFactory


.decodeResource(getResources(), R.drawable.mascot_1); NotificationCompat.Builder

notifyBuilder = getNotificationBuilder();

notifyBuilder.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(androidImage)
.setBigContentTitle(getString(R.string.notification_updated)));

mNotifyManager.notify(NOTIFICATION_ID, notifyBuilder.build());

setNotificationButtonState(false, false, true);


}

public void cancelNotification() {


mNotifyManager.cancel(NOTIFICATION_ID);

setNotificationButtonState(true, false, false);


}

void setNotificationButtonState(Boolean isNotifyEnabled, Boolean isUpdateEnabled,


Boolean isCancelEnabled) {
button_notify.setEnabled(isNotifyEnabled);
button_update.setEnabled(isUpdateEnabled);
Sushant Poojary MAD 2067
button_cancel.setEnabled(isCancelEnabled);
}

public class NotificationReceiver extends BroadcastReceiver {

public NotificationReceiver() {
}

@Override
public void onReceive(Context context, Intent intent) { updateNotification();
}
}

Outputs :
Mobile View :

Conclusion:
Android app to show Notifications and Alarm manager is made.

19/03/21

Practical 9
Aim: Create an android app to save user data in a database and use of different queries.
Sushant Poojary MAD 2067
Code:

MainActivity.java:
package com.srs.practical9;

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

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

public class MainActivity extends AppCompatActivity {


EditText name, contact, dob;
Button insert, update, delete, view;
DBHelper DB;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
contact = findViewById(R.id.contact);
dob = findViewById(R.id.dob);
insert = findViewById(R.id.btnInsert);
update = findViewById(R.id.btnUpdate);
delete = findViewById(R.id.btnDelete);
view = findViewById(R.id.btnView);
DB = new DBHelper(this);
insert.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
String nameTXT = name.getText().toString();
String contactTXT = contact.getText().toString(); String dobTXT =
dob.getText().toString();

Boolean checkinsertdata = DB.insertuserdata(nameTXT, contactTXT, dobTXT);


if(checkinsertdata==true)
Toast.makeText(MainActivity.this, "New Entry Inserted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "New Entry Not Inserted", Toast.LENGTH_SHORT).show();
} });
update.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
String nameTXT = name.getText().toString();
String contactTXT = contact.getText().toString(); String dobTXT =
dob.getText().toString();

Boolean checkupdatedata = DB.updateuserdata(nameTXT, contactTXT,


Sushant Poojary MAD 2067

dobTXT);
if(checkupdatedata==true)
Toast.makeText(MainActivity.this, "Entry Updated", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "New Entry Not Updated", Toast.LENGTH_SHORT).show();
} });
delete.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View view) {
String nameTXT = name.getText().toString();
Boolean checkudeletedata = DB.deletedata(nameTXT);
if(checkudeletedata==true)
Toast.makeText(MainActivity.this, "Entry Deleted", Toast.LENGTH_SHORT).show();
else
Toast.makeText(MainActivity.this, "Entry Not Deleted", Toast.LENGTH_SHORT).show();
} });

view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Cursor res = DB.getdata();
if(res.getCount()==0){
Toast.makeText(MainActivity.this, "No Entry Exists", Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("Name :"+res.getString(0)+"\n"); buffer.append("Contact :"+res.getString(1)+"\n");
buffer.append("Date of Birth :"+res.getString(2)+"\n\n"); }

AlertDialog.Builder builder = new


AlertDialog.Builder(MainActivity.this);
builder.setCancelable(true);
builder.setTitle("User Entries");
builder.setMessage(buffer.toString());
builder.show();
} });
}}

DBHelper.java:
package com.srs.practical9;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
Sushant Poojary MAD 2067

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {


public DBHelper(Context context) {
super(context, "Userdata.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase DB) {
DB.execSQL("create Table Userdetails(name TEXT primary key, contact TEXT, dob TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase DB, int i, int i1) {
DB.execSQL("drop Table if exists Userdetails");
}

public Boolean insertuserdata(String name, String contact, String dob) {


SQLiteDatabase DB = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("name", name);
contentValues.put("contact", contact);
contentValues.put("dob", dob);
long result=DB.insert("Userdetails", null, contentValues); if(result==-1){
return false;
}else{
return true;
}
}

public Boolean updateuserdata(String name, String contact, String dob) { SQLiteDatabase DB


= this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("contact", contact);
contentValues.put("dob", dob);
Cursor cursor = DB.rawQuery("Select * from Userdetails where name = ?", new String[]{name});
if (cursor.getCount() > 0) {
long result = DB.update("Userdetails", contentValues, "name=?", new String[]{name});
if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}}

public Boolean deletedata (String name)


{
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery("Select * from Userdetails where name = ?",
Sushant Poojary MAD 2067

new String[]{name});
if (cursor.getCount() > 0) {
long result = DB.delete("Userdetails", "name=?", new String[]{name}); if (result == -1) {
return false;
} else {
return true;
}
} else {
return false;
}
}

public Cursor getdata ()


{
SQLiteDatabase DB = this.getWritableDatabase();
Cursor cursor = DB.rawQuery("Select * from Userdetails", null); return cursor;

}
}

Activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/texttitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please enter the details below"
android:textSize="24dp"
android:layout_marginTop="20dp"
/>
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:textSize="24dp"
android:layout_below="@+id/texttitle"
android:inputType="textPersonName"/>

<EditText
android:id="@+id/contact"
android:layout_width="match_parent"
Sushant Poojary MAD 2067

android:layout_height="wrap_content"
android:hint="Contact"
android:textSize="24dp"
android:layout_below="@+id/name"
android:inputType="number"/>
<EditText
android:id="@+id/dob"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date of Birth"
android:textSize="24dp"
android:layout_below="@+id/contact"
android:inputType="number"/>

<Button
android:id="@+id/btnInsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:text="Insert New Data"
android:layout_marginTop="30dp"
android:layout_below="@id/dob"/>

<Button
android:id="@+id/btnUpdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:text="Update Data"
android:layout_below="@id/btnInsert"/>
<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:text="Delete Existing Data"
android:layout_below="@id/btnUpdate"/>
<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="24dp"
android:text="View Data"
android:layout_below="@id/btnDelete"/>
</RelativeLayout>

OUTPUT:
Mobile View:
Sushant Poojary MAD 2067

Inserting Data
Sushant Poojary MAD 2067

View Data
Sushant Poojary MAD 2067

Deleting Data
Sushant Poojary MAD 2067

View Data
Sushant Poojary MAD 2067

Conclusion:
Android app to save user data in a database and use of different
queries is created

26/03/21
Sushant Poojary MAD 2067 Practical 10

Aim: Create an android app that uses MySQL Server using suitable server side
scripting programs.

Code:

MainActivity.java:
package com.srs.practical10;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import java.sql.Connection;

public class MainActivity extends AppCompatActivity {

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

RegisterActivity:
package com.srs.practical10;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.srs.practical10.Connection.ConnectionClass;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class RegisterActivity extends AppCompatActivity {


EditText name,email,password;
Button registerbtn;
TextView status;
Connection con;
Sushant Poojary MAD 2067

Statement stmt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
name = (EditText)findViewById(R.id.name);
email = (EditText)findViewById(R.id.email);
password = (EditText)findViewById(R.id.password);
registerbtn = (Button)findViewById(R.id.registerbtn); status =
(TextView)findViewById(R.id.status);

registerbtn.setOnClickListener(new View.OnClickListener() { @Override


public void onClick(View v) {
new RegisterActivity.registeruser().execute(""); }
});
}

public class registeruser extends AsyncTask<String, String , String> {

String z = "";
Boolean isSuccess = false;

@Override
protected void onPreExecute() {
status.setText("Sending Data to Database");
}

@Override
protected void onPostExecute(String s) {
status.setText("Registration Successful");
name.setText("");
email.setText("");
password.setText("");
}

@Override
protected String doInBackground(String... strings) {
try{
con =
connectionClass(ConnectionClass.un.toString(),ConnectionClass.pass.toString(),Conn
ectionClass.db.toString(),ConnectionClass.ip.toString());
if(con == null){
z = "Check Your Internet Connection";
}
else{
String sql = "INSERT INTO register (name,email,password) VALUES
('"+name.getText()+"','"+email.getText()+"','"+password.getText()+"')"; stmt =
con.createStatement();
stmt.executeUpdate(sql);
}

}catch (Exception e){


isSuccess = false;
z = e.getMessage();
}
Sushant Poojary MAD 2067
return z;
}
}

@SuppressLint("NewApi")
public Connection connectionClass(String user, String password, String database, String
server){
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Connection connection = null;
String connectionURL = null;
try{
Class.forName("net.sourceforge.jtds.jdbc.Driver"); connectionURL = "jdbc:jtds:sqlserver://" +
server+"/" + database + ";user=" + user + ";password=" + password + ";";
connection = DriverManager.getConnection(connectionURL); }catch (Exception e){
Log.e("SQL Connection Error : ", e.getMessage()); }

return connection;
}
}

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

<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:textSize="25dp" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:id="@+id/email"
android:hint="Email"
android:inputType="textEmailAddress"
/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
Sushant Poojary MAD 2067
android:id="@+id/password"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:id="@+id/registerbtn" android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Register"
android:textColorLink="#171616" app:backgroundTint="#EA0505" />

<TextView
android:id="@+id/status" android:layout_width="match_parent" android:layout_height="318dp"
android:layout_marginTop="20dp" android:text="Register"
android:textSize="25dp" />
</LinearLayout>

Creating Table In Mysql Manager:

Output:
Sushant Poojary MAD 2067

Sushant Poojary MAD 2067


Sushant Poojary MAD 2067

Viewing Data in Microsoft SQL Manager:


Sushant Poojary MAD 2067

Conclusion:
Android app that uses MySQL Server using suitable server side
scripting programs is made and executed successfully.

You might also like