WebView with Custom
Header in Android with Loading Progress
Step 1: Create header_before_login .xml file and include it into webview.xml file,
so that you can use that header by including into other xml files. (Use proper
img for background)
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar_bg " >
<ImageView
android:id="@+id/ivHeaderBLLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:src="@drawable/back_arrow" />
<LinearLayout
android:id="@+id/ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/imgHeaderBLTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvHeaderBLTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/white"
android:textSize="17dip" />
</LinearLayout>
<Button
android:id="@+id/btnHeaderBLRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:background="@drawable/continue_btn" />
</RelativeLayout>
</merge>
Step 2: Now create
webview.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:background="@drawable/back_lightbg"
android:orientation="vertical" >
<include
android:id="@+id/RegHeader"
layout="@layout/header_before_login" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/dd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<WebView
android:id="@+id/wvcmspage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip" />
<ProgressBar
android:id="@+id/pB1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="15dip"
android:padding="2dip" >
</ProgressBar>
</LinearLayout>
</ScrollView>
</LinearLayout>
Step:3 Now Create
Activity
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class WebView extends Activity{
//Header
private Button btnHeaderBLRight;
private ImageView ivHeaderBLLeft;
private TextView tvHeaderBLTitle;
private WebView wvcmspages;
private ProgressDialog m_ProgressDialog = null;
private String strFromClassName;
private String strURL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
overridePendingTransition( R.anim.push_down_in, 0
);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.webview);
m_ProgressDialog = ProgressDialog.show(WebView.this, "Please
wait..", "Retrieving data ...", true);
m_ProgressDialog.setContentView(R.layout.progress);
final ProgressBar Pbar;
Pbar = (ProgressBar) findViewById(R.id.pB1);
wvcmspages = (WebView) findViewById(R.id.wvcmspage);
wvcmspages.setBackgroundColor(0x00000000);
wvcmspages.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView
view, int progress) {
if ((progress < 100) && (Pbar.getVisibility() == ProgressBar.GONE)) {
Pbar.setVisibility(ProgressBar.VISIBLE);
}
Pbar.setProgress(progress);
if (progress == 100) {
Pbar.setVisibility(ProgressBar.GONE);
m_ProgressDialog.dismiss();
}
}
});
SetHeader();
strURL = “YOUR URL TO LOAD”;
wvcmspages.loadUrl(strURL);
}
public void SetHeader() {
//Header
ivHeaderBLLeft = (ImageView) findViewById(R.id.ivHeaderBLLeft);
btnHeaderBLRight = (Button) findViewById(R.id.btnHeaderBLRight);
tvHeaderBLTitle = (TextView) findViewById(R.id.tvHeaderBLTitle);
tvHeaderBLTitle.setText("My WebView");
btnHeaderBLRight.setVisibility(View.GONE);
ivHeaderBLLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
No comments:
Post a Comment