Networking: Part 1 (Web Content)
Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine used by Google Chrome. –Versions of Android prior to 4.4 (KitKat) were based on the WebKit open source web browser engine A view class, WebView, that can be used within an activity for displaying web content. Like the full-featured web browser, WebView also uses Chromium to render the web content. Standard Java features in package java.net for network access using TCP/IP sockets. Slide 2©SoftMoore Consulting
Launching an Internet Browser An Internet browser can be launched to display a web page by starting an activity with an intent whose action is ACTION_VIEW and whose data that is in the form of a URL. Uri uri = Uri.parse(" Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); If the URL is entered by the user as part of the application, then the following soft keyboard options should be used for the EditText field: –android:inputType="textUri" (provides ".com" and "/") –android:imeOptions="actionGo" (provides a "Go" button) Slide 3©SoftMoore Consulting
Class WebView Class WebView (in package android.webkit ) provides the ability to view web content within a portion of an activity’s screen. By default, some browser-related features such as JavaScript are disabled, but it is possible for an application to enable them when the view is created. In order to access the Internet, the following permission must be added to AndroidManifest.xml before the tag. Slide 4©SoftMoore Consulting
Defining a WebView in the Layout File (e.g., in activity_main.xml ) <WebView xmlns:android=" xmlns:tools=" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" /> Slide 5©SoftMoore Consulting Note: A web view can be the root element for the xml file; i.e., it does not have to be nested within a layout element. If defined as the root element, it must include the namespace (“ xmlns ”) attribute.
Loading a URL private WebView public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); webView = (WebView) findViewById(R.id.webview); // enable JavaScript webView.getSettings().setJavaScriptEnabled(true); // force links to open in WebView instead of browser webView.setWebViewClient(new WebViewClient()); webView.loadUrl(" } Slide 6©SoftMoore Consulting
Example: WebView Slide 7©SoftMoore Consulting
Loading HTML Defined Within the Application private static final String HELLO_HTML = " Hello, Android! ";... public void onCreate(Bundle savedInstanceState) {... webView.loadData(HELLO_HTML, "text/html", "utf-8"); } Slide 8©SoftMoore Consulting
Enhancing a WebView: Navigating Back to a Previous Page To allow navigation back to a previous pages, handle the “back” button on the device so that it will return to the previous page rather than exit the public void onBackPressed() { if(webView.canGoBack()) webView.goBack(); else super.onBackPressed(); } Slide 9©SoftMoore Consulting Note: Older guides/tutorials might recommend using the onKeyDown() method and checking for KEYCODE_BACK, but the above is the preferred way to handle the “back” button.
Relevant Links Building Web Apps in WebView Getting Started: WebView-based Applications for Web Developers Android WebView Tutorial Slide 10©SoftMoore Consulting