0% found this document useful (0 votes)
28 views17 pages

Lab Content Prov, Intenal Sto

The document is a lab manual providing sample projects for Android development, focusing on Content Providers, Internal Storage, External Storage, and SQLite databases. Each project includes Java code snippets for creating and managing data, along with XML layouts for user interfaces. Students are encouraged to practice by implementing these examples and exploring additional questions related to the projects.

Uploaded by

menber988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views17 pages

Lab Content Prov, Intenal Sto

The document is a lab manual providing sample projects for Android development, focusing on Content Providers, Internal Storage, External Storage, and SQLite databases. Each project includes Java code snippets for creating and managing data, along with XML layouts for user interfaces. Students are encouraged to practice by implementing these examples and exploring additional questions related to the projects.

Uploaded by

menber988
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

MAD- lab Manual

Dear student; Please do those sample projects and lastly practice yourself by doing
some questions that looks them or something different.

Sample projects for

 Content Provider
 Internal Storage
 External Storage
 SQLite

Project 1: Content Provider

// DBHelper.java

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Students.db";

public static final String TABLE_NAME = "students";

public DBHelper(Context context) {

super(context, DATABASE_NAME, null, 1);

@Override

public void onCreate(SQLiteDatabase db) {

1
db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY
AUTOINCREMENT, name TEXT)");

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

onCreate(db);

// StudentProvider.java

public class StudentProvider extends ContentProvider {

static final String PROVIDER_NAME =


"com.example.contentproviderdemo.StudentProvider";

static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/students");

SQLiteDatabase db;

@Override

public boolean onCreate() {

DBHelper helper = new DBHelper(getContext());

db = helper.getWritableDatabase();

return (db != null);

2
}

@Override

public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String
sortOrder) {

return db.query(DBHelper.TABLE_NAME, projection, selection, selectionArgs, null, null,


sortOrder);

@Override

public Uri insert(Uri uri, ContentValues values) {

long rowID = db.insert(DBHelper.TABLE_NAME, null, values);

return ContentUris.withAppendedId(CONTENT_URI, rowID);

@Override

public int delete(Uri uri, String selection, String[] selectionArgs) {

return db.delete(DBHelper.TABLE_NAME, selection, selectionArgs);

@Override

public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {

return db.update(DBHelper.TABLE_NAME, values, selection, selectionArgs);

3
}

@Override

public String getType(Uri uri) {

return null;

// AndroidManifest.xml (partial)

<provider

android:name=".StudentProvider"

android:authorities="com.example.contentproviderdemo.StudentProvider"

android:exported="true" />

// MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText etName;

Button btnInsert, btnQuery;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

4
etName = findViewById(R.id.etName);

btnInsert = findViewById(R.id.btnInsert);

btnQuery = findViewById(R.id.btnQuery);

btnInsert.setOnClickListener(v -> {

ContentValues values = new ContentValues();

values.put("name", etName.getText().toString());

getContentResolver().insert(StudentProvider.CONTENT_URI, values);

});

btnQuery.setOnClickListener(v -> {

Cursor cursor = getContentResolver().query(StudentProvider.CONTENT_URI, null, null,


null, null);

if (cursor.moveToFirst()) {

do {

Toast.makeText(this, cursor.getString(cursor.getColumnIndex("name")),
Toast.LENGTH_SHORT).show();

} while (cursor.moveToNext());

});

5
// activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<EditText

android:id="@+id/etName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Name"/>

<Button

android:id="@+id/btnInsert"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Insert"/>

<Button

android:id="@+id/btnQuery"

android:layout_width="match_parent"

6
android:layout_height="wrap_content"

android:text="Query"/>

</LinearLayout>

Project 2: Internal Storage

// MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText etMessage;

Button btnSave, btnRead;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etMessage = findViewById(R.id.etMessage);

btnSave = findViewById(R.id.btnSave);

btnRead = findViewById(R.id.btnRead);

btnSave.setOnClickListener(v -> {

try {

FileOutputStream fos = openFileOutput("myfile.txt", MODE_PRIVATE);

7
fos.write(etMessage.getText().toString().getBytes());

fos.close();

Toast.makeText(this, "Saved to internal storage", Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

});

btnRead.setOnClickListener(v -> {

try {

FileInputStream fis = openFileInput("myfile.txt");

BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

StringBuilder sb = new StringBuilder();

String line;

while ((line = reader.readLine()) != null) {

sb.append(line).append("\n");

fis.close();

Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

});

8
}

// activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<EditText

android:id="@+id/etMessage"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Write something"/>

<Button

android:id="@+id/btnSave"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Save"/>

<Button

9
android:id="@+id/btnRead"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Read"/>

</LinearLayout>

Project 3: External Storage

// AndroidManifest.xml (permission)

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

// MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText etData;

Button btnWrite, btnRead;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etData = findViewById(R.id.etData);

10
btnWrite = findViewById(R.id.btnWrite);

btnRead = findViewById(R.id.btnRead);

btnWrite.setOnClickListener(v -> {

try {

File file = new File(Environment.getExternalStorageDirectory(), "mydata.txt");

FileOutputStream fos = new FileOutputStream(file);

fos.write(etData.getText().toString().getBytes());

fos.close();

Toast.makeText(this, "Data saved to external storage",


Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

});

btnRead.setOnClickListener(v -> {

try {

File file = new File(Environment.getExternalStorageDirectory(), "mydata.txt");

FileInputStream fis = new FileInputStream(file);

BufferedReader reader = new BufferedReader(new InputStreamReader(fis));

StringBuilder sb = new StringBuilder();

String line;

11
while ((line = reader.readLine()) != null) {

sb.append(line).append("\n");

fis.close();

Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

});

// activity_main.xml (same as previous, update IDs as needed)

Project 4: SQLite Database CRUD

// DBHelper.java

public class DBHelper extends SQLiteOpenHelper {

public static final String DB_NAME = "userDB";

public static final String TABLE_NAME = "users";

12
public DBHelper(Context context) {

super(context, DB_NAME, null, 1);

@Override

public void onCreate(SQLiteDatabase db) {

db.execSQL("CREATE TABLE " + TABLE_NAME + " (id INTEGER PRIMARY KEY


AUTOINCREMENT, name TEXT, email TEXT)");

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

onCreate(db);

public boolean insertUser(String name, String email) {

SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("name", name);

values.put("email", email);

long result = db.insert(TABLE_NAME, null, values);

return result != -1;

13
}

public Cursor getAllUsers() {

SQLiteDatabase db = this.getReadableDatabase();

return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);

// MainActivity.java

public class MainActivity extends AppCompatActivity {

EditText etName, etEmail;

Button btnAdd, btnShow;

DBHelper dbHelper;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

etName = findViewById(R.id.etName);

etEmail = findViewById(R.id.etEmail);

btnAdd = findViewById(R.id.btnAdd);

btnShow = findViewById(R.id.btnShow);

14
dbHelper = new DBHelper(this);

btnAdd.setOnClickListener(v -> {

String name = etName.getText().toString();

String email = etEmail.getText().toString();

if (dbHelper.insertUser(name, email)) {

Toast.makeText(this, "Inserted", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(this, "Failed", Toast.LENGTH_SHORT).show();

});

btnShow.setOnClickListener(v -> {

Cursor cursor = dbHelper.getAllUsers();

if (cursor.moveToFirst()) {

StringBuilder sb = new StringBuilder();

do {

sb.append("Name: ").append(cursor.getString(1)).append(", Email:


").append(cursor.getString(2)).append("\n");

} while (cursor.moveToNext());

Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();

15
});

// activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:padding="16dp">

<EditText

android:id="@+id/etName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Name"/>

<EditText

android:id="@+id/etEmail"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Enter Email"/>

16
<Button

android:id="@+id/btnAdd"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Add User"/>

<Button

android:id="@+id/btnShow"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="Show Users"/>

</LinearLayout>

17

You might also like