Yippyyyy!!!
Just successfully created my first Android App.
So I have been playing around recently with modifications, improvements and configurations on my current project – “Smart Home using Raspberry Pi & Android with Voice Recognition.”
I have been thinking of implementing a simple Android application which directly opens up URL, rather than going to my browser each and every time I need to access the web control server(Apache2).
So one might be wondering what is exactly this WebView ‘nonsense’ a simple Google search gave me this definition, A “webview” is a browser bundled inside of a mobile application producing what is called a hybrid app. Using a webview allows mobile apps to be built using Web technologies (HTML, JavaScript, CSS, etc.) but still package it as a native app and put it in the app store.
Now coming back to the App, i’m using the latest Android Studio, source code below.
Main_Activity.java
//__author__ = "Mpho Mphego" //__description__ = "Android WebView App controlling Raspberry Pi running Apache2" //__version__ = "Revision: 1.0 " //__date__ = "Date: 2015/01/17 22:23 " //__url__ = "mmphego.wordpress.com" //__copyright__ = "Copyright (c) 2015 Mpho Mphego" //__license__ = "Java" package com.wordpress.mpho112.mobilehomectrl; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.webkit.WebSettings; import android.webkit.WebView; public class MainActivity extends ActionBarActivity { private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.activity_main_webview); // Enable Javascript WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.loadUrl("http://172.18.20.41/"); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> tools:ignore="MergeRootFrame"> <WebView android:id="@+id/activity_main_webview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout>