Android Studio Practical Code for Students

PRACTICAL-1

Create an android application that display the message “Welcome to Graphic Era University MCA” on the screen.

<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/welcomeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Graphic Era University MCA"
        android:textSize="24sp"
        android:textColor="@android:color/black"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


// src/main/java/com/example/myapplication/MainActivity.java

package com.example.myapplication;
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);
    }
}

PRACTICAL-2

Q2. Create an Android application that display the message “Welcome to Graphic Era University – MCA” at the click of a button.

// src/main/java/com/example/myapplication/MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    private Button button;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initialize button and textView
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
        // Set a click listener for the button
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Make the TextView visible and set the message
                textView.setVisibility(View.VISIBLE);
                textView.setText("Welcome to Graphic Era University-MCA");
            }
        });
    }
}

PRACTICAL-3

Q3. Create an Android application to display the message “Welcome to GEU-Learning android application development -MCA” and execute the application using different emulators.

<!-- res/layout/activity_main.xml -->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/welcomeText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to GEU-Learning android application development -MCA"
        android:textSize="22sp"
        android:textColor="@android:color/black"
        android:gravity="center"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
// src/main/java/com/example/geuwelcomeapp/MainActivity.java
package com.example.geuwelcomeapp;
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);
    }
}

Q4. Illustrate with a suitable example the use of Toast to display a message in an Android Application. { The message display should wait for a long time.)

<!-- res/layout/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">
    <Button
        android:id="@+id/showToastButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Toast"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

// src/main/java/com/example/toastexampleapp/MainActivity.java
package com.example.toastexampleapp;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Find the button by its ID
        Button showToastButton = findViewById(R.id.showToastButton);
        // Set an OnClickListener to the button
        showToastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Show a Toast message
                Toast.makeText(MainActivity.this,
                               "Welcome to GEU-Learning android application development -MCA",
                               Toast.LENGTH_LONG).show();
            }
        });
    }
}

Q5. Create an Android application for designing a simple calculator having basic functionality like Addition, Subtraction, Multiplication and Division using controls like Buttons, TextViews, EditTexts, etc.

<!-- res/layout/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">
    <!-- EditText for first number -->
    <EditText
        android:id="@+id/editTextNumber1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter First Number"
        android:inputType="numberDecimal"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="30dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"/>
    <!-- EditText for second number -->
    <EditText
        android:id="@+id/editTextNumber2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter Second Number"
        android:inputType="numberDecimal"
        app:layout_constraintTop_toBottomOf="@id/editTextNumber1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:layout_marginEnd="20dp"/>
    <!-- Buttons for the four operations -->
    <Button
        android:id="@+id/btnAdd"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Add"
        app:layout_constraintTop_toBottomOf="@id/editTextNumber2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/btnSubtract"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btnSubtract"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Subtract"
        app:layout_constraintTop_toBottomOf="@id/editTextNumber2"
        app:layout_constraintStart_toEndOf="@id/btnAdd"
        app:layout_constraintEnd_toStartOf="@id/btnMultiply"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btnMultiply"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Multiply"
        app:layout_constraintTop_toBottomOf="@id/editTextNumber2"
        app:layout_constraintStart_toEndOf="@id/btnSubtract"
        app:layout_constraintEnd_toStartOf="@id/btnDivide"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="10dp"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btnDivide"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Divide"
        app:layout_constraintTop_toBottomOf="@id/editTextNumber2"
        app:layout_constraintStart_toEndOf="@id/btnMultiply"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="20dp"
        android:layout_marginStart="10dp"
        android:layout_weight="1"/>
    <!-- TextView to display the result -->
    <TextView
        android:id="@+id/resultTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result:"
        android:textSize="22sp"
        android:layout_marginTop="30dp"
        app:layout_constraintTop_toBottomOf="@id/btnAdd"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
// src/main/java/com/example/simplecalculatorapp/MainActivity.java
package com.example.simplecalculatorapp;
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 androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    private EditText editTextNumber1, editTextNumber2;
    private Button btnAdd, btnSubtract, btnMultiply, btnDivide;
    private TextView resultTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initialize the UI components
        editTextNumber1 = findViewById(R.id.editTextNumber1);
        editTextNumber2 = findViewById(R.id.editTextNumber2);
        btnAdd = findViewById(R.id.btnAdd);
        btnSubtract = findViewById(R.id.btnSubtract);
        btnMultiply = findViewById(R.id.btnMultiply);
        btnDivide = findViewById(R.id.btnDivide);
        resultTextView = findViewById(R.id.resultTextView);
        // Set click listeners for each button
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate('+');
            }
        });
        btnSubtract.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate('-');
            }
        });
        btnMultiply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate('*');
            }
        });
        btnDivide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calculate('/');
            }
        });
    }
    // Method to perform the calculation
    private void calculate(char operator) {
        String num1 = editTextNumber1.getText().toString();
        String num2 = editTextNumber2.getText().toString();
        // Check if the input fields are not empty
        if (num1.isEmpty() || num2.isEmpty()) {
            Toast.makeText(this, "Please enter both numbers", Toast.LENGTH_SHORT).show();
            return;
        }
        // Convert the input to double
        double number1 = Double.parseDouble(num1);
        double number2 = Double.parseDouble(num2);
        double result = 0;
        // Perform the operation based on the operator
        switch (operator) {
            case '+':
                result = number1 + number2;
                break;
            case '-':
                result = number1 - number2;
                break;
            case '*':
                result = number1 * number2;
                break;
            case '/':
                if (number2 != 0) {
                    result = number1 / number2;
                } else {
                    Toast.makeText(this, "Cannot divide by zero", Toast.LENGTH_SHORT).show();
                    return;
                }
                break;
        }
        // Display the result
        resultTextView.setText("Result: " + result);
    }
}

Q6. Illustrate with a suitable example the use of Intents in linking activities. At least two activities are to be used.

<!-- res/layout/activity
    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/buttonGoToSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Go to Second Activity"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
// src/main/java/com/example/intentexampleapp/MainActivity.java
package com.example.intentexampleapp;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Find the button
        Button buttonGoToSecond = findViewById(R.id.buttonGoToSecond);
        // Set an onClickListener on the button to navigate to SecondActivity
        buttonGoToSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Create an Intent to navigate to SecondActivity
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);  // Start the second activity
            }
        });
    }
}
<!-- res/layout/activity_second.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=".SecondActivity">
    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Welcome to Second Activity"
        android:textSize="24sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
// src/main/java/com/example/intentexampleapp/SecondActivity.java
package com.example.intentexampleapp;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}
<!-- AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.intentexampleapp">
    <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.IntentExampleApp">
        <activity android:name=".SecondActivity" />
        <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>

Ques 7. Activity of Food Delivery

// Activity 1
package com.example.apnaapp;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });
        Toast.makeText(this,"Please select the menu",Toast.LENGTH_LONG).show();
    }
}
<?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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#73BEFA"
    tools:context=".MainActivity2">

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="209dp"
        android:layout_height="51dp"
        android:text="PIZZA"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        app:layout_constraintVertical_bias="0.0" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="218dp"
        android:layout_height="50dp"
        android:text="COFFE"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox3"
        app:layout_constraintVertical_bias="0.026" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="224dp"
        android:layout_height="67dp"
        android:layout_marginTop="28dp"
        android:text="BURGER"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/checkBox1"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/button2"
        android:layout_width="168dp"
        android:layout_height="58dp"
        android:text="PAISA DO"
        android:textSize="16sp"
        tools:layout_editor_absoluteX="121dp"
        tools:layout_editor_absoluteY="450dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="268dp"
        android:layout_height="72dp"
        android:text="    MENU"
        android:textColor="#673AB7"
        android:textColorHint="#602DBA"
        android:textColorLink="#673AB7"
        android:textSize="60sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.559"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.024" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="252dp"
        android:layout_height="47dp"
        android:layout_marginBottom="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.503"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:layout_constraintVertical_bias="0.138" />
</androidx.constraintlayout.widget.ConstraintLayout>
//  Activity 2
package com.example.apnaapp;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity2 extends AppCompatActivity {
    private Button button2;
    private CheckBox c1,c2,c3;
    private TextView textView;
    StringBuilder result=new StringBuilder();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main2);
        c1= (CheckBox)findViewById(R.id.checkBox1);
        c2= (CheckBox)findViewById(R.id.checkBox2);
        c3= (CheckBox)findViewById(R.id.checkBox3);
        textView=(TextView)findViewById(R.id.textView3);
        button2=(Button)findViewById(R.id.button2);

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int total=0;

                if(c1.isChecked()){
                    result.append("Pizza is of 300");
                    total=total+300;
                }
                if(c2.isChecked()){
                    result.append("Coffe is of 100");
                    total=total+100;
                }
                if(c3.isChecked()){
                    result.append("Burger is of 350");
                    total=total+350;
                }

                textView.setText("PAYMENT = "+total);
            }

        });
        Toast.makeText(this,result.toString(),Toast.LENGTH_LONG).show();
    }
}
<?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/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#6CBDFD"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="381dp"
        android:layout_height="105dp"
        android:text="Hello World!"
        android:textColor="#673AB7"
        android:textSize="48sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.833"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.306"
        tools:text="Apna Chaat Wala" />

    <Button
        android:id="@+id/button"
        android:layout_width="274dp"
        android:layout_height="60dp"
        android:text="ABHI ORDER KARE"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.468"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Practical 9: With the help of SQLite database using android studio make a form

package com.example.a29112024android;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import java.sql.SQLData;
public class DataBaseHelper extends SQLiteOpenHelper {

    // Database name and table name
    public static final String DB_NAME = "gehu.db";
    public static final String TABLE_NAME = "student";
    public static final String COL1 = "name";
    public static final String COL2 = "pass";

    // Constructor to initialize the database helper
    public DataBaseHelper(Context context) {
        super(context, DB_NAME, null, 1);  // Initialize the SQLiteOpenHelper
    }

    // Called when the database is created for the first time
    @Override
    public void onCreate(SQLiteDatabase db) {
        // SQL query to create the table
        String createTableQuery = "CREATE TABLE " + TABLE_NAME + " (" + COL1 + " TEXT, " + COL2 + " TEXT)";
        db.execSQL(createTableQuery);
    }

    // Called when the database is updated
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop the table if it exists and recreate it
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);  // Recreate the table
    }

    // Method to insert data into the database
    public boolean insertData(String name, String pass) {
        SQLiteDatabase db = this.getWritableDatabase();  // Open the database in writable mode
        ContentValues contentValues = new ContentValues();  // ContentValues to store data

        // Put the data into the contentValues object
        contentValues.put(COL1, name);
        contentValues.put(COL2, pass);

        // Insert data into the database
        long result = db.insert(TABLE_NAME, null, contentValues);

        // If insertion fails, return false
        return result != -1;
    }
}