Skip to content

Instantly share code, notes, and snippets.

@chirag-v
Created March 31, 2013 17:17
Show Gist options
  • Save chirag-v/5281337 to your computer and use it in GitHub Desktop.
Save chirag-v/5281337 to your computer and use it in GitHub Desktop.
package com.myurl.myapp;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.webkit.ValueCallback;
import android.webkit.WebView;
import android.webkit.WebChromeClient;
import android.webkit.WebViewClient;
import android.provider.Settings;
public class MainActivity extends Activity {
private WebView wv;
//make HTML upload button work in Webview
private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE=1;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE)
{
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
//Check if user is online
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected()) {
return true;
}
return false;
}
//if not online show alertdialog with settings button
public void showNoConnectionDialog(Context ctx1) {
final Context ctx = ctx1;
AlertDialog.Builder builder = new AlertDialog.Builder(ctx);
builder.setCancelable(true);
builder.setMessage(R.string.no_connection);
builder.setTitle(R.string.no_connection_title);
builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
return;
}
});
builder.setCancelable(false);
builder.show();
}
public void onBackPressed (){
if (wv.isFocused() && wv.canGoBack()) {
wv.goBack();
}
else {
super.onBackPressed();
finish();
}
}
//catching back key to take back if history exists doesnot finish Activity
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && wv.canGoBack()) {
wv.goBack();
return true;
}
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
//supress lint coz eclipse cries over javascript enabled
@SuppressLint("SetJavaScriptEnabled") @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//if not online then show NO CONNECTION Dialog
if(!isOnline()) {
showNoConnectionDialog(MainActivity.this);
}
//webview
wv = new WebView(this);
//loadurl
wv.loadUrl(getString(R.string.siteurl));
//Enable JavaScript
wv.getSettings().setJavaScriptEnabled(true);
//Zoom
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setSupportZoom(true);
//progressdialog
final ProgressDialog pd = ProgressDialog.show(this, "", getString(R.string.loading), true);
pd.setCanceledOnTouchOutside(false);
wv.setWebViewClient(new WebViewClient(){
//hide progressdialog when page loads completely
@Override
public void onPageFinished(WebView view, String url) {
if(pd.isShowing() && pd!=null)
{
pd.dismiss();
}
}
});
wv.setWebChromeClient(new WebChromeClient() {
// For Android 3.0+
public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, getString(R.string.fileselect) ), MainActivity.FILECHOOSER_RESULTCODE );
}
// For Android < 3.0
public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
openFileChooser( uploadMsg, "" );
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
openFileChooser( uploadMsg, "" );
}
});
setContentView(wv);
}
}
@drynkme
Copy link

drynkme commented Feb 14, 2014

is this working for webview the gallery and camera?

many thanks,
fernando

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment