While working on an Android app, I needed to get people to sign into Facebook however I didn’t want them to leave my app. The simple solution was to launch the browser through an intent; however, this was going to stop my activity which was unacceptable. I wanted a solution that enabled users to sign into the Facebook platform from inside the app. I had to embed a webview inside a dialog and pop up the required webview. How? Read on
Create the Activity that pops up the dialog
public class WebviewActivity extends Activity { private static final String REGISTRATION_URL = "http://10.0.2.2/test.php"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.blog_learning); new blog(this, REGISTRATION_URL, new DialogListener() { @Override public void onComplete(JSONObject values) { // Do processing here with the JSONObject, you can change it if you so desire Log.d("Oncomplete","called"); } @Override public void onError(DialogError e) {} @Override public void onStop() {} }).show(); } }
Create a Listener interface that links your Activity to the Dialog
public interface DialogListener { public void onComplete(JSONObject values); public void onError(); public void onStop(); }
The listener provides a callback pathway that monitor the state of the dialog; you can use it to get responses, check errors and do similar activities.
The Dialog class
Also, one thing, the Layouts and buttons are declared programmatically in the code, I couldn’t find a way to automatically load layout files in the onCreate method of the Dialog. If you know how to do this, kindly explain in the comments. Making a call to shouldOverrideUrlLoading in the WebViewClient doesn’t seem to work either, so you should do your processing in onPageFinished. Here is the code:
public class blog extends Dialog { static final FrameLayout.LayoutParams FILL = new FrameLayout.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); private static final String RESULT_URL = "XXX.php"; // Url for retrieving results private String mUrl; //URL you are making a request to private DialogListener mListener; private ProgressDialog mSpinner; private Button mButton; private WebView mWebView; private LinearLayout mWebViewContainer; public blog(Context context, String url, DialogListener listener) { super(context, android.R.style.Theme_NoTitleBar); mUrl = url; mListener = listener; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mSpinner = new ProgressDialog(getContext()); mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE); mSpinner.setMessage("Loading..."); requestWindowFeature(Window.FEATURE_NO_TITLE); mButton = new Button(getContext()); mButton.setText("Close"); mWebViewContainer = new LinearLayout(getContext()); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mListener.onStop(); blog.this.dismiss(); } }); //Close button should only become visible after the webview is fully loaded mButton.setVisibility(View.INVISIBLE); mWebView = new WebView(getContext()); mWebView.setVerticalScrollBarEnabled(false); mWebView.setHorizontalScrollBarEnabled(false); mWebView.setWebViewClient(new blog.blogWebViewClient()); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl(mUrl); mWebView.setVisibility(View.INVISIBLE); mWebViewContainer.addView(mButton); mWebViewContainer.addView(mWebView); addContentView( mWebViewContainer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT) ); } private class blogWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return false; } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); mListener.onError(new DialogError(description, errorCode,failingUrl)); blog.this.dismiss(); } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); mSpinner.show(); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); mSpinner.dismiss(); mWebViewContainer.setBackgroundColor(Color.TRANSPARENT); mWebView.setVisibility(View.VISIBLE); mButton.setVisibility(View.VISIBLE); //Do result processing here if (url.contains(RESULT_URL)) { JSONObject obj = null; try { URL json = new URL(url); } catch (IOException e) { e.printStackTrace(); } mListener.onComplete(obj); blog.this.dismiss(); } } } }
For more information on this, check this:
- Facebook Android SDK
- Download the complete code as an Eclipse project here
Related articles
- Loading a URL in a webview window (stackoverflow.com)