Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

( Android ) Webview refresh button

I'm trying to create a button to reload a Webview but ( sorry I'm new in android developement ) I didn't find a way to do it ( even in stackoverflow )

Here is the Mainactivity.java

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;

import static com.d4rkunicorn.partyhard.R.id.webView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("url");
    }

}

The webview.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/webview.xml">

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="REFRESH"
        android:id="@+id/button" />

 </RelativeLayout>

Thanks for helping

like image 936
k0spwn Avatar asked Dec 07 '25 07:12

k0spwn


2 Answers

In Activity class :

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Button;

import static com.d4rkunicorn.partyhard.R.id.webView;


public class MainActivity extends ActionBarActivity {

     WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = new WebView(this);
        setContentView(webview);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("url");

        Button button = (Button) findViewById(R.id.button);
        
        button.setOnClickListener(new OnClickListener() {
            
            public void onClick(View v) {
                
                webview.reload();
                
            }
        });
    }

}

In Layout XML :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:id="@+id/webview.xml">

<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

    <Button
         android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="REFRESH"
        android:id="@+id/button" />

 </RelativeLayout>

There many possible way to reload your webview:

  1. you can use webview.loadUrl( "javascript:window.location.reload( true )" ); inside any method/action event

  2. webview.loadUrl(webview.toString); //getting the webview URL and use to reload ur web page

  3. webview.loadUrl(url); // you can use your desire URL to reload your web page

  4. webview.loadUrl( "onclick=\"window.history.back()"\");

like image 45
Michael Avatar answered Dec 08 '25 21:12

Michael