To create side menu bar with side gesture on webview.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:layerType="software" />
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</TableRow>
</TableLayout>
public class MainActivity extends Activity {
private String[] menu;
private Integer[] icons;
private DrawerLayout dLayout;
private ListView dList;
private CustomList adapter;
private String url1 = "http://www.yahoo.com/";
private String url2 = "http://www.youtube.com/";
private String url3 = "https://9gag.com/";
private String url;
private ProgressDialog pDialog;
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = new String[] { "", "", "", "" }; // add text if you need in menu
icons = new Integer[] { R.drawable.yahoo, R.drawable.youtube_log,
R.drawable.ninegag, R.drawable.exit_logo };
view = (WebView) findViewById(R.id.webView1);
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
new BullpenClass().execute();
adapter = new CustomList(this, menu, icons);
dList.setAdapter(adapter);
dList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
dLayout.closeDrawers();
if (position == 0) {
url = url1;
} else if (position == 1) {
url = url2;
} else if (position == 2) {
url = url3;
} else if (position == 3) {
finish();
System.exit(0);
} else {
url = url1;
}
Bundle args = new Bundle();
args.putString("URL", url);
Fragment detail = new WebviewActivity();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail).commit();
}
});
}
class BullpenClass extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Your Internet is Too Slow..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating user
* */
protected String doInBackground(String... args) {
// Building Parameters
Bundle bundle = new Bundle();
bundle.putString("URL", url1);
Fragment detail = new WebviewActivity();
detail.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail).commit();
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
// One webview back button will load the previous page
public void onBackPressed() {
view = (WebView) findViewById(R.id.webView1);
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
}
public class WebviewActivity extends Fragment {
// TextView text;
private WebView webview;
private String URL;
private Context ctx;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle args) {
View view = inflater.inflate(R.layout.webviewactivity, container,
false);
// add context for fragment.
ctx = container.getContext();
getInitialValues(view);
setWebCondition();
startWebView(webview, URL);
return view;
}
private void getInitialValues(View view) {
// TODO Auto-generated method stub
URL = getArguments().getString("URL");
webview = (WebView) view.findViewById(R.id.webView1);
}
private void startWebView(WebView view, String url) {
// TODO Auto-generated method stub
view.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
// If you will not use this method url links are open in new brower
// not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != "outofapp") {
view.loadUrl(url);
return true;
}
return false;
}
public void onLoadResource(WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
});
view.loadUrl(url);
}
// Webview settings
private void setWebCondition() {
// TODO Auto-generated method stub
WebSettings settings = webview.getSettings();
settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
settings.setSupportMultipleWindows(false);
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
webview.requestFocusFromTouch();
}
}
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.sidemenulist, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.sidemenulist, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sidebarmenuwebview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<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>
</manifest>
1. activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:layerType="software" />
</android.support.v4.widget.DrawerLayout>
2. webviewactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
3. sidemenulist.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</TableRow>
</TableLayout>
4. MainActivity.java
public class MainActivity extends Activity {
private String[] menu;
private Integer[] icons;
private DrawerLayout dLayout;
private ListView dList;
private CustomList adapter;
private String url1 = "http://www.yahoo.com/";
private String url2 = "http://www.youtube.com/";
private String url3 = "https://9gag.com/";
private String url;
private ProgressDialog pDialog;
private WebView view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
menu = new String[] { "", "", "", "" }; // add text if you need in menu
icons = new Integer[] { R.drawable.yahoo, R.drawable.youtube_log,
R.drawable.ninegag, R.drawable.exit_logo };
view = (WebView) findViewById(R.id.webView1);
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
dList = (ListView) findViewById(R.id.left_drawer);
new BullpenClass().execute();
adapter = new CustomList(this, menu, icons);
dList.setAdapter(adapter);
dList.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View v, int position,
long id) {
dLayout.closeDrawers();
if (position == 0) {
url = url1;
} else if (position == 1) {
url = url2;
} else if (position == 2) {
url = url3;
} else if (position == 3) {
finish();
System.exit(0);
} else {
url = url1;
}
Bundle args = new Bundle();
args.putString("URL", url);
Fragment detail = new WebviewActivity();
detail.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail).commit();
}
});
}
class BullpenClass extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Your Internet is Too Slow..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating user
* */
protected String doInBackground(String... args) {
// Building Parameters
Bundle bundle = new Bundle();
bundle.putString("URL", url1);
Fragment detail = new WebviewActivity();
detail.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, detail).commit();
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
@Override
// One webview back button will load the previous page
public void onBackPressed() {
view = (WebView) findViewById(R.id.webView1);
if (view.canGoBack()) {
view.goBack();
} else {
super.onBackPressed();
}
}
}
5. WebviewActivity.java
public class WebviewActivity extends Fragment {
// TextView text;
private WebView webview;
private String URL;
private Context ctx;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle args) {
View view = inflater.inflate(R.layout.webviewactivity, container,
false);
// add context for fragment.
ctx = container.getContext();
getInitialValues(view);
setWebCondition();
startWebView(webview, URL);
return view;
}
private void getInitialValues(View view) {
// TODO Auto-generated method stub
URL = getArguments().getString("URL");
webview = (WebView) view.findViewById(R.id.webView1);
}
private void startWebView(WebView view, String url) {
// TODO Auto-generated method stub
view.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;
// If you will not use this method url links are open in new brower
// not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != "outofapp") {
view.loadUrl(url);
return true;
}
return false;
}
public void onLoadResource(WebView view, String url) {
if (progressDialog == null) {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressDialog.dismiss();
}
});
view.loadUrl(url);
}
// Webview settings
private void setWebCondition() {
// TODO Auto-generated method stub
WebSettings settings = webview.getSettings();
settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
settings.setSupportMultipleWindows(false);
settings.setJavaScriptEnabled(true);
settings.setLoadWithOverviewMode(true);
settings.setUseWideViewPort(true);
webview.setWebViewClient(new WebViewClient());
webview.setWebChromeClient(new WebChromeClient());
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setBuiltInZoomControls(true);
webview.requestFocusFromTouch();
}
}
6. CustomList.java
public class CustomList extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public CustomList(Activity context, String[] web, Integer[] imageId) {
super(context, R.layout.sidemenulist, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.sidemenulist, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
7. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sidebarmenuwebview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<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>
</manifest>
8. Action Points.
- minSdkVersion should be over 11.
- Internet permission is mandatory.
- menu icons need to add under drawable folder.
No comments:
Post a Comment