1

I want to add webView in Firebase so that if i have to change the webpage URL of that webView , i can simply add a new URL in firebase Database and it changes the webView accordingly

I have searched everywhere but didnt get helpful answer, is it possible with firebase to do so?

Update:

Actually I am trying to build a news app, where users can click on web link of the news and it will take them to that respective web page of in-app web browser, I don't think remote-config will be helpful in this purpose.

I'm using Android Studio.

Firebase Java Class File

public class webViewNews extends AppCompatActivity {
    private RecyclerView webRecycle;
    private DatabaseReference mDatabaseReference;
    private WebView webviewthis;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview_page);
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("play");
        webviewthis = (WebView) findViewById(R.id.webView_news);
        webRecycle = (RecyclerView) findViewById(R.id.webViewList);
        webviewthis.getSettings().setJavaScriptEnabled(true);
        webviewthis.getSettings().setLoadsImagesAutomatically(true);
        webviewthis.loadUrl(mDatabaseReference.toString());
    }
    @Override
    protected void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<post, webViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post, webViewHolder>(
                post.class,
                R.layout.webview_card,
                webViewHolder.class,
                mDatabaseReference

        ) {
            @Override
            protected void populateViewHolder(webViewHolder viewHolder, post model, int position) {
                viewHolder.setWebViewPost( model.getWebViewPost());
            }
        };
        webRecycle.setAdapter(firebaseRecyclerAdapter);

    }

    public static class webViewHolder extends RecyclerViewPager.ViewHolder {

        View mView;

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

        public void setWebViewPost( final String webViewPost) {
            WebView WebViewPost = (WebView)mView.findViewById(R.id.webView_news);
            WebViewPost.loadUrl(webViewPost);
        }
    }
}

Post Class File

public class postClass {
private String titleName, DescriptionName, Picture,webViewPost;

public post(String webViewPost) {
        this.webViewPost = webViewPost;
    }

public postClass(){
}

public postClass(String TitleName, String descriptionName, String Picture) {
    this.titleName = TitleName;
    this.DescriptionName = descriptionName;
    this.Picture = picture;
}

public String gettitleName() {
    return titleName;
}

public void setTitleName(String TitleName) {
    this.titleName = TitleName;
}

public String getDescriptionName() {
    return DescriptionName;
}

public void setDescriptionName(String descriptionName) {
    this.DescriptionName = descriptionName;
}

public String getPicture() {
    return Picture;
}

public void setPicture(String picture) {
    this.Picture= picture;
}


public String getWebViewPost() {
        return webViewPost;
    }

    public void setWebViewPost(String webViewPost) {
        this.webViewPost = webViewPost;
    }

}

Crash report

 java.lang.RuntimeException: Unable to start activity ComponentInfo{hello.yellow/hello.yellow.webViewNews}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.webkit.WebSettings android.webkit.WebView.getSettings()' on a null object reference
                                                                                at hello.yellow.webViewNews.onCreate(webViewNews.java:29)
                                                                                at android.app.Activity.performCreate(Activity.java:6662)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
  • Yes, this is possible. On what specific step are you stuck? Are you having trouble adding the web view? Are you having problems reading from Firebase? Or do those already work and is the problem in gluing them together? – Frank van Puffelen Jul 10 '17 at 13:46
  • @FrankvanPuffelen i am having trouble with reading from Firebase and gluing them together, i want to change the URL from Firebase Console can you please help me out here –  Jul 10 '17 at 15:02
  • As it stands this question is too broad: it's really just a requirement. Show what you've already tried and where you are stuck. If you do that in the form of a [minimal, complete verifiable example](http://stackoverflow.com/help/mcve) (read the link, it's quite useful), you're most likely to get help. – Frank van Puffelen Jul 10 '17 at 15:19
  • @FrankvanPuffelen sorry for late post, i have added my class files of Firebase implemetation and the RecyclerAdapter Class, can you please help me implemeting the webView with this now. –  Jul 12 '17 at 20:21
  • What is the specific problem with the code you shared? When you step through it in a debugger, which part of the code doesn't do what you expect it to do? – Frank van Puffelen Jul 12 '17 at 20:30
  • @FrankvanPuffelen it all works good, but the problem is i cant glue webView and Firebase together from start, i have tried many times but it does not work, if you can just show me how to bind it with some example, i will be very thankful to you –  Jul 12 '17 at 20:40
  • @FrankvanPuffelen sir, i have implemented the webView in above code but it is giving me nullpoint exception and please can you tell me is this the right way to implement this. thanks in advance –  Jul 18 '17 at 11:01
  • NullPointerExceptions are typically easiest to fix by setting breakpoint on the offending line and troubleshooting from there. See https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Frank van Puffelen Jul 18 '17 at 13:11
  • @FrankvanPuffelen I used if else by if (webviewthis ==null){ webviewthis.getSettings().setJavaScriptEnabled(true); webviewthis.getSettings().setLoadsImagesAutomatically(true); webviewthis.loadUrl(mDatabaseReference.toString());} but it is again crashing on these lines, i have posted my crash report please check –  Jul 18 '17 at 18:44

1 Answers1

1

Yes. You can use do it by configuring remote config in firebase console.

For more info check this link https://firebase.google.com/docs/remote-config/use-config-android

Dhanumjay
  • 540
  • 7
  • 17
  • Actually I am trying to build a news app, where users can click on web link of the news and it will take them to that respective web page of in-app web browser, I don't think remote-config will be helpful in this purpose. –  Jul 10 '17 at 12:48