CLICK2CODE

first you learn then you earn

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”.

Leave a Reply

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