Rizwan Sanawar (BCSM-F17-132)
Rizwan Sanawar (BCSM-F17-132)
Rizwan Sanawar (BCSM-F17-132)
Rizwan Sanawar
BCSM-F17-132
BSSE-6B
Assignment #3:
Question. 1
Main_Activity.JAVA Code:
SQLITE:
package com.example.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
@Override
public void onCreate(SQLiteDatabase db) {
String sql= "CREATE TABLE PRODUCT (_ID INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT, DESCRIPTION TEXT, PRICE REAL)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
Question.2
Profile_Activity.XML Code:
<TextView
android:id="@+id/greeting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
/>
<TextView
android:id="@+id/emailAddressTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="220dp"
android:text="Email Address:"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="@+id/greeting"
app:layout_constraintTop_toBottomOf="@+id/greeting" />
<TextView
android:id="@+id/emailAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/emailAddressTitle"
app:layout_constraintLeft_toLeftOf="@+id/emailAddressTitle"
android:layout_marginLeft="40dp" />
<TextView
android:id="@+id/fullNameTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Full Name:"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="@+id/emailAddress"
app:layout_constraintLeft_toLeftOf="@+id/greeting"
android:layout_marginTop="25dp"
android:layout_marginLeft="40dp"
android:textSize="20sp"
/>
<TextView
android:id="@+id/fullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/fullNameTitle"
app:layout_constraintLeft_toLeftOf="@+id/emailAddressTitle"
android:textSize="16sp"
android:layout_marginLeft="40dp"/>
<TextView
android:id="@+id/ageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age:"
android:textStyle="bold"
android:textSize="20sp"
app:layout_constraintTop_toBottomOf="@+id/fullName"
app:layout_constraintLeft_toLeftOf="@+id/fullNameTitle"
android:layout_marginTop="33dp"
/>
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/ageTitle"
app:layout_constraintLeft_toLeftOf="@+id/fullNameTitle"
android:layout_marginLeft="60dp"/>
<Button
android:id="@+id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
app:layout_constraintTop_toBottomOf="@+id/ageTitle"
app:layout_constraintLeft_toLeftOf="@+id/fullNameTitle"
android:layout_marginLeft="100dp"
android:layout_marginTop="85dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
User_Location.XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ProfileActivity">
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Location"
android:layout_marginTop="200sp"
android:layout_marginLeft="40sp"
app:layout_constraintsBottom_toBottomOf="parent"
app:layout_constaintEnd_toEndOf="parent"
app:layout_constraintStart_toStartof="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location"
android:layout_marginLeft="120sp"
android:layout_marginTop="50sp"/>
</LinearLayout>
Main_Activity.JAVA Code:
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
btn = findViewById(R.id.button_btn);
textview=findViewById(R.id.location);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M){
if(getApplicationContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATIO
N)
== PackageManager.PERMISSION_GRANTED){
fusedLocationProviderClient.getLastLocation()
.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null){
textview.setText(lat+", "+longt);
Toast.makeText(MainActivity.this, "Success",
Toast.LENGTH_SHORT);
}
}
});
}
else{
requestPermissions(new String[]
{Manifest.permission.ACCESS_FINE_LOCATION});
}
}
}
logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent( MainActivity.this, MainActivity.class));
}
});
user = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users");
userID = user.getUid();
reference.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
User userProfile = snapshot.getValue(User.class);
if(userProfile != null){
String fullName = userProfile.fullName;
String email= userProfile.email;
String age = userProfile.age;
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, "Something wrong happened!",
Toast.LENGTH_LONG).show();
}
});
}
}