Rizwan Sanawar (BCSM-F17-132)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Mobile App Development

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;

class GroceryStore extends SQLiteOpenHelper {

private static final String dbname = "mad";


private static final int version = 1;

public GroceryStore(Context context){


super(context, dbname, null, version);
}

@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:

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/design_default_color_on_primary"
tools:context=".ProfileActivity">

<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;

public class MainActivity extends AppCompatActivity {

private FirebaseUser user;


private DatabaseReference reference;
private String userID;
private Button logout;

private Button btn;


private TextView textview;
private FusedLocationProviderClient fusedLocationProviderClient;

@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){

Double lat = location.getLatitude();


Double longt = location.getLongitude();

textview.setText(lat+", "+longt);

Toast.makeText(MainActivity.this, "Success",
Toast.LENGTH_SHORT);
}

}
});

}
else{
requestPermissions(new String[]
{Manifest.permission.ACCESS_FINE_LOCATION});
}
}
}

private void requestPermissions(String[] strings) {


}
});

logout = (Button) findViewById(R.id.signin);

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();

final TextView greetingTextView = (TextView) findViewById(R.id.greeting);


final TextView fullNameTextView = (TextView) findViewById(R.id.fullName);
final TextView emailTextView = (TextView) findViewById(R.id.emailAddress);
final TextView ageTextView = (TextView) findViewById(R.id.age);

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;

greetingTextView.setText("Welcome, " + fullName + "1");


fullNameTextView.setText(fullName);
emailTextView.setText(email);
ageTextView.setText(age);

}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(MainActivity.this, "Something wrong happened!",
Toast.LENGTH_LONG).show();

}
});
}
}

You might also like