تطبيق إعلانات بأندرويد و firebase الجزء الثالت
نظرة سريعة بالفيديو
1- إضافة فورم التسجيل
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/white"
android:id="@+id/registerLayout"
tools:context="com.example.belasri.hiresell.RegisterActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar"></include>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:elevation="5dp"
android:layout_marginBottom="20dp"
android:background="@color/white"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:text="@string/Register"
android:textColor="@color/colorPrimary"
android:fontFamily="sans-serif"
android:textSize="30sp"
android:padding="10dp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/colorPrimaryDark"></View>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_fullname_layout"
android:layout_marginTop="50dp"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/fullname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="text"
android:hint="@string/full_name"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_email_layout"
android:textAlignment="center"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textEmailAddress"
android:hint="@string/email"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_password_layout"
android:layout_below="@+id/text_email_layout"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
app:passwordToggleEnabled="true"
app:counterOverflowTextAppearance="@style/textErrorColor"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textPassword"
android:hint="@string/password"
/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn"
android:text="@string/register"
android:textAllCaps="false"
android:textColor="@color/white"
android:onClick="registerNewUser"
android:textSize="20dp"
android:layout_marginTop="20dp"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="24dp"
android:text="Déjà inscrit"
android:textColor="@color/colorPrimary"
android:gravity="center"
android:textSize="16sp"/>
</LinearLayout>
</ScrollView>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/snackbar"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
2- إضافة المستخدم الجديد لقاعدة البيانات ف firebase
package com.example.belasri.hiresell;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.UserInfo;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class RegisterActivity extends AppCompatActivity {
AppCompatEditText fullName,Email,Password;
private Toolbar toolbar;
private FirebaseAuth mAuth;
private TextInputLayout fullnameLayout,emailLayout,passwordLayout;
private DatabaseReference db;
private ProgressBar progressBar;
private RelativeLayout layout;
private TextView registerText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
toolbar = (Toolbar) findViewById(R.id.toolbar);
layout = (RelativeLayout) findViewById(R.id.registerLayout);
progressBar = new ProgressBar(RegisterActivity.this,null,android.R.attr.progressBarStyleLarge);
fullName = (AppCompatEditText) findViewById(R.id.fullname);
Email = (AppCompatEditText) findViewById(R.id.email);
Password = (AppCompatEditText) findViewById(R.id.password);
fullnameLayout = (TextInputLayout) findViewById(R.id.text_fullname_layout);
passwordLayout = (TextInputLayout) findViewById(R.id.text_password_layout);
emailLayout = (TextInputLayout) findViewById(R.id.text_email_layout);
mAuth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance().getReference().child("profiles");
setSupportActionBar(toolbar);
registerText = (TextView) findViewById(R.id.link_signup);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
goToRegister();
}
@Override
public void onBackPressed()
{
super.onBackPressed();
finish();
}
public void checkField(){
Boolean cancel = false;
if(fullName.getText().toString().isEmpty()){
fullnameLayout.setErrorEnabled(true);
fullnameLayout.setError("Ce champ est obligatoire");
cancel = true;
}else{
fullnameLayout.setErrorEnabled(false);
cancel = false;
}
if(Password.getText().toString().isEmpty()){
passwordLayout.setErrorEnabled(true);
passwordLayout.setError("Ce champ est obligatoire");
cancel = true;
}else{
passwordLayout.setErrorEnabled(false);
cancel = false;
}
if(Email.getText().toString().isEmpty()){
emailLayout.setErrorEnabled(true);
emailLayout.setError("Ce champ est obligatoire");
cancel = true;
}else{
emailLayout.setErrorEnabled(false);
cancel = false;
}
if(!cancel){
addUser();
}
}
public void registerNewUser(View v){
checkField();
}
public void addUser(){
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.snackbar);
final String fullname = fullName.getText().toString();
final String email = Email.getText().toString();
String password = Password.getText().toString();
showProgressBar();
mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(this,new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
UserInfo user = mAuth.getCurrentUser();
DatabaseReference current_user_profile = db.child(user.getUid());
current_user_profile.child("user_id").setValue(user.getUid());
current_user_profile.child("fullname").setValue(fullname);
progressBar.setVisibility(View.GONE);
layout.removeView(progressBar);
Email.setText("");
Password.setText("");
fullName.setText("");
Snackbar snackbar = Snackbar.make(coordinatorLayout, "Compte crée", Snackbar.LENGTH_LONG).setAction("Login", new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginIntent = new Intent(RegisterActivity.this,LoginActivity.class);
finish();
startActivity(loginIntent);
}
});
snackbar.show();
}else{
progressBar.setVisibility(View.GONE);
layout.removeView(progressBar);
Toast.makeText(RegisterActivity.this,"Erreur : " + task.getException(), Toast.LENGTH_SHORT).show();
}
}
});
}
private void showProgressBar(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
progressBar.setVisibility(View.VISIBLE); //To show ProgressBar// To Hide ProgressBar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
private void goToRegister(){
registerText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new Intent(RegisterActivity.this,LoginActivity.class);
finish();
startActivity(registerIntent);
}
});
}
}
3- إضافة فورم تسجيل الدخول
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/loginLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="com.example.belasri.hiresell.LoginActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout">
<include layout="@layout/toolbar"></include>
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:orientation="vertical"
android:background="@color/white"
android:layout_marginBottom="20dp"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:text="@string/login"
android:textColor="@color/colorPrimary"
android:fontFamily="sans-serif"
android:textSize="30sp"
android:padding="10dp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/colorPrimaryDark"></View>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_email_layout"
android:layout_marginTop="50dp"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/pseudo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textEmailAddress"
android:hint="@string/email"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_password_layout"
android:layout_below="@+id/text_email_layout"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
app:passwordToggleEnabled="true"
app:counterOverflowTextAppearance="@style/textErrorColor"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="textPassword"
android:hint="@string/password"
/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn"
android:text="@string/login_text"
android:textColor="@color/white"
android:textSize="20dp"
android:onClick="logUser"
android:layout_marginTop="20dp"
android:textAllCaps="false"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/link_signup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:layout_marginTop="20dp"
android:layout_marginBottom="24dp"
android:text="Nouveau utilisateur inscrivez vous."
android:gravity="center"
android:textSize="16sp"/>
</LinearLayout>
</ScrollView>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/snackbar"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
4- تسجيل دخول المستخدم ب firebase auth
فالمجلد java كاين LoginActivity.java لي هو الملف الي فيه الكود لي كيمكن المستخدم باش يتكونيكطا فأول حاجة كنسترجع les champs من الملف activity_login.xml من بعد كاين ل fonction goToLogin ولي كتدي المستخدم للملف الخاص بالتسجيل يلا مكانش عندو حساب ولي كتنفد ملي كيضغط على TextView لي زدنا ثم كاين fonction checkField لي كنسترجع بها القيم لي دخل المستخدم فالحقول وكنتحقق بلي حتى حقل ما خاوي ثم كاين ل fonction logUser كتنفد ملي كيضغط المستخدم على connexion لي منبعد مكينجح التحقق فكنخدم بل FirebaseAuth باش كنكونيكطي المستخدم بالإمايل ديالو وال mot de passe ولي يلا نجحت الكونيكسيون كنوجهوه للصفحة الرئيسية لي فيها الإعلانات معروضين الكود ديال الملف هو :
package com.example.belasri.hiresell;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import org.w3c.dom.Text;
public class LoginActivity extends AppCompatActivity {
private Toolbar toolbar;
private AppCompatEditText Email,Password;
private TextInputLayout emailLayout,passwordLayout;
private FirebaseAuth mAuth;
private ProgressBar progressBar;
private RelativeLayout layout;
private TextView loginText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
layout = (RelativeLayout) findViewById(R.id.loginLayout);
progressBar = new ProgressBar(LoginActivity.this,null,android.R.attr.progressBarStyleLarge);
Email = (AppCompatEditText) findViewById(R.id.pseudo);
Password = (AppCompatEditText) findViewById(R.id.password);
loginText = (TextView) findViewById(R.id.link_signup);
emailLayout = (TextInputLayout) findViewById(R.id.text_email_layout);
passwordLayout = (TextInputLayout) findViewById(R.id.text_password_layout);
mAuth = FirebaseAuth.getInstance();
goToLogin();
}
@Override
public void onBackPressed()
{
super.onBackPressed();
finish();
}
public Boolean checkField(){
Boolean valid = true;
if(Email.getText().toString().isEmpty()){
emailLayout.setErrorEnabled(true);
emailLayout.setError("Ce champ est obligatoire");
valid = false;
}else{
emailLayout.setErrorEnabled(false);
}
if(Password.getText().toString().isEmpty()){
passwordLayout.setErrorEnabled(true);
passwordLayout.setError("Ce champ est obligatoire");
valid = false;
}else{
passwordLayout.setErrorEnabled(false);
}
return valid;
}
public void logUser(View view) {
if(checkField()){
authUser();
}
}
private void authUser() {
final CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.snackbar);
String email = Email.getText().toString();
String passord = Password.getText().toString();
showProgressBar();
mAuth.signInWithEmailAndPassword(email,passord).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
progressBar.setVisibility(View.GONE);
layout.removeView(progressBar);
new android.os.Handler().postDelayed(new Runnable() {
@Override
public void run() {
Snackbar snackbar = Snackbar.make(coordinatorLayout, "Connecté", Snackbar.LENGTH_LONG);
snackbar.show();
}
},2000);
Intent loginIntent = new Intent(LoginActivity.this,MainActivity.class);
finish();
startActivity(loginIntent);
}else{
progressBar.setVisibility(View.GONE);
layout.removeView(progressBar);
Toast.makeText(LoginActivity.this,"Email ou mot de passe est incorrect", Toast.LENGTH_SHORT).show();
}
}
});
}
private void showProgressBar(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
progressBar.setVisibility(View.VISIBLE); //To show ProgressBar// To Hide ProgressBar
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
}
private void goToLogin(){
loginText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new Intent(LoginActivity.this,RegisterActivity.class);
finish();
startActivity(registerIntent);
}
});
}
}
5- إضافة الملف الخاص بإضافة الإعلانات
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/white"
tools:context="com.example.belasri.hiresell.CreateAds">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/toolbar"></include>
</RelativeLayout>
<ScrollView
android:layout_marginTop="55dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="10dp"
android:elevation="5dp"
android:background="@color/white"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:id="@+id/title"
android:text="@string/post_ad"
android:textColor="@color/colorPrimary"
android:fontFamily="sans-serif"
android:textSize="30sp"
android:padding="10dp"
android:textAlignment="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/colorPrimaryDark"></View>
<android.support.design.widget.TextInputLayout
android:id="@+id/text_category_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="32dp"
android:layout_marginRight="32dp"
android:layout_marginTop="50dp">
<Spinner
android:id="@+id/categorieSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_city_layout"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
>
<Spinner
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/citySpinner" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_title_layout"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/adtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="text"
android:hint="@string/title"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_body_layout"
android:layout_below="@+id/text_title_layout"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
android:inputType="textMultiLine"
app:passwordToggleEnabled="true"
app:counterOverflowTextAppearance="@style/textErrorColor"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/adbody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ems="10"
android:inputType="textMultiLine"
android:hint="@string/description"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_phone_layout"
android:layout_below="@+id/text_body_layout"
android:layout_marginLeft="32dp"
android:layout_marginBottom="8dp"
android:layout_marginRight="32dp"
app:passwordToggleEnabled="true"
app:counterOverflowTextAppearance="@style/textErrorColor"
>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/userTel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:ems="10"
android:inputType="number"
android:hint="Tél"
/>
</android.support.design.widget.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<ImageButton
android:id="@+id/image_uploaded"
android:background="@color/white"
android:layout_width="match_parent"
android:src="@drawable/ic_camera"
android:onClick="uploadAdImage"
android:layout_height="150dp"
android:padding="10dp"
android:layout_marginBottom="24dp"
android:layout_gravity="center_horizontal" />
</LinearLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/btn"
android:text="@string/add_add"
android:textColor="@color/white"
android:onClick="createNewAd"
android:textSize="20dp"
android:textAllCaps="false"
android:layout_marginTop="20dp"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</RelativeLayout>