Dennis muroni
Dennis muroni

Reputation: 21

Progressbar while the RecyclerView search for values in firebase

Good night. often the RecyclerView is delayed to return with Firebase values, this causes my main window to go blank at this time. I would like to put a ProgressBar while the Recyclerview returns nothing. I would like something Simple, in the login screen I able to do this, while it verifies if the user and password are valid. But on my main screen I could not, I tried everything and I researched everything. Can someone help me? thank you.

This is my code:

 import android.content.Context;
 import android.content.Intent;
 import android.support.annotation.NonNull;
 import android.support.design.widget.FloatingActionButton;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.text.TextUtils;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.ImageView;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.firebase.ui.database.FirebaseRecyclerAdapter;
 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.database.DatabaseReference;
 import com.google.firebase.database.FirebaseDatabase;
 import com.squareup.picasso.Picasso;

 public class MainActivity extends AppCompatActivity {
 FloatingActionButton contatoLojas;
 private RecyclerView mBlogList;
 FirebaseDatabase database;
 DatabaseReference myRef;


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

    contatoLojas = (FloatingActionButton)findViewById(R.id.contatolojas);
    contatoLojas.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(MainActivity.this, ContatoLojasActivity.class));
        }
    });

    //Recycler View
    mBlogList = (RecyclerView)findViewById(R.id.blog_list);
    mBlogList.setHasFixedSize(true);
    mBlogList.setLayoutManager(new LinearLayoutManager(this));

    //Send a Query to the database
    database = FirebaseDatabase.getInstance();
    myRef = database.getReference("Data");
}


@Override
protected void onStart() {
    super.onStart();

    FirebaseRecyclerAdapter<ModelClass, BlogViewHolder> firebaseRecyclerAdapter =
            new FirebaseRecyclerAdapter<ModelClass, BlogViewHolder>(
                    ModelClass.class,
                    R.layout.design_row,
                    BlogViewHolder.class,
                    myRef){

                @Override
                protected void populateViewHolder(BlogViewHolder viewHolder, ModelClass model, int position) {
                    viewHolder.setTitle(model.getTitle());
                    viewHolder.setValor(model.getValor());
                    viewHolder.setDesc(model.getDesc());
                    viewHolder.setImage(getApplicationContext(), model.getImage());
                }
            };
   mBlogList.setAdapter(firebaseRecyclerAdapter);
}

public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder (View itemView){
    super(itemView);
    mView= itemView;
}

public void setTitle (String title){
    TextView post_title = (TextView)mView.findViewById(R.id.titleText);
    post_title.setText(title);
}

public void setValor (String valor){
    TextView post_title2 = (TextView)mView.findViewById(R.id.titleText2);
    post_title2.setText(valor);
}

public void setDesc (String desc){
    TextView post_title3 = (TextView)mView.findViewById(R.id.titleText3);
    post_title3.setText(desc);
}

public void setImage (Context ctx , String image){
    ImageView post_image = (ImageView)mView.findViewById(R.id.imageViewy);
    // We need TO pass Context
    Picasso.with(ctx).load(image).into(post_image);
   // Picasso.with(ctx).load(image).into(post_image);
}
 }}

This is the login screen code, which is working fine.

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
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.FirebaseApp;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class SplashScreen extends AppCompatActivity {

FirebaseDatabase database1;
DatabaseReference myRef1;
private EditText edtLogin;
private EditText edtSenha;
private Button btnVerOfertas;
private ProgressBar progressCarregando;
private FirebaseAuth mAuth;

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

    edtLogin = (EditText) findViewById(R.id.edt_login);
    edtSenha = (EditText) findViewById(R.id.edt_senha);
    btnVerOfertas = (Button) findViewById(R.id.btn_VerOfertas);
    progressCarregando = (ProgressBar) findViewById(R.id.progress_Carregando);

    mAuth = FirebaseAuth.getInstance();

    String email = edtLogin.getText().toString();
    String password = edtSenha.getText().toString();

    if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
        progressCarregando.setVisibility(View.VISIBLE);

        mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()){
                    sendToMain();
                    progressCarregando.setVisibility(View.INVISIBLE);
                }else {
                    Toast.makeText(SplashScreen.this, "Erro : " + task.getException().getMessage(), Toast.LENGTH_LONG).show();
                    progressCarregando.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
}

}

Upvotes: 2

Views: 1465

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138824

To solve this, add a ProgressBar view inside your .XML layout file like this:

<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/progress_bar"
    android:layout_gravity="center"/>

Then find it in your activity class:

progressBar = (ProgressBar) findViewById(R.id.progress_bar);

Don't forget also to declare it as a global variable:

private ProgressBar progressBar;

Then inside your FirebaseRecyclerAdapter class, just override the following method:

@Override
public void onDataChanged() {
    if (progressBar != null) {
        progressBar.setVisibility(View.GONE);
    }
}

This means that when the activity starts, the progress bar will be active and visible and by the time you have finished getting the data from the database, the progress bar will gone.

Upvotes: 2

Janvi Vyas
Janvi Vyas

Reputation: 752

You can show Progressbar instead of blank screen using below code :-

First, Create one method like showLoader()

protected Dialog loadDialog = null;  // make it global for single activity - in your case Main window.

    protected void showLoader(Context context) {
        if (loadDialog != null) {
            if (loadDialog.isShowing()) {
                if (!((Activity) context).isFinishing()) {
                    loadDialog.dismiss();
                }
            }
        }
        loadDialog = new Dialog(context, R.style.TransparentDialogTheme);
        loadDialog.setContentView(R.layout.spinner_rotate);
        loadDialog.setCanceledOnTouchOutside(false);

        loadDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                return keyCode == KeyEvent.KEYCODE_BACK;
            }
        });


        if (loadDialog != null && !((Activity) context).isFinishing() && !loadDialog.isShowing()) {
            loadDialog.show();
        }
    }

Add below dependency to gradle

 compile 'com.wang.avi:library:2.1.3'

In spinner_rotate.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:background="@android:color/transparent"
     android:gravity="center">

    <com.wang.avi.AVLoadingIndicatorView
    style="@style/AVLoadingIndicatorView.Large"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:visibility="visible"
     app:indicatorColor="@color/colorBlue"
     app:indicatorName="BallClipRotateIndicator" />

</LinearLayout>

You can call this method when before(at above line) writing code of fetching data from firebase and set data to adapter when your data set in adapter you need to hide that loader using below hideLoader() method.

void hideSpinner() {
        if (loadDialog.isShowing()) {
            if (!(BaseActivity.this).isFinishing()) {
                loadDialog.dismiss();
            }

        }

    }

Show Whole code which may be help you

 showSpinner(this);

        BaseActivity.mDatabase.child("your database's child name where you want to get data").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot != null && dataSnapshot.hasChildren()) {
                for (DataSnapshot child : dataSnapshot.getChildren()) {

//          here add data to list which are fetched from firebase

                }
            }

            if (adapter == null) {
//                     Then set Your adapter here ..
            }

            hideSpinner();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            hideSpinner();

        }
    });

I hope it may help you.

Upvotes: 0

Related Questions