Step 1 ) Now open main.xml file and insert ProgressBar activity.
<ProgressBar
android:id="@+id/pb"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="@+id/wp">
<WebView
android:id="@+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="@+id/pb"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
MainActivity.java
package com.w4apk.webview;
import android.app.*;
import android.os.*;
import android.webkit.*;
import android.widget.*;
import android.view.*;
import android.content.*;
public class MainActivity extends Activity
{
private WebView web;
private FrameLayout wp;
private ProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState)
{
ActionBar ab=getActionBar();
ab.hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web=(WebView)findViewById(R.id.web);
wp=(FrameLayout)findViewById(R.id.wp);
pb=(ProgressBar)findViewById(R.id.pb);
WebSettings WebSettings= web.getSettings();
WebSettings.setJavaScriptEnabled(true);
web.loadUrl("http://w4apk.blogspot.com");
web.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView View, String url){
wp.removeView(pb);
}
});
}
@Override
public void onBackPressed()
{
if(web.canGoBack()){
web.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();
}
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.w4apk.webview" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Most welcome
ReplyDelete