Every App, nowadays, seems to need to be integrated with Facebook. One of our recent Android apps required such integration. The requirement was to allow the user to like a company Facebook page. Since this App had extremely tight dead lines there was no time to register the app with Facebook. The registration process creates a Facebook app and generates a App ID that is used to authenticate your Android app. Once you have the App ID you download the Facebook SDK for Android and start your Facebook integration. Because of the lack of time we decided not to register the app and implement our own solution that would allow a user to the like a company website.
Before you can like any thing on Facebook you have to log in. Hence the first step is you display the Facebook login screen. Here is the code that will create and display the Facebook login dialog fragment.
private void showDialog() { DialogFragment newFragment = new FacebookDialog(); newFragment.show(getFragmentManager(), "dialog"); } public class FacebookDialog extends DialogFragment { public FacebookDialog(){} @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NO_TITLE,android.R.style.Theme_Holo_Light); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { final Context context = getActivity(); getDialog().getWindow().setLayout(1000,600); wv = new WebView(context); wv.setScrollbarFadingEnabled(false); wv.setHorizontalScrollBarEnabled(false); wv.getSettings().setJavaScriptEnabled(true); wv.clearCache(true); wv.loadUrl("https://m.facebook.com/"); return wv; } }
The above code creates a dialog fragment. The fragment display a web view which loads the Facebook login page. The showDialog method creates and shows the dialog. This a basic setup. The dialog will popup and load the Facebook login page. Once you login you will have access to your account. The requirement was to like a company Facebook page. The user can now navigate to the company Facebook page and like it. Since the web view is in a dialog it is difficult to navigate and we want the process to be as fast as possible. Once we started to deal with the web view this became more of a web app issue than Android issue. First thing that we needed to do is to redirect to the company website after login so that the user can quickly like the company and log out and continue to use rest of the app. Here is an updated code that redirect to a company website after login.
wv.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("http://m.facebook.com/home.php")) { view.loadUrl("https://www.facebook.com/translucent.computing"); } else{ view.loadUrl(url); } return true; } });
We added a new web view client to the web view that intercepts all the web view load calls. We then looked for the Facebook home page and redirected to a company website. After you login to Facebook you get redirect to your home page since that page is not likely to change any time soon we conclude that it was reasonable to use the home page url as flag. Hence, after the user logs in, the next screen they will see is the company Facebook page. Where they can like the page and move one to the rest of the app. We also added a header to the dialog fragment with a Facebook logout button that will close the dialog as well. This solution allowed as to implement the required feature quickly and we did not have to register the app and use the Facebook SDK.
June 12th, 2012
by Patryk Golabek in Technology
⟵ Back
See more:
December 10th, 2021
Cloud Composer – Terraform Deploymentby Patryk Golabek in Data-Driven, Technology
December 2nd, 2021
Provision Kubernetes: Securing Virtual MachinesAugust 6th, 2023
The Critical Need for Application Modernization in SMEs