ad728

Saturday, March 11, 2023

AIDE Tutorial - 20.4 WebView with SwipeRefreshLayout



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:id="@+id/container"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".Browser">


    <android.support.v4.widget.SwipeRefreshLayout

              android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:id="@+id/swipe">


        <android.support.v4.widget.NestedScrollView

            android:layout_width="match_parent"

            android:layout_height="match_parent">


            <WebView

                android:id="@+id/wv"

                android:layout_marginTop="10dp"

                   android:layout_width="match_parent"

                android:layout_height="match_parent">

            </WebView>

        </android.support.v4.widget.NestedScrollView>

    </android.support.v4.widget.SwipeRefreshLayout>

</RelativeLayout>


MainActivity.java

package com.mycompany.myapp2;

import android.app.*;
import android.os.*;
import android.webkit.*;
import android.widget.*;
import android.support.v4.widget.*;
import android.view.*;
import android.content.*;

public class MainActivity extends Activity
{WebView wv;
 
    SwipeRefreshLayout swipe;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
wv=(WebView)findViewById(R.id.wv);
        swipe = (SwipeRefreshLayout)findViewById(R.id.swipe);

     
   
        wv.loadUrl("https://shyamkumarkc20.com.np");
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
swipe.setRefreshing(true);
        wv.setWebViewClient(new WebViewClient(){
public void onPageFinished (WebView view, String url){
swipe.setRefreshing(false);
}
});
swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
wv.reload();
}





});
}
@Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if(wv.canGoBack()) {
            wv.goBack();
        } else {


AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Are you sure you want to exit?");
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
MainActivity.this.finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
AlertDialog alert =builder.create();
alert.show();
}
}


}


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

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

Share:

Total Pageviews

Sponsor

Sponsor

ad300