简体   繁体   中英

How to open a webpage in a webView from a fragment with cardView on click event?

I am new to android development so if any thing wrong in my code approach please correct me.

I wanted to a open webView with specific urls when we click on a cardview from fragment. My all cardview in fragment1_layout.xml

  1. A layout file with more than one cardview.
  2. Each cardview click loads a different url in a webview.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="17dp"
            android:alignmentMode="alignMargins"
            android:columnCount="2"
            android:columnOrderPreserved="false"
            android:rowCount="1">


            <androidx.cardview.widget.CardView
                android:id="@+id/newsCardView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_margin="12dp"
                android:elevation="60dp"
                app:cardCornerRadius="12dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    android:padding="16dp">

                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:clickable="true"
                        android:src="@mipmap/ic_launcher_round" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Card One"
                        android:textColor="@color/black"
                        android:textSize="13dp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>

            <androidx.cardview.widget.CardView
                android:id="@+id/newsCardView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_rowWeight="1"
                android:layout_columnWeight="1"
                android:layout_margin="12dp"
                android:elevation="60dp"
                app:cardCornerRadius="12dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    android:padding="16dp">

                    <ImageView
                        android:layout_width="100dp"
                        android:layout_height="50dp"
                        android:layout_gravity="center"
                        android:clickable="true"
                        android:src="@mipmap/ic_launcher_round" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:text="Card Two"
                        android:textColor="@color/black"
                        android:textSize="13dp" />
                </LinearLayout>
            </androidx.cardview.widget.CardView>
        </GridLayout>
    </LinearLayout>
</ScrollView>

I Have 2 cardview and i wanted to open a different web page each time on the click of a cardview? This is my Fragment1.java

package com.example.readnews;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {
    public CardView cardView1, cardView2;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment1_layout, container, false);

        cardView1 = rootView.findViewById(R.id.newsCardView1);
        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Some Code To Open  1st Url in webView

            }
        });

        cardView2 = rootView.findViewById(R.id.newsCardView2);
        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Some Code To Open 2nd Specific Url in webView
            }
        });
        return rootView;
    }
}

Open All URLs Webpage in activity_web_viewer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
</RelativeLayout>

You can pass the URL of your webpage to your webViewActivity by Intent .

@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment1_layout, container, false);

       Intent myIntent = new Intent(this, WebViewActivityName.class); //WebViewActivityName is the  activity name of the webview activity 
       

        cardView1 = rootView.findViewById(R.id.newsCardView1);
        cardView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//               Add these 
                     myIntent.putExtra("url", "yourUrlHere"); //Add your url in "yourUrlHere"
                    requireActivity().startActivity(myIntent);

            }
        });

        cardView2 = rootView.findViewById(R.id.newsCardView2);
        cardView2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myIntent.putExtra("url", "yourUrlHere");//Add your url in "yourUrlHere"
                    requireActivity().startActivity(myIntent);
            }
        });
        return rootView;
    }

In your wevViewActivity,get the url like this::

@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String url= intent.getStringExtra("url"); //this is your URL string pass from the previous activity.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM