Location and DB
Location and DB
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationdemo">
<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>
<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>
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;
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();
}
});
}
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.");
}
}
});
}
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>
<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>
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;
@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);
}
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;
@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);
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;
}
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);
}
});
}
}