So you got a website? Great! If you are looking to convert it into an Android application, you have come to the right place. Since you are new to Android development, (I assume you are and that’s why you are here) I will try to be specific about every single thing. So let’s get started.
You need to set up the IDE and Android SDK to develop apps for Android. If you already have everything set up, move on. If not, follow this tutorial first.
Converting a website into Android application:
It’s very simple and straight forward. We will be making a simple app that displays a website using WebView. Let’s get going.
Open up ecipse and go to File >> New >> Other. Choose Android Application Project from the list.
Minimum required SDK should be set to API 8. It means that a user must be running Android 2.2 (froyo) or higher to run your app .Set the target sdk and compile with to 4.3.
Click finish. Eclipse will now generate some folders and files for your project and you will be taken to main.xml layout.
This is the graphical layout where you see an Android frame displaying Hello world! Click main.xml beside Graphical Layout. Remove TextView and initiazlie new WebView link this:
1
2
3
4
5
6
|
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebSettings.PluginState;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView myWebView;
@SuppressWarnings("deprecation")
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true); //Enables Javascript. Remove this line if your site doesn't require javascript
myWebView.getSettings().setPluginState(PluginState.ON); //Enables plugins like Adobe flash. Remove if not required
myWebView.loadUrl("http://samadmalik.com"); //Change this with your website's url
myWebView.setWebViewClient(new myWebViewClient());
}
private class myWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK){
if(myWebView.canGoBack()){
myWebView.goBack();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
|
1
|
<uses-permission android:name="android.permission.INTERNET" />
|
Now initialize Android emulator from virtual device manager to test your app. To do so, open Android Virtual Device Manager from Eclipse.
Click New. Fill out the fields as shown below and click ok to create your new android emulator.
That’s it. Now right click on your package and choose Run As >> Android Application. You should now see your app running on android emulator.