Final Mad File
Final Mad File
Final Mad File
BCA-318
BCA VI(E1)
INDEX
OUTPUT:
PRACTICAL 3:
CREATE AN ANDROID APP TO DISPLAY VARIOUS ANDROID LIFECYCLE PHASES.
CODE:
DESIGN (ACITIVITY_MAIN.XML)
LOGIC (MainActivity.java)
package com.example.myapplication2lifecycle;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView textLifecycle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLifecycle = findViewById(R.id.textLifecycle);
updateLifecycleState("onCreate");
Log.d("lifecycle", "onCreate() method executed"); }
@Override
protected void onStart() {
super.onStart();
updateLifecycleState("onStart");
Log.d("lifecycle", "onStart() method executed");}
@Override
protected void onResume() {
super.onResume();
updateLifecycleState("onResume");
Log.d("lifecycle", "onResume() method executed");
}
@Override
protected void onPause() {
super.onPause();
updateLifecycleState("onPause");
Log.d("lifecycle", "onPause() method executed");
}
@Override
protected void onStop() {
super.onStop();
updateLifecycleState("onStop");
Log.d("lifecycle", "onStop() method executed");
}
@Override
protected void onDestroy() {
super.onDestroy();
updateLifecycleState("onDestroy");
Log.d("lifecycle", "onDestroy() method executed");
}
@Override
protected void onRestart() {
super.onRestart();
updateLifecycleState("onRestart");
Log.d("lifecycle", "onRestart() method executed");
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
updateLifecycleState("onAttachedToWindow");
Log.d("lifecycle", "onAttachedToWindow() method executed");
}
@Override
public void onDetachedFromWindow() {
super.onDetachedFromWindow();
updateLifecycleState("onDetachedFromWindow");
Log.d("lifecycle", "onDetachedFromWindow() method executed");
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
updateLifecycleState("onSaveInstanceState");
Log.d("lifecycle", "onSaveInstanceState() method executed");
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
updateLifecycleState("onRestoreInstanceState");
Log.d("lifecycle", "onRestoreInstanceState() method executed");
}
@Override
public void finish() {
// Call finish() to ensure onDestroy() is called before the
activity is finished.
updateLifecycleState("onDestroy (called by finish())");
Log.d("lifecycle", "onDestroy (called by finish()) method
executed");
super.finish();
}
(DESIGN)
(ACTIVITY_MAIN.JAVA)
LOGIC (activity_main.xml)
package com.example.myapplication3calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
findViewById(R.id.buttonAdd).setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(num1EditText1.getText().toString());
double num2 = Double.parseDouble(num2EditText2.getText().toString());
double result = num1 + num2;
texViewResult.setText(String.valueOf(result));}});
findViewById(R.id.buttonSubtract).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(num1EditText1.getText().toString());
double num2 = Double.parseDouble(num2EditText2.getText().toString());
double result = num1 - num2;
texViewResult.setText(String.valueOf(result));}});
findViewById(R.id.buttonMultiply).setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(num1EditText1.getText().toString());
double num2 = Double.parseDouble(num2EditText2.getText().toString());
double result = num1 * num2;
texViewResult.setText(String.valueOf(result)); }});
findViewById(R.id.buttonDivide).setOnClickListener(new
View.OnClickListener() {
@SuppressLint("SetTextI18n")
@Override
public void onClick(View v) {
double num1 = Double.parseDouble(num1EditText1.getText().toString());
double num2 = Double.parseDouble(num2EditText2.getText().toString());
if (num2 == 0) {
texViewResult.setText("Cannot divide by zero");}else {
double result = num1 / num2;
texViewResult.setText(String.valueOf(result));}}});}}
OUTPUT:
LOGIC : MainActivity.java
package com.example.myapplication5;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private EditText editTextAmount;
private Spinner SpinnerFrom, SpinnerTo;
private TextView textViewResult;
private final String[] currencies = {"USD", "EUR", "DHR", "INR", "PESO"};
// Add more currencies
private final double[][] conversations = {
{1.0, 0.85, 0.73, 74.87, 112.61},
{1.18, 1.0, 0.86, 86.66, 133.11},
{1.37, 1.16, 1.0, 102.33, 153.74},
{0.013, 0.011, 0.0096, 1.0, 1.5},
{0.0089, 0.0075, 0.0065, 0.088, 1.0} };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAmount = findViewById(R.id.editTextAmount);
SpinnerFrom = findViewById(R.id.SpinnerFrom);
SpinnerTo = findViewById(R.id.spinnerTo);
Button btn1 = findViewById(R.id.btn1);
textViewResult = findViewById(R.id.textViewResult);
SpinnerFrom.setAdapter(adapter);
SpinnerTo.setAdapter(adapter);
SpinnerFrom.setSelection(0); // set default selection
SpinnerTo.setSelection(1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
convertCurrency();
}
@SuppressLint("SetTextI18n")
private void convertCurrency() {
try {
double amount = Double.parseDouble(editTextAmount.getText().toString());
int fromIndex = SpinnerFrom.getSelectedItemPosition();
int toIndex = SpinnerTo.getSelectedItemPosition();double result = amount *
conversations[fromIndex][toIndex];
DecimalFormat df = new DecimalFormat("#.##");
String formattedResult = df.format(result);
textViewResult.setText(amount + " " + currencies[fromIndex] + " = " +
formattedResult + " " + currencies[toIndex]);}
catch (NumberFormatException e){
textViewResult.setText("Invalid Input");}}});}}
OUTPUT:
Practical 6
Write an Android application to create custom toast
CODE:
DESIGN(ACITIVITY_MAIN.XML)
LOGIC(MainActivity.java)
package com.example.customtoast;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onStart() {
super.onStart();
showCustomToast("onStart()");
@Override
protected void onRestart() {
super.onRestart();
showCustomToast("onRestart()");
}
@Override
protected void onResume() {
super.onResume();
showCustomToast("onResume()");
}
@Override
protected void onPause() {
super.onPause();
showCustomToast("onPause()");
}
@Override
protected void onStop() {
super.onStop();
showCustomToast("onStop()");
}
@Override
protected void onDestroy() {
super.onDestroy();
showCustomToast("onDestroy()");
}
DESIGN: ANDROIDMANIFEST.XML
ACTIVITY_MAIN.XML
package com.example.nativapplication;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
Button IMAGE;
ImageView view;
ActivityResultLauncher<Intent> mStartForResult =
registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result) {
Toast.makeText(getApplicationContext(),result.toString(),Toast.LENGTH_LONG)
.show();
if (result.getResultCode() == Activity.RESULT_OK) {
Intent intent = result.getData();
Bundle data=intent.getExtras();
Bitmap bitmap= (Bitmap) data.get("data");
view.setImageBitmap(bitmap);
}
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IMAGE=findViewById(R.id.btn);
view=findViewById(R.id.imageview);
IMAGE.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mStartForResult.launch(intent);
}
});
}
}
OUTPUT:
PRACTICAL 8
CREATE AN APPLICATION TO PICK UP ANY IMAGE FROM THE NATIVE APPLICATION
GALLERY AND DISPLAY IT ON THE SCREEN
CODE :
DESIGN: 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"
tools:context=".MainActivity">
<com.example.customview.MyCustomView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cutom_view"/>
</RelativeLayout>
LOGIC :MainActivity.java
package com.example.customview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}}
package com.example.customview;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class MyCustomView extends View {
private Paint paint;
public MyCustomView(Context context) {
super(context);
init();}
public MyCustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();}
private void init()
{ paint=new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);}
@Override
protected void onDraw(@NonNull Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(150,150,getWidth()-120,getHeight()-120,paint);
}}
PRACTICAL-9.
CREATE AN ANDROID APPLICATION TO PERFORM OPERATIONS OF CREATE ,
INSERT, DELETE VIEW AND UPDATE, USING SQLITE DATABASE.
CODE:
DESIGN: ACITIVITY_MAIN.XML
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayData();
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = Name1.getText().toString().trim();
String age = Age1.getText().toString().trim();
if (id != -1){
Toast.makeText(MainActivity.this, "Data inserted
Successfully", Toast.LENGTH_LONG).show();
Name1.setText("");
Age1.setText("");
}
else {
Toast.makeText(MainActivity.this, "Failed to insert
Data", Toast.LENGTH_LONG).show();
}
}
else {
Toast.makeText(MainActivity.this, "Please enter name
and age", Toast.LENGTH_LONG).show();
}
}
});
buttonUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Placeholder for update operation
Toast.makeText(MainActivity.this, "Update operation
clicked", Toast.LENGTH_LONG).show();
}
});
Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Placeholder for delete operation
Toast.makeText(MainActivity.this, "Delete operation
clicked", Toast.LENGTH_LONG).show();
}
});
listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
String selectedItem = arrayList.get(position);
String[] parts = selectedItem.split(": ");
String personId = parts[0];
if (rowsDeleted > 0) {
Toast.makeText(MainActivity.this, "Data deleted
successfully", Toast.LENGTH_LONG).show();
displayData();
}
else {
Toast.makeText(MainActivity.this, "Failed to delete
data", Toast.LENGTH_LONG).show();
}
}
});
}
private void displayData(){
arrayList.clear();
@SuppressLint("Recycle") Cursor cursor =
sqLiteDatabase.rawQuery("SELECT * FROM Persons", null);
if (cursor.moveToFirst()){
do {
@SuppressLint("Range") String name =
cursor.getString(cursor.getColumnIndex("Name"));
@SuppressLint("Range") String age =
cursor.getString(cursor.getColumnIndex("Age"));
} while (cursor.moveToNext());
}
arrayAdapter.notifyDataSetChanged();
cursor.close();
}
DBHelper.java
package com.example.crud;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE Persons (Id INTEGER PRIMARY KEY
AUTOINCREMENT, Name TEXT, Age TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
db.execSQL("DROP TABLE IF EXISTS Persons");
onCreate(db);
}
}
OUTPUT
PRACTICAL-10
READ PHONEBOOK CONTACTS USING CONTENT PROVIDERS AND DISPLAY IT IN LIST.
CODE:
DESIGN:Activity_layout.xml
LOGIC : MainActivity.java
package com.example.contact;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
} }
OUTPUT:
PRACTICAL11.
CREATE AN APPLICATION THAT USES CHECKBOX FOR CONSTRUCTION OF A
SHOPPING LIST SO THE USER CAN CHECK OFF ITEMS AS THEY ARE PICKED UP.
THE CHECKED ITEMS SHOULD BE DISPLAYED IN A TEXTVIEW CONTROL.
CODE:
DESIGN
package com.example.check;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
checkBox4 = findViewById(R.id.checkBox4);
checkBox5 = findViewById(R.id.checkBox5);
textView1 = findViewById(R.id.textView1);
checkBox2.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
updateTextView();
}
});
checkBox3.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
updateTextView();
}
});
checkBox4.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
updateTextView();
}
});
checkBox5.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean
isChecked) {
updateTextView();
}
});
if (checkBox1.isChecked()){
stringBuilder.append("Grapes \n");
}
if (checkBox2.isChecked()){
stringBuilder.append("Guava \n");
}
if (checkBox3.isChecked()){
stringBuilder.append("watermelon \n");
}
if (checkBox4.isChecked()){
stringBuilder.append("Apple \n");
}
if (checkBox5.isChecked()){
stringBuilder.append("Orange \n");
}
textView1.setText(stringBuilder.toString());
}
}
OUTPUT
PRACTICAL-12
Write an android application to parse JSON and show into Recycler view
CODE:
MainActivity.java
package com.example.recyclerviewdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int
viewType) {
View view=
LayoutInflater.from(context).inflate(R.layout.item_custom_list,parent,false
);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int
position) {
holder.nm.setText(name.get(position));
holder.cl.setText(classes.get(position));
}
@Override
public int getItemCount() {
return name.size();
}
cutom_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardBackgroundColor="@color/meng"
app:cardCornerRadius="5mm"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Name"
android:textSize="25dp"
/>
<TextView
android:id="@+id/class_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Class"
android:textSize="25dp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
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"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
OUTPUT: