2

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

3
  • use button onclick and call webview Commented Sep 23, 2015 at 11:49
  • 3
    See this, stackoverflow.com/questions/2563325/… Commented Sep 23, 2015 at 11:49
  • Already tried it, didn't work Commented Sep 23, 2015 at 13:27

3 Answers 3

8

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Android studio show "Error:(29, 39) error: cannot find symbol class OnClickListener" on build
import android.view.View.OnClickListener; in activity
My pleasure :). Please up vote to answer so it's help to other
1

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()"\");

Comments

0

You can reload your web view or you can use below code :

yourActivity.this.webView.loadUrl(url);

on your button click.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.