Translate

Wednesday, January 9, 2013

Android App Development 101 5th tutorial; Android and Web, Turn your Web Site into an Android App!

In this tutorial, I will be using Android WebView to display my blog in our android app. Web View is very straight forward and offer several features. We will be looking at few of them. At first we need to make a layout in xml with our WebView object, then we will load our url into the layout.

activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
 android:orientation="vertical"
 >

    <WebView
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
      />

</LinearLayout>


MainActivity.java


package com.example.webview;

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
WebView ourwebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadWeb();
}

public void loadWeb() {
WebView webView = (WebView)findViewById(R.id.webview);
ourwebView=webView;
this.setTitle("Android Happenings");
ourwebView.setWebViewClient(new WebViewClient());
String url="http://androidhappenings.blogspot.com/";
ourwebView.canGoBack();
ourwebView.loadUrl(url);

}


public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK && ourwebView.canGoBack()) {
ourwebView.goBack();
return true;

}
return super.onKeyDown(keyCode, event);
}


}

As we can see in the above java code, the onKeyDown method checks for back presses and move our web view one page back in each back press whenever possible. Also make sure that you add internet permission in your manifest or it will display web page not found error.
<uses-permission android:name="android.permission.INTERNET" />



Sunday, January 6, 2013

Android App Development 101 4th tutorial; Developing Android StopWatch/Timer

In this tutorial, i will be making a stop watch for android. We will create a text view to display the time elapsed and two buttons one to start the timer and second to stop the timer. We will be using more xml attributes and new java concepts such as runnables and handlers. In addition we will be using android Timer class. Here is our layout:

main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

      <TextView
        android:id="@+id/androidtimer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Android Timer"
        android:textSize="40sp"
        android:textStyle="bold"
        android:textColor="#FF0000"/>
   
    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="bottom|center" >
    <Button
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text=" start "
        android:textSize="35sp"
        android:textColor="#FFFFFF"
        android:onClick="startTimer"
/>
    <View
        android:layout_width="20dp"
        android:layout_height="1dp"/>
       <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text=" stop "
        android:textSize="35sp"
        android:textColor="#FFFFFF"
        android:onClick="stopTimer"
       
/>
       </LinearLayout>
</LinearLayout>

The View objects are used just to create some space between two buttons. Also you can see that we have used a new linear layout within a linear layout to display two buttons in a horizontal row.

TimerActivity.java


package com.example.timer;

import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;

public class TimerActivity extends Activity {
TimerTask timerTask;
int n=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void startTimer(View view) {
final Handler handler = new Handler();
Timer ourtimer = new Timer();
timerTask = new TimerTask() {
       public void run() {
               handler.post(new Runnable() {
                       public void run() {
                  TextView timer = (TextView)findViewById(R.id.androidtimer);
               
                  timer.setText(n + " Seconds");
                      n++;
                       }
              });
       }};


   ourtimer.schedule(timerTask, 0, 1000);

}
public void stopTimer(View view) {
timerTask.cancel();
timerTask=null;
n=0;
}

}