Tutorial 4 - Data Retrieve

 



This is Sample Project Which you can improve UI design & Coding skills .

*This is data retrieve from database*


activity_home

HomeActivity

WordPress

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/top_gradient" />

<TextView

android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Covid Tracker"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:height="30dp"
android:fontFamily="sans-serif-smallcaps"
android:textAlignment="center"
android:textSize="25dp"
android:textColor="@color/white"
android:gravity="center_horizontal" />

<ScrollView

android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_marginRight="10dp"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Your Profile"
android:fontFamily="@font/baloo"
android:textAlignment="center"
android:textSize="20dp"
android:gravity="center_horizontal" />

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:src="@drawable/ic_user"
android:scaleType="centerCrop"
/>

<EditText
android:id="@+id/profileName"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:textColor="@color/textcolor"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_user"
android:hint="Your Name"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

<EditText
android:id="@+id/profileEmail"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_email"
android:hint="Your Email Address"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

<EditText
android:id="@+id/profilePhone"
android:layout_width="350dp"
android:layout_height="45dp"
android:editable="false"
android:background="@drawable/input_style"
android:drawableRight="@drawable/ic_phone"
android:hint="Your Phone"
android:textColor="@color/textcolor"
android:layout_gravity="center"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/baloo"
/>

</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btn_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_style"
android:layout_alignParentEnd="true"
android:text="Log Out"
android:textSize="18dp"
android:textStyle="bold"
android:textColor="#FFF"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"/>

</RelativeLayout>

package com.rrmchathura.myfirstapp;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;

public class HomeActivity extends AppCompatActivity {

private EditText fullName,email,phone;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;

private Button logout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

fullName = findViewById(R.id.profileName);
email = findViewById(R.id.profileEmail);
phone = findViewById(R.id.profilePhone);

logout = findViewById(R.id.btn_logout);

fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();

userID = fAuth.getCurrentUser().getUid();

DocumentReference documentReference = fStore.
collection("users").document(userID);
documentReference.
addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot documentSnapshot,
@Nullable FirebaseFirestoreException error) {
phone.setText(documentSnapshot.getString("phone"));
fullName.setText(documentSnapshot.getString("fName"));
email.setText(documentSnapshot.getString("email"));
}
});

logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Toast.makeText(HomeActivity.
this,"Logeed Out!",Toast.LENGTH_SHORT).show();
startActivity(new Intent(HomeActivity.
this,LoginActivity.class));
finish();
}
});
}
}


Post a Comment

0 Comments

Youtube Channel Image
Coding With Chathura Subscribe To watch more Project Tutorials
Subscribe
Do you have any doubts? chat with us on WhatsApp
Hello, How can I help you? ...
Click me to start the chat...

Welcome to freesourcecodelk

X