Mark Acquaisie Mark Acquaisie - 1 month ago 10
Android Question

How do I open a webview in another activity using editText for the url and a button click

MainActivity.java

public class MainActivity extends Activity {

WebView web1;
EditText ed1;
Button bt1;
String Address;
ProgressBar pbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

web1 = (WebView) findViewById(R.id.webView1);
ed1 = (EditText) findViewById(R.id.editText1);
bt1 = (Button) findViewById(R.id.button1);

pbar.setVisibility(View.GONE);

bt1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);


Address = "http://" + ed1.getText().toString();
WebSettings webSetting = web1.getSettings();
webSetting.setBuiltInZoomControls(true);
webSetting.setJavaScriptEnabled(true);
setContentView(R.layout.activity_main);


//below gives me this error stating new WebViewClient can not resolve.

web1.setWebViewClient(new WebViewClient());

web1.loadUrl(Address);

}
});
}
}


WebViewActivity.java

public class WebViewActivity extends Activity {


WebView web1;

String Address;
String add;
ProgressBar pbar;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);

web1 = (WebView)findViewById(R.id.webView1);

pbar = (ProgressBar)findViewById(R.id.progressBar1);
pbar.setVisibility(View.GONE);

web1.setWebViewClient(new WebViewActivity.WebViewClient());

}


public class WebViewClient extends android.webkit.WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {

// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
pbar.setVisibility(View.VISIBLE);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {

// TODO Auto-generated method stub

super.onPageFinished(view, url);
pbar.setVisibility(View.GONE);
}

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web1.canGoBack()) {
web1.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

Answer

Use Intent to pass the url as string to the webview activity:

Intent intent = new Intent(context, WebViewActivity.class);
intent.putExtra("URL",url)
startActivity(intent);

And Access it in WebViewActivity onCreate by:

Intent in = getIntent();
String url = in.getStringExtra("URL");

Now pass this url in

weview.loadurl(url);

MainActivity

public class MainActivity extends AppCompatActivity {

Button b1;
EditText ed1;

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

    b1 = (Button) findViewById(R.id.button);
    ed1 = (EditText) findViewById(R.id.editText);

    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String url = ed1.getText().toString();
            Intent intent = new Intent(getApplicationContext(), WebViewActivity.class);
            intent.putExtra("URL", url);
            startActivityForResult(intent, 1);

        }
    });
}


}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">

<EditText
    android:id="@+id/editText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="GO"/>

  </LinearLayout>

WebViewActivity

public class WebViewActivity extends AppCompatActivity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {

    String url = getIntent().getStringExtra("URL");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);
    webView = (WebView) findViewById(R.id.webView);
    webView.setWebViewClient(new MyBrowser());
    webView.getSettings().setLoadsImagesAutomatically(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    webView.loadUrl(url);
}

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}
}

webview.xml

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

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

</WebView>

</LinearLayout>

AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mywebviewapplication">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".WebViewActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
</application>

</manifest>
Comments