How to create web view with firebase | How to operate web view with firebase | How to open web view using firebase

18 Comments


Step: 1

Go to Manifests.xml file and give Internet Permission. The code is given below.

<uses-permission android:name="android.permission.INTERNET" />

Step: 2

Connect your project with a firebase with Android Studio. Go to “TOOLS” and click “Firebase” and click “Realtime Database”. Here you can follow two-step.

  1. Connect your app to Firebase.
  2. Add the Realtime Database to your app.

Then click “SYNC NOW” into your project.

Step: 3

Now open activity.xml and paste the below code.

NOTE: COPY ALL CODE AND FIRST YOU DELETE ALL CODE IN YOUR ACTIVITY.XML FILE THEN YOU CAN PASTE BELOW CODE.
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="horizontal"
        android:weightSum="1">

        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />

        <androidx.cardview.widget.CardView
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_weight=".8"
            android:padding="4dp"
            app:cardBackgroundColor="@android:color/white"
            app:cardCornerRadius="100dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:id="@+id/webURL"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="5dp"
                    android:text="Sample URL"
                    android:textAppearance="@style/TextAppearance.AppCompat.Medium"
                    android:textColor="#808080" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
        <View
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />
    </LinearLayout>
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Step: 4

Go to MainActivity.java and insert below code under “Public Class MainActivity”.

private WebView webView;
private TextView msgURL;
private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
private DatabaseReference reference = firebaseDatabase.getReference();
private DatabaseReference childReference = reference.child("url");

Step: 5

Under “protected void onCreate” paste given below code. DO NOT MISS ANY CODE OTHERWISE YOUR APP HAS BEEN CRASHED.

msgURL = findViewById(R.id.webURL);
webView = findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());

Step: 6

Now you can copy “public void onBackPressed” code given below and paste out of “protected void onCreate” method.

@Override
public void onBackPressed() {
    if (webView.canGoBack()) {
        webView.goBack();
    } else {
        super.onBackPressed();
    }
}

Step: 7

After that, you can copy “protected void onStart” code and paste out of “protected void onCreate” method.

@Override
protected void onStart() {
    super.onStart();
    childReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            String message = dataSnapshot.getValue(String.class);
            msgURL.setText(message);
            webView.loadUrl(message);
        }
        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
        }
    });
}

Step: 8

Go to project settings and select General (show default always) scroll up and see your app package name, and right side available “google-services.json”. You can download a file and save in your computer and Now go to the android studio project and open with project mode under the app folder you can paste “json” file.

Step: 9

Now go to https://console.firebase.google.com/ and open your project and open Database on your left sidebar and select lock mode then go to Rules and replace “false” to “true” both places.

And back on Data Tab and click (+) PLUS sign and type Name “url” and Value “ you can paste your website URL whatever you want to show in your android application”.

share this

18 thoughts on “How to create web view with firebase | How to operate web view with firebase | How to open web view using firebase”

  1. Hi there, I do believe your site might be having web browser compatibility problems.
    When I look at your site in Safari, it looks fine however, when opening in IE, it has some overlapping issues.
    I just wanted to give you a quick heads up! Other than that, wonderful website!

    fotballdrakter barn

  2. Wonderful article! That is the type of information that should
    be shared across the net. Disgrace on Google for now not positioning this publish higher!

    Come on over and talk over with my web site . Thank you =) Maglie real madrid

  3. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now
    each time a comment is added I get several emails with the same comment.
    Is there any way you can remove me from that service?

    Appreciate it!
    Spanien tröja

  4. A fascinating discussion is worth comment. I do believe that you need to publish more about this topic,
    it might not be a taboo matter but generally people do not speak about such
    issues. To the next! All the best!!
    Fotbollströjor

  5. This is really interesting, You are a very skilled blogger.
    I’ve joined your feed and look forward to seeking
    more of your fantastic post. Also, I have shared your website
    in my social networks!
    Fotbollskläder

Leave a Reply

Your email address will not be published. Required fields are marked *