How to use firebase in webview android studio

 

WebView And Firebase :

In this blog I will talking about WebView and Firebase. The WebView allows the programmer to write the bulk of the application using HTML, JavaScript and CSS, the standard Web programming tools.
Now, I show you How to change url from firebase in Android Webview 2022.

How to change an URL for webview dynamically using firebase ?

Frist you add some permission like - Internet Permission, usesCleartextTraffic etc. if you don't know how to to add. please following this articale - https://www.bdtopcoder.xyz/2022/07/webview-with-stylish-progress-bar.html
once it is done follow this steps.

Step 1.

Frist you Create project in firebase. then you connect firebase with android studio. if you don't know how to connect please watch my video.
 

I hope you can do this successfully. if any problem you comments below.

Step 2.

Go to the activity_main.xml file and refer to the following code.
Below is the code for the activity_main.xml file.

activity_main.xml
<?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"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/AppBar"
        android:background="#2196F3"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:orientation="vertical"
        >
        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            style="@style/CardView"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            app:cardCornerRadius="20dp"
            >
            <LinearLayout
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:layout_gravity="center"
                android:gravity="center"
                >
                <TextView
                    android:id="@+id/url"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="@string/app_name"
                    />
            </LinearLayout>
            
        </androidx.cardview.widget.CardView>

    </LinearLayout>

    <WebView
        android:layout_below="@id/AppBar"
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>
 

Step 3.

Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.

MainActivity.java
public class MainActivity extends AppCompatActivity {

    WebView webView;
    TextView url;
    private FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    private DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
    private DatabaseReference childreference = reference.child("url");
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);
        url = findViewById(R.id.url);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());

    } // OnCreate Method close here =================
    
    @Override
    protected void onStart() {
        super.onStart();
        childreference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {

                String message = snapshot.getValue(String.class);
                url.setText(message);
                webView.loadUrl(message);

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

    }
} // Public class close here ======================
Next Post Previous Post
No Comment
Add Comment
comment url