1 — SharedPreferences (Simple Storage)
Lightweight key-value persistent storage
What it is
SharedPreferences is used to store small data in key-value pairs (like saving settings or simple user data).
Key points
- Data is saved permanently (until app is deleted)
- Very fast and easy to use
- Used for login status, user preferences (theme, language)
How your program works
You type a name in the input box. When you tap Save, the name is stored using a key like "username". When you tap Load, the app reads that value and shows it on screen.
<?xml version="1.0" encoding="utf-8"?>
<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="20dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"/>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save"/>
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load"/>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="18sp"/>
</LinearLayout>
package com.example.sharedprefdemo;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button btnSave, btnLoad;
TextView textView;
SharedPreferences sharedPreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editTextName);
btnSave = findViewById(R.id.btnSave);
btnLoad = findViewById(R.id.btnLoad);
textView = findViewById(R.id.textViewResult);
// Create SharedPreferences
sharedPreferences = getSharedPreferences("MyPrefs", MODE_PRIVATE);
// Save Data
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", name);
editor.apply();
textView.setText("Saved!");
}
});
// Load Data
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String savedName = sharedPreferences.getString("username", "No Data");
textView.setText(savedName);
}
});
}
}
Key concepts
2 — SQLite (Database Storage)
Local structured database with rows and columns
What it is
SQLite is a local database inside Android used to store large or structured data (like tables).
Key points
- Stores data in table format (rows & columns)
- Can store multiple records
- Used for user lists, offline apps, notes, contacts, etc.
How your program works
A database and table (users) is created. When you tap Save, the name is inserted into the table. When you tap Load, the app reads data from the table and shows the last saved name.
<?xml version="1.0" encoding="utf-8"?>
<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="20dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"/>
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save to DB"/>
<Button
android:id="@+id/btnLoad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load from DB"/>
<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Result"
android:textSize="18sp"/>
</LinearLayout>
package com.example.sqlitedemo;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "MyDB", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE users(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS users");
onCreate(db);
}
// Insert data
public void insertData(String name) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("INSERT INTO users(name) VALUES('" + name + "')");
}
// Get last saved name
public String getData() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM users", null);
String result = "No Data";
if (cursor.moveToLast()) {
result = cursor.getString(1); // name column
}
cursor.close();
return result;
}
}
package com.example.sqlitedemo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button btnSave, btnLoad;
TextView textView;
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editTextName);
btnSave = findViewById(R.id.btnSave);
btnLoad = findViewById(R.id.btnLoad);
textView = findViewById(R.id.textViewResult);
dbHelper = new DBHelper(this);
// Save to SQLite
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editText.getText().toString();
dbHelper.insertData(name);
textView.setText("Saved to DB!");
}
});
// Load from SQLite
btnLoad.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = dbHelper.getData();
textView.setText(data);
}
});
}
}
Key concepts
3 — Android Camera Demo
Capture image using Intent and display in ImageView
What it is
This uses Android's camera to take a photo and display it directly inside your app using an ImageView.
Key points
- Uses Intent to open the camera app
- Returns a small image (thumbnail)
- Needs camera permission in the manifest
How your program works
When you tap Open Camera, the camera app opens. After clicking a photo, the image is returned to your app and displayed in the ImageView.
<?xml version="1.0" encoding="utf-8"?>
<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="20dp"
android:gravity="center">
<Button
android:id="@+id/btnCamera"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Camera"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="20dp"
android:scaleType="centerCrop"/>
</LinearLayout>
package com.example.camerademo;
import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
public class CamActivity extends AppCompatActivity {
ImageView imageView;
Button btnCamera;
ActivityResultLauncher cameraLauncher;
ActivityResultLauncher permissionLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cam);
imageView = findViewById(R.id.imageView);
btnCamera = findViewById(R.id.btnCamera);
cameraLauncher = registerForActivityResult(
new ActivityResultContracts.TakePicturePreview(),
bitmap -> {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
);
permissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
isGranted -> {
if (isGranted) {
cameraLauncher.launch(null); // open camera
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
}
);
btnCamera.setOnClickListener(view -> {
permissionLauncher.launch(Manifest.permission.CAMERA);
});
}
}
Note: This captures a small thumbnail image (not full-size). Works on most devices without extra setup.
Key concepts

0 Comments
If you have any doubts, Please let me know