How to use Webview without showing Url in android.

In this Article we create how to use Webview without showing url in android.

WebView is a view that display web page in android. WebView makes turns your application to a web application.  It is used to display online content in android activity.

Step 1: Firstly, we create a code in XML file for Webview.

//Code for Xml file:

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

<WebView
android:id="@+id/myweb"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_weight="1">
</WebView>

</LinearLayout>

 

Step 2: Now, See the .java file.
//WebActivity.java file:

package com.example.thakur.itbunch;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebActivity extends Activity {

WebView myweb;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
myweb= (WebView) findViewById(R.id.web);
myweb.setWebViewClient(new myWebClient());
myweb.getSettings().setJavaScriptEnabled(true);
myweb.loadUrl("http://www.itbunch.com/");
}

private class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}

/* @Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub

view.loadUrl(url);
return true;

}*/
}
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && myweb.canGoBack()) {
myweb.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

 

Step 3: Now, we have to take Permission of AndroidManifest.xml file.

//Code for take permission

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

 

//AndroidManifest.xml file.

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

<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=".WebActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

</manifest>

 

I hope you like this Article.

Tags: , ,