Search code examples
androidurlfirebasefirebase-realtime-databaseandroid-webview

WebView implementation with Firebase Database in Android


I want to implement webView with Firebase so that if I add a URL in firebase Database it gets implemented on my webView in application in realtime, I have successfully implemented the text and images through database but could not get webView work correctly, I have searched many options but neither work for me, please help. If you want any more detail I can provide you.

I am using Android Studio

Below is my Java class code for Firebase implementation for Images and text

@Override
    public void onStart() {
        super.onStart();
        FirebaseUser currentUser = mAuth.getCurrentUser();
        if (currentUser == null) {
            sentToStart();
        }
        FirebaseRecyclerAdapter <post, postViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, postViewHolder>(

            post.class,
            R.layout.post_row_recycle_home,
            postViewHolder.class,
            mDatabaseReference
    ) {
        @Override
        protected void populateViewHolder(postViewHolder viewHolder, post model, int position) {
            viewHolder.setTitle(model.getTitle());
            viewHolder.setdescription(model.getDescription());
            viewHolder.setimage(getApplicationContext(), model.getImage());
            viewHolder.setsource(model.getSource());
        }
    };
    mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class postViewHolder extends RecyclerViewPager.ViewHolder{

    View mView;

    public postViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setTitle(String title){
        TextView post_title = (TextView)mView.findViewById(R.id.title_cardView);
        post_title.setText(title);
    }
    public void setsource(String source){
        TextView post_source = (TextView)mView.findViewById(R.id.source_cardView);
        post_source.setText(source);
    }

    public void setdescription(String description){
        TextView post_description = (TextView)mView.findViewById(R.id.description_cardView);
        post_description.setText(description);
    }

    public void setimage(final Context ctx, final String image){
        final ImageView post_image = (ImageView)mView.findViewById(R.id.post_image);
        Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() {

            @Override
            public void onSuccess() {
            }
            @Override
            public void onError() {
                Picasso.with(ctx).load(image).into(post_image);
            }
        });
    }
}

Getter/Setter Java Class

public class post {

    private String title;
    private String description;
    private String image;
    private String source;



    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public post(){
    }

    public post(String title, String description, String image, String source) {
        this.title = title;
        this.description = description;
        this.image = image;
        this.source = source;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

}

Updated: WebView java Activity

public class webViewNews extends AppCompatActivity {

    private WebView webviewthis;
    private DatabaseReference mDatabaseReference;
    private DatabaseReference mdataRef;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_page);
        mdataRef = FirebaseDatabase.getInstance().getReference().child("webView");
        webviewthis = (WebView)findViewById(R.id.webView_news);
        webviewthis.setWebViewClient(new WebViewClient());
        webviewthis.getSettings().setJavaScriptEnabled(true);
        webviewthis.getSettings().setLoadsImagesAutomatically(true);


        mdataRef.child("webView").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                    dataSnapshot.child("webView").getValue();
                    webviewthis.loadUrl("");

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

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

Solution

  • First of all, as i see in your code, you are using .child("webView") twice. Once in the DatabaseReference and once when you add the ValueEventListener. Is that what you are having in your database? If there are no duplicate childrens in your database, you need to remove one of them.

    You have added a ValueEventListener on mdataRef but you are doing nothing there. Your onDataChange() method is empty. Remember, this method is triggered every time a change is taking place in your database. So in order to display some content, you need to get the data out from the dataSnapshot object according to your database structure and display it right there.

    Hope it helps.