C21053 Jay Vijay Karwatkar Mobile Computing Journal

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

C21053 Mobile APP Lab 1st Dec,2022

Practical 1: Android program using various UI components:

a. Android Lifecycle

Code: MainActivity.java

package com.example.practical1;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
final String My_TAG = "My_Custom_Message";
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(My_TAG, "onCreate");
}

protected void onStart() {


super.onStart();
Log.i("MY_TAG", "onStart");
}

protected void onPause() {


super.onPause();
Log.i("MY_TAG", "onPause");
}

protected void onResume() {


super.onResume();
Log.i("MY_TAG", "onResume");
}

protected void onStop() {


super.onStop();
Log.i("MY_TAG", "onStop");
}

protected void onDestroy() {


super.onDestroy();
Log.i("MY_TAG", "onDestroy");
}
}

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

1|P a g e
C21053 Mobile APP Lab 1st Dec,2022

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Lifecycle Phase!"
android:textSize="20sp"
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.117" />

<ImageView
android:id="@+id/imageView7"
android:layout_width="300dp"
android:layout_height="497dp"
android:layout_marginStart="28dp"
android:layout_marginTop="172dp"
android:layout_marginEnd="83dp"
android:layout_marginBottom="62dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/android" />

</androidx.constraintlayout.widget.ConstraintLayout>

2|P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

3|P a g e
C21053 Mobile APP Lab 1st Dec,2022

b. Registration Form

Code:
package com.example.practical1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity {
Button b;
EditText etName;
EditText etEmail;
EditText etPassword;
RadioButton r1;
RadioButton r2;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById(R.id.register);
etEmail = (EditText) findViewById(R.id.email);
etPassword = (EditText) findViewById(R.id.password);
etName = (EditText) findViewById(R.id.input_name);
r1 = (RadioButton) findViewById(R.id.female);
r2 = (RadioButton) findViewById(R.id.male);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String salute = "";
if (r1.isChecked()) {
salute = "Ms";
} else {
salute = "Mr";
}
final String name = etName.getText().toString();
final String email = etEmail.getText().toString();
final String pass = etPassword.getText().toString();
Toast.makeText(MainActivity.this, "Thank you!",
Toast.LENGTH_SHORT).show();
System.out.println("Name" + salute + "." + name + "\n
Email" + email + "\n Pass" + pass);
}
});
}
}
Design:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical"
android:id="@+id/r">

4|P a g e
C21053 Mobile APP Lab 1st Dec,2022

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="158dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/image" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/input_name"
android:textColor="#ffff151f"
android:hint="Name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gender"
android:textSize="18dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/radioGroup">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="Female"
android:textSize="15dp"
android:checked="true"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="male"
android:textSize="15dp"
android:checked="false"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/email"
android:inputType="textEmailAddress"
android:textColor="#ffff1a3e"
android:hint="Email"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
<EditText

5|P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/password"
android:inputType="textPassword"
android:textColor="#ffff1a3e"
android:hint="Password"
android:layout_above="@+id/register"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register"
android:id="@+id/register"
android:layout_gravity="center_horizontal"
android:layout_marginStart="46dp"
android:layout_marginBottom="84dp"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/imageView" />
</LinearLayout>

6|P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

Vedant Kshirsagar

7|P a g e
C21053 Mobile APP Lab 1st Dec,2022

c. Simple Calculator

Code:

package com.example.practical1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Calculator extends AppCompatActivity {


Button btnadd,btnsubs,btnmult,btndiv;
EditText txt1,txt2;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);

btnadd=findViewById(R.id.btnadd);
btnsubs=findViewById(R.id.btnsubs);
btndiv=findViewById(R.id.btndiv);
btnmult=findViewById(R.id.btnmult);

txt1=findViewById(R.id.txt1);
txt2=findViewById(R.id.txt2);

result=findViewById(R.id.result);

btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checking Input First Is Blank Or Not
if (txt1.getText().toString().equals("")) {
// Showing Toast (Message)
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
} else if (txt2.getText().toString().equals("")) {
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
}

// Both Inputs Are Not Blank , Starting Calculation


else {
float a, b, c;
a = Float.parseFloat(txt1.getText().toString());
b = Float.parseFloat(txt2.getText().toString());
c = a + b; // Using Third Variable To Store
Output Value
result.setText("The Addition Result Is " + c);

}
});

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

8|P a g e
C21053 Mobile APP Lab 1st Dec,2022

public void onClick(View view) {


// Checking Input First Is Blank Or Not
if (txt1.getText().toString().equals("")) {
// Showing Toast (Message)
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
} else if (txt2.getText().toString().equals("")) {
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
}

// Both Inputs Are Not Blank , Starting Calculation


else {
float a, b, c;
a = Float.parseFloat(txt1.getText().toString());
b = Float.parseFloat(txt2.getText().toString());
c = a - b; // Using Third Variable To Store
Output Value
result.setText("The Subtraction Result Is " + c);

}
}
});
btnmult.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checking Input First Is Blank Or Not
if (txt1.getText().toString().equals("")) {
// Showing Toast (Message)
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
} else if (txt2.getText().toString().equals("")) {
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
}

// Both Inputs Are Not Blank , Starting Calculation


else {
float a, b, c;
a = Float.parseFloat(txt1.getText().toString());
b = Float.parseFloat(txt2.getText().toString());
c = a*b; // Using Third Variable To Store Output
Value
result.setText("The Multiplication Result Is " +
c);

}
}
});
btndiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checking Input First Is Blank Or Not
if (txt1.getText().toString().equals("")) {
// Showing Toast (Message)
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
} else if (txt2.getText().toString().equals("")) {
Toast.makeText(Calculator.this, "Please Enter
Number", Toast.LENGTH_SHORT).show();
}

9|P a g e
C21053 Mobile APP Lab 1st Dec,2022

// Both Inputs Are Not Blank , Starting Calculation


else {
float a, b, c;
a = Float.parseFloat(txt1.getText().toString());
b = Float.parseFloat(txt2.getText().toString());
c = a/b; // Using Third Variable To Store Output
Value
result.setText("The Division Result Is " + c);

}
}
});
}
}

Design :

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

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="111dp"
android:layout_marginTop="25dp"
app:srcCompat="@drawable/calc" />

<EditText
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="First Number"
android:inputType="numberDecimal"
tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck"
/>

<EditText
android:id="@+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:ems="10"
android:hint="Second Number"
android:inputType="numberDecimal"
tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck"
/>

10 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

<Button
android:id="@+id/btnadd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="Add" />

<Button
android:id="@+id/btnsubs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="Subtract" />

<Button
android:id="@+id/btndiv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="Divide" />

<Button
android:id="@+id/btnmult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:text="Multiply" />

<TextView
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="25dp"
android:layout_marginRight="20dp"/>
</LinearLayout>

11 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

12 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

13 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Practical 2: Android program using different layouts and views:

a) Program on Linear layout

Code:
package com.example.practical2;

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

public class MainActivity extends AppCompatActivity {


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

Design:

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

<LinearLayout
android:layout_width="409dp"
android:layout_height="665dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp"
tools:ignore="MissingConstraints">
<EditText
android:id="@+id/editTextTextPersonName6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="To:" />
<EditText
android:id="@+id/editTextTextPersonName7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Subject:" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Message:"
android:textSize="18sp" />

14 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

<Button
android:id="@+id/button8"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_marginStart="200dp"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:text="Send" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

15 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

16 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

b) Program on Grid layout

Code:
package com.example.practical2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {


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

Design:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnCount="4">

<TextView
android:text="Email setup"
android:textSize="32dip"
android:layout_columnSpan="4"
android:layout_gravity="center_horizontal"
/>

<TextView
android:text="You can configure email in just a few steps:"
android:textSize="16dip"
android:layout_columnSpan="4"
android:layout_gravity="left"
/>

<TextView
android:text="Email address:"
android:layout_gravity="right"
/>

<EditText
android:ems="10"

tools:ignore="SpeakableTextPresentCheck,TouchTargetSizeCheck" />

<TextView
android:text="Password:"
android:layout_column="0"
android:layout_gravity="right"
/>

<EditText

17 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:ems="8"

tools:ignore="TouchTargetSizeCheck,SpeakableTextPresentCheck" />

<Space
android:layout_row="4"
android:layout_column="0"
android:layout_columnSpan="3"
android:layout_gravity="fill" />

<Button
android:text="Next"
android:layout_row="5"
android:layout_column="3"
/>
</GridLayout>

18 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

c) Program on Constraint layout

Code:
package com.example.practical2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity3 extends AppCompatActivity {


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
}
}

Design:

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

19 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:gravity="center"
android:padding="10dp"
android:text="Mobile App Lab conducted by Apeksha Maam and
Pritam Sir"
android:textColor="@color/black"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

20 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

d) Program on Table layout:

Code:
package com.example.practical2;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity4 extends AppCompatActivity {


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
}
}

Design:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:outlineAmbientShadowColor="#FDFDFD"
android:outlineSpotShadowColor="#FFFFFF"
tools:context=".MainActivity4">
<ScrollView

21 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:layout_width="249dp"
android:layout_height="214dp"
android:layout_marginTop="167dp"
android:layout_marginBottom="100dp"
android:layout_weight="1"
android:fillViewport="true"
android:foregroundTint="#FFFFFF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.437"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04">
<TableLayout
android:id="@+id/tbl_statistics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingTop="20dp"
android:paddingRight="20dp"
android:paddingBottom="20dp"
android:stretchColumns="*">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Details"
android:textColor="#009688" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="#03A9F4">
<TextView
android:id="@+id/textView21"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9C27B0"
android:text="Student ID"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9C27B0"
android:text="Name"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/textView19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#9C27B0"
android:text="Marks"
android:textColor="#FFFFFF" />
</TableRow>
<TableRow
android:layout_width="match_parent"

22 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:layout_height="match_parent">
<TextView
android:id="@+id/textView18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="30dp"
android:text="1" />
<TextView
android:id="@+id/textView17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vedant" />
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="23" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="30dp"
android:text="2" />
<TextView
android:id="@+id/textView14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mansi" />
<TextView
android:id="@+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="30dp"
android:text="3" />
<TextView
android:id="@+id/textView11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Alisha" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="24" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">

23 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="30dp"
android:text="4" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rohini" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="25" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="30dp"
android:text="5" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pooja" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="18" />
</TableRow>
</TableLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

24 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

25 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

e) Perform on Relative layout

Code:

package com.example.practical2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity5 extends AppCompatActivity {

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

Design:

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


<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Left Button"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top Right Button"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Left Button"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bottom Right Button"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>
<Button
android:id="@+id/button5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Middle Button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>

26 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

</RelativeLayout>

Output:

27 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

f) Perform on Frame Layout

Code:

package com.example.practical2;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity6 extends AppCompatActivity{


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
}
}

Design:

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


<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">

<ImageView
android:id="@+id/imageView2"
android:layout_width="476dp"
android:layout_height="486dp"
app:srcCompat="@drawable/download"
tools:srcCompat="@drawable/images" />

<TextView
android:id="@+id/txtvw1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#286F24"
android:padding="10dp"
android:text="Login Details"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_marginLeft="100dp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:background="#ECEEE8"
android:padding="10dp"
android:hint="Enter your email" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"

28 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:background="#ECEEE8"
android:padding="10dp"
android:hint="Enter password"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= "Submit"
android:layout_marginTop="240dp"
android:layout_marginLeft="110dp"/>
</FrameLayout>

Output:

29 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Practical 3: Android program based on Intents - Login application.

Code:

Login Page:
package com.example.practical2;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity7 extends AppCompatActivity {

Button mLogin;
EditText mUsername, mPassword;

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

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


mUsername = (EditText) findViewById(R.id.etusername);
mPassword = (EditText) findViewById(R.id.etpassword);

mLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//String u = mUsername.getText().toString();
//String p = mPassword.getText().toString();
if (mUsername.getText().toString().equals("Vedant")
&& mPassword.getText().toString().equals("123")) {
String str = "Username = " +
mUsername.getText().toString();
Intent intent = new Intent(MainActivity7.this,
MainActivity8.class);
intent.putExtra("username", str);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity7.this,"Invalid
credentials",Toast.LENGTH_SHORT).show();
}
}
});
}
}

Redirect Page
package com.example.practical2;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

30 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity8 extends AppCompatActivity {

TextView t1;

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

t1 = (TextView) findViewById(R.id.s_username);

Intent intent = getIntent();

String user = intent.getStringExtra("username");


t1.setTextColor(Color.GREEN);
t1.setText(user);
}
}

Design Page for login:

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#00CC99"

tools:context="com.example.practical2.MainActivity7">

<EditText
android:id="@+id/etusername"
android:hint="Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:padding="8dp"
android:background="#fff"
android:layout_marginTop="92dp"
android:layout_alignParentTop="true" />

<EditText
android:id="@+id/etpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginRight="18dp"
android:padding="8dp"
android:background="#fff"
android:hint="Password"
android:inputType="textPassword"
android:layout_marginTop="14dp"

31 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:layout_below="@+id/etusername"
android:layout_alignParentStart="true" />

<Button
android:id="@+id/b1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:textColor="#00CC99"
android:layout_marginTop="17dp"
android:layout_below="@+id/etpassword"
android:layout_alignStart="@+id/etpassword"
android:layout_alignEnd="@+id/etpassword" />

</RelativeLayout>

Design Page for Redirect:

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.practical2.MainActivity8">

<TextView
android:layout_width="match_parent"
android:layout_height="50dp"

32 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:id="@+id/s_username"
android:text=""
android:textColor="#0f0"
android:gravity="center"
android:textSize="18sp"
android:layout_centerVertical="true"
android:layout_alignParentStart="true"
android:layout_marginStart="14dp" />

</RelativeLayout>

33 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output: For Invalid Login and Valid Login

With successful login will direct to next intent.

34 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Practical 5 : Android program to perform CRUD operation using SQLite :Database application
using SQLite

Code: Database.java

package com.example.practical2;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Database extends AppCompatActivity {
DatabaseHelper myDb;
EditText etId,etName,etSurname,etMarks;
Button
btnAdd,btnClear,btnView,btnDelete,btnEdit,btnUpdate,btnSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main9);
myDb=new DatabaseHelper(this);
etId=findViewById(R.id.et_id);
etName=findViewById(R.id.et_name);
etSurname=findViewById(R.id.et_surname);
etMarks=findViewById(R.id.et_marks);
btnAdd=findViewById(R.id.btn_add);
btnClear=findViewById(R.id.btn_clear);
btnView=findViewById(R.id.btn_view);
btnDelete=findViewById(R.id.btn_delete);
btnEdit=findViewById(R.id.btn_edit);
btnUpdate=findViewById(R.id.btn_update);
btnSearch=findViewById(R.id.btn_search);
btnAdd.setOnClickListener(view -> {

if(myDb.insertData(etName.getText().toString(),etSurname.getText().to
String
(),etMarks.getText().toString()))
Toast.makeText(this, "Data inserted successfully..",
Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "Failed to insert data..",
Toast.LENGTH_SHORT).show();
});
btnClear.setOnClickListener(view -> {
etId.getText().clear();
etName.getText().clear();
etSurname.getText().clear();
etMarks.getText().clear();
});
btnView.setOnClickListener(view -> {
Cursor res= myDb.getAllStudents();
if(res.getCount()==0){
Toast.makeText(Database.this, "No Entry Exists",
Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("ID :"+res.getString(0)+"\n");

35 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

buffer.append("Name :"+res.getString(1)+"\n");
buffer.append("Surname :"+res.getString(2)+"\n");
buffer.append("Marks :"+res.getString(3)+"\n\n");
}
AlertDialog.Builder builder = new
AlertDialog.Builder(Database.this);
builder.setCancelable(true);
builder.setTitle("User Entries");
builder.setMessage(buffer.toString());
builder.show();
});
btnDelete.setOnClickListener(view -> {
if(myDb.deleteStudent(etId.getText().toString())>0)
Toast.makeText(this, "Data deleted..",
Toast.LENGTH_SHORT).show();
});
btnSearch.setOnClickListener(view -> {
Cursor res=
myDb.getStudentById(etId.getText().toString());
if(res.getCount()==0){
Toast.makeText(Database.this, "No Entry Exists",
Toast.LENGTH_SHORT).show();
return;
}
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("ID :"+res.getString(0)+"\n");
buffer.append("Name :"+res.getString(1)+"\n");
buffer.append("Surname :"+res.getString(2)+"\n");
buffer.append("Marks :"+res.getString(3)+"\n\n");
}
AlertDialog.Builder builder = new
AlertDialog.Builder(Database.this);
builder.setCancelable(true);
builder.setTitle("User Entries");
builder.setMessage(buffer.toString());
builder.show();
});
btnEdit.setOnClickListener(view -> {
Cursor cursor =
myDb.getStudentById(etId.getText().toString());
cursor.moveToFirst();
etName.setText(cursor.getString(1));
etSurname.setText(cursor.getString(2));
etMarks.setText("" + cursor.getString(3));
});
btnUpdate.setOnClickListener(view -> {
myDb.update(etId.getText().toString(),
etName.getText().toString(),
etSurname.getText().toString(),
etMarks.getText().toString());
Toast.makeText(this, "Data updated...",
Toast.LENGTH_SHORT).show();
});
}
}

DatabaseHelper.java

36 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

package com.example.practical2;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class DatabaseHelper extends SQLiteOpenHelper {
private SQLiteDatabase sqliteDatabase;
public static final String DATABASE_NAME="Student.db";
public static final String TABLE_NAME="student_table";
public static final String COL_3="SURNAME";
public static final String COL_4="MARKS";
public DatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("create table "+TABLE_NAME+"(ID
Integer Primary key Autoincrement,NAME text,SURNAME text,MARKS
integer)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int
i1) {
sqLiteDatabase.execSQL("drop table if exists "+TABLE_NAME);
onCreate(sqLiteDatabase);
}
// method to insert data into database
public boolean insertData(String name,String surname,String
marks){
sqliteDatabase=getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("NAME",name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
long result=
sqliteDatabase.insert(TABLE_NAME,null,contentValues);
if(result==-1)
return false;
else
return true;
}
// method to return all students data
public Cursor getAllStudents()
{
sqliteDatabase=getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME;
return sqliteDatabase.rawQuery(query,null);
}
public Cursor getStudentById(String id)
{
sqliteDatabase=getReadableDatabase();
String query = "SELECT * FROM " + TABLE_NAME + " WHERE ID = "
+id;
return sqliteDatabase.rawQuery(query,null);
}
// method to update data to database
public void update(String id, String name, String surname, String
marks) {
sqliteDatabase=getWritableDatabase();

37 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

ContentValues contentValues=new ContentValues();


contentValues.put("ID",id);
contentValues.put("NAME",name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,marks);
sqliteDatabase.update(TABLE_NAME,contentValues,"ID=? ",new
String[]{id});
}
public Integer deleteStudent(String id)
{
sqliteDatabase=getWritableDatabase();
return sqliteDatabase.delete(TABLE_NAME, "ID=? ", new
String[]{id});
}
}

Activity_main9.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center_horizontal"
tools:context=".Database">
<!-- edit text for student ID -->
<EditText
android:id="@+id/et_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:hint="Student ID" />
<!-- edit text for student name -->
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:hint="Student name" />
<!-- edit text for surname -->
<EditText
android:id="@+id/et_surname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:hint="Surname" />
<!-- edit text for player's runs -->
<!-- container for add and clear buttons -->
<EditText
android:id="@+id/et_marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginVertical="8dp"
android:hint="Marks"
android:inputType="number" />
<LinearLayout
android:id="@+id/btns_add_clear"
android:layout_width="match_parent"

38 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<!-- add button -->
<!-- clear button -->
<Button
android:id="@+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#4CAF50"
android:text="Clear" />
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#4CAF50"
android:text="Add"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/btn_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#4CAF50"
android:text="View"
android:textColor="#FFFFFF" />
</LinearLayout>
<!-- container for update and cancel buttons -->
<LinearLayout
android:id="@+id/btns_update_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:orientation="horizontal"
android:gravity="center">
<!-- Edit button -->
<!-- update button -->
<Button
android:id="@+id/btn_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#2196F3"
android:text="Edit"
android:textColor="#FFFFFF" />
<!-- delete button -->
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#2196F3"
android:text="Update"
android:textColor="#FFFFFF" />
<Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"

39 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:background="#CCF44336"
android:textColor="#FFFFFF"
android:text="Delete" />
<!-- search button -->
</LinearLayout>
<Button
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:background="#CCF44336"
android:textColor="#FFFFFF"
android:text="Search" />
</LinearLayout>

Output: Entering the data in app. By clicking add button.

40 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

41 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

42 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

43 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Practical 6 : Android program using Shared Preferences, Internal and External Storage:

a) Program using file handling

Code: Mainactivity.java
package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button btnWrite, btnLoad;
TextView write;
private String FILENAME = "test.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.edit_text);
btnWrite = findViewById(R.id.btn_write);
btnLoad = findViewById(R.id.btn_load);
write=findViewById(R.id.write);
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = editText.getText().toString();
try {
FileOutputStream fos = openFileOutput(FILENAME,
MODE_PRIVATE);
fos.write(data.getBytes());
Toast.makeText(getApplicationContext(), "Data
written successfully...", Toast.LENGTH_SHORT).show();
editText.getText().clear();
fos.close();
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileInputStream fis = openFileInput(FILENAME);
InputStreamReader isr = new
InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder data = new StringBuilder();

44 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

String line;
while((line = br.readLine()) != null) {
data.append("\n").append(line);
}
data.deleteCharAt(0);
write.setText(data);
Toast.makeText(getApplicationContext(), "Data
loaded successfully...", Toast.LENGTH_SHORT).show();
fis.close();
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),
e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
getDir();
}
private void getDir()
{
StringBuilder builder=new StringBuilder();
builder.append("Cache Directories:
").append(getCacheDir().getAbsolutePath()).append("\n").append("File
Directories:").append(getFilesDir().getAbsolutePath());
write.setText(builder.toString());
}
}

AndroidManifest.xml

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


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

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

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

<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>

45 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

</application>

</manifest>

Design 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<!-- view to get and display file data -->
<EditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Enter text"
android:lineHeight="20sp"
android:textColor="@color/black" />
<!-- button to write data to file -->
<Button
android:id="@+id/btn_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write" />
<!-- button to read data from file -->
<Button
android:id="@+id/btn_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load" />
</LinearLayout>

46 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

47 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

b) Program on shared preferences

External Storage:

Code for MainActivity.java

package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainActivity1 extends AppCompatActivity {
private EditText inputText;
private Button btnWrite, btnLoad;
private TextView dir;
private String filename = "hello.txt";
private String filepath = "MyFileStorage";
private File extFile;
private String data = "";
@Override

48 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
inputText = findViewById(R.id.input_text);
btnWrite = findViewById(R.id.btn_write);
btnLoad = findViewById(R.id.btn_load);
dir=findViewById(R.id.dir);
if (!isExternalStorageAvailable() ||
isExternalStorageReadOnly()) {
btnWrite.setEnabled(false);
}
else {
extFile = new File(getExternalFilesDir(filepath),
filename);
}
getDir();
btnWrite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
data = inputText.getText().toString();
try {
FileOutputStream fos = new
FileOutputStream(extFile);
fos.write(data.getBytes());
// fos.write("Hello".getBytes());
inputText.getText().clear();
Toast.makeText(getApplicationContext(), filename
+ " saved to external storage...", Toast.LENGTH_SHORT).show();
fos.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try {
FileInputStream fis = new
FileInputStream(extFile);
InputStreamReader isr = new
InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuilder data = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
data.append("\n").append(line);
}
inputText.setText(data);
Toast.makeText(getApplicationContext(), "Data
Retrieved from External File Successfully...",
Toast.LENGTH_SHORT).show();
fis.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
});
}

49 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

private static boolean isExternalStorageAvailable() {


String extStorageState =
Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(extStorageState);
}
private static boolean isExternalStorageReadOnly() {
String extStorageState =
Environment.getExternalStorageState();
return
Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState);
}
private void getDir()
{
StringBuilder builder=new StringBuilder();
builder.append("External File Directories:
").append(getExternalFilesDir(filepath).getAbsolutePath()).append("\n
");
dir.setText(builder.toString());
}
}

Design Code : activity_main1.xml

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity1">
<!-- view to get and display file data -->
<TextView
android:id="@+id/dir"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:id="@+id/input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Enter text"
android:lineHeight="25sp"
android:textColor="@color/black" />
<!-- button to write data to file -->
<Button
android:id="@+id/btn_write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Write" />
<!-- button to read data from file -->
<Button
android:id="@+id/btn_load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load" />
</LinearLayout>

50 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

51 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Shared Preferences:

Code for MainActivity2.java

package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity2 extends AppCompatActivity {
EditText edit;
Button save, clear, get;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
edit = findViewById(R.id.e1);
save = findViewById(R.id.b1);
clear = findViewById(R.id.b2);
get = findViewById(R.id.b3);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = edit.getText().toString();
SharedPreferences sp = getSharedPreferences("Data",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();

52 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

editor.putString("Text", text);
editor.commit();
Toast.makeText(getApplicationContext(), "Successfully
saved", Toast.LENGTH_SHORT).show();
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
edit.setText(" ");
}
});
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences sp = getSharedPreferences("Data",
Context.MODE_PRIVATE);
edit.setText(sp.getString("Text", null));
Toast.makeText(getApplicationContext(), "Data
Retrieved Successfully", Toast.LENGTH_SHORT).show();
}
});
}
}

Design Code for activity_main2.xml

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity2">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:id="@+id/e1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:layout_below="@+id/e1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:id="@+id/b1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLEAR"
android:layout_below="@+id/b1"
android:id="@+id/b2"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
<Button
android:layout_width="wrap_content"

53 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

android:layout_height="wrap_content"
android:id="@+id/b3"
android:layout_below="@+id/b2"
android:text="GET"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>
</RelativeLayout>

54 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

Data gets cleared when we click on clear button and can be seen in above screenshot.

55 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

56 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Practical 7: Android program to work with graphics and animation.

i. Fading

ii. Sliding

iii. Rotating

Code for MainActivity3.java:


package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity3 extends AppCompatActivity {
ImageView image;
Button fade, zoom, rotate;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
image = findViewById(R.id.iv);
fade = findViewById(R.id.b1);
zoom = findViewById(R.id.b2);
rotate = findViewById(R.id.b3);
fade.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation anim_fade =

AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
image.startAnimation(anim_fade);
}
});
zoom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation anim_zoom =

AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom);
image.startAnimation(anim_zoom);
}
});
rotate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation anim_rotate =

AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
image.startAnimation(anim_rotate);
}
});

57 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

}
}

Design Code : activity_main3.xml

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginLeft="30dp"
android:layout_marginTop="50dp"
android:layout_marginRight="30dp"
android:src="@drawable/images" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="fade"
android:layout_below="@id/iv"
android:layout_marginLeft="25dp"
android:layout_marginTop="50dp" android:id="@+id/b1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="zoom"
android:layout_centerHorizontal="true"
android:layout_below="@id/iv"
android:layout_marginLeft="25dp"
android:layout_marginTop="50dp"
android:id="@+id/b2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="rotate"
android:layout_alignParentRight="true"
android:layout_below="@id/iv"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_marginTop="50dp" android:id="@+id/b3"/>
</RelativeLayout>

To create animation folder -> right click on res folder -> Android Resource Directory -> In
Resource type (anim).

58 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Codes for anim xml files:

Fade.xml

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


<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1" android:toAlpha="0"
android:duration="1000"
android:repeatMode="reverse" android:repeatCount="2"
/>
</set>

Rotate.xml

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


<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:toDegrees="360" android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%" android:duration="1000"
android:repeatMode="reverse"
android:repeatCount="2"
/>
</set>

Zoom.xml

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


<set xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:ignore="ExtraText">
//zoom in
<scale
android:fromXScale="0.2" android:fromYScale="0.2"
android:toXScale="5.0" android:toYScale="5.0"
android:pivotX="50%"
android:pivotY="50%" android:duration="1000"
android:repeatMode="restart"
android:repeatCount="2"
/>
//zoom out
//fromscale="5.0"
//toxscale="0.2"
</set>

Design View of XML file:

59 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

60 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Frame by Frame Animation:

Code for MainActivity4.java

package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity4 extends AppCompatActivity {
ImageView img;
Button btnStartStop;
AnimationDrawable animation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
img = findViewById(R.id.img);
btnStartStop = findViewById(R.id.btn_start_stop);
// fetching animation drawable from image view
animation = (AnimationDrawable) img.getDrawable();
// starting and stopping animation on button click
btnStartStop.setOnClickListener(view -> {
// if running then stop
if (animation.isRunning()) {
animation.stop();
btnStartStop.setText("Start");
return;
}
// else start animation

61 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

animation.start();
btnStartStop.setText("Stop");
});
}
}

Running.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/one" android:duration="100" />
<item android:drawable="@drawable/two" android:duration="100" />
<item android:drawable="@drawable/three" android:duration="100" />
<item android:drawable="@drawable/four" android:duration="100" />
</animation-list>

Design Code for activity_main4.xml

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity4">
<!-- image view to hold animation frame images -->
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/running" />
<!-- button to start or pause animation -->
<Button
android:id="@+id/btn_start_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start" />
</LinearLayout>

62 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Output:

63 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Practical 8: Android program to work with images and videos.

Code for audio player:

package com.example.practical3;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.View;
import android.widget.Button;
public class MainActivity5 extends AppCompatActivity {
Button play, pause, stop;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main5);
play = findViewById(R.id.btnPlay);
pause = findViewById(R.id.btnPause);
stop = findViewById(R.id.btnStop);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp == null) {
mp = MediaPlayer.create(getApplicationContext(),
R.raw.song);
}
mp.start();
}
});
pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null) {
mp.pause();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null) {
mp.release();
mp = null;
}
}
});
}
}

activity_main5.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:background="@drawable/ic_launcher_background"

64 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

tools:context=".MainActivity5">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="#C6A8A8"
android:text="Play"
android:textColor="#01579B" />
<Button
android:id="@+id/btnPause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="#C6A8A8"
android:text="Pause"
android:textColor="#01579B" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:backgroundTint="#C6A8A8"
android:text="Stop"
android:textColor="#01579B" />
</LinearLayout>
</RelativeLayout>

65 | P a g e
C21053 Mobile APP Lab 1st Dec,2022

Right click on res folder -> Android Resource Directory -> resource type(raw) -> song.mp3

Output: Song would be played. When we click on play button. We can stop or pause the song
too. The song which is played is Taylor Swift song.

66 | P a g e

You might also like