Write program to show user's current location using geocoding (XML & JAVA).
Step 1: Add Required Permissions in AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationdemo">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:label="Location Demo"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<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>
Step 2: Design Layout (res/layout/activity_main.xml)
<?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="24dp">
<Button
android:id="@+id/btnLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Current Location" />
<TextView
android:id="@+id/txtLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Location will appear here"
android:textSize="18sp" />
</LinearLayout>
Step 3: Java Code (MainActivity.java)
package com.example.locationdemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
Button btnLocation;
TextView txtLocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_LOCATION_PERMISSION = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLocation = findViewById(R.id.btnLocation);
txtLocation = findViewById(R.id.txtLocation);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getLocation();
}
});
}
private void getLocation() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
return;
}
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
String addressText = address.getAddressLine(0);
txtLocation.setText("Current Location:\n" + addressText);
} else {
txtLocation.setText("Unable to get address.");
}
} catch (IOException e) {
e.printStackTrace();
txtLocation.setText("Geocoder failed.");
}
} else {
txtLocation.setText("Location is null.");
}
}
});
}
// Handle the result of permission request
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_LOCATION_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
txtLocation.setText("Permission denied.");
}
}
}
}
Program to store and retrieve students data into database (JAVA & XML)
Step 1:AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.studentdatabase">
<application
android:allowBackup="true"
android:label="StudentDB"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<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>
2. Layout File – res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editName"
android:hint="Enter Name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:id="@+id/editRoll"
android:hint="Enter Roll Number"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnSave"
android:text="Save"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnView"
android:text="View All Students"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/txtResult"
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Results will appear here"/>
</LinearLayout>
3. SQLite Database Helper – StudentDatabaseHelper.java
package com.example.studentdatabase;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class StudentDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "students.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "student";
public static final String COL_NAME = "name";
public static final String COL_ROLL = "roll";
public StudentDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (" +
COL_ROLL + " INTEGER PRIMARY KEY, " +
COL_NAME + " TEXT NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertStudent(String name, int roll) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COL_NAME, name);
values.put(COL_ROLL, roll);
long result = db.insert(TABLE_NAME, null, values);
return result != -1;
}
public String getAllStudents() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
StringBuilder builder = new StringBuilder();
while (cursor.moveToNext()) {
int roll = cursor.getInt(cursor.getColumnIndexOrThrow(COL_ROLL));
String name = cursor.getString(cursor.getColumnIndexOrThrow(COL_NAME));
builder.append("Roll: ").append(roll).append(", Name: ").append(name).append("\n");
}
cursor.close();
return builder.toString();
}
}
4. Activity Code – MainActivity.java
package com.example.studentdatabase;
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 {
EditText editName, editRoll;
Button btnSave, btnView;
TextView txtResult;
StudentDatabaseHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editName = findViewById(R.id.editName);
editRoll = findViewById(R.id.editRoll);
btnSave = findViewById(R.id.btnSave);
btnView = findViewById(R.id.btnView);
txtResult = findViewById(R.id.txtResult);
dbHelper = new StudentDatabaseHelper(this);
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editName.getText().toString();
String rollStr = editRoll.getText().toString();
if (name.isEmpty() || rollStr.isEmpty()) {
Toast.makeText(MainActivity.this, "Please fill both fields", Toast.LENGTH_SHORT).show();
return;
}
int roll = Integer.parseInt(rollStr);
boolean inserted = dbHelper.insertStudent(name, roll);
if (inserted) {
Toast.makeText(MainActivity.this, "Student Added", Toast.LENGTH_SHORT).show();
editName.setText("");
editRoll.setText("");
} else {
Toast.makeText(MainActivity.this, "Failed to Add Student", Toast.LENGTH_SHORT).show();
}
}
});
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String data = dbHelper.getAllStudents();
txtResult.setText(data.isEmpty() ? "No data found." : data);
}
});
}
}