Wednesday, March 5, 2014

Android Toast Example With Source Code

In Android, Toast is a notification message that pop up, display a certain amount of time, and automtaically fades in and out, most people just use it for debugging purpose.
Code snippets to create a Toast message :
//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
 
//display in long period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_LONG).show();

1.  Toast View

Simple Toast example.
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


<Button android:id="@+id/button1" android:layout_marginLeft="100dp" android:layout_marginTop="100dp" android:layout_height="wrap_content" android:text="Click Me" android:layout_width="wrap_content"></Button>
</LinearLayout>
File : MainActivity.java
package com.helloexample;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class HelloAndroidActivity extends Activity implements  OnClickListener{
 
 private Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
        
    }
    public void onClick(View v) {
  Intent i;
  switch (v.getId()) {
  case R.id.button1:
   Toast.makeText(getApplicationContext(), "Hello Android", Toast.LENGTH_LONG).show();
   
   
   break;
  
  default:
   break;
  }
 }

}
See demo, when a button is clicked, display a  Toast message

Android Activity Demo With Source Code

Android Activity – From One Screen To Another Screen

In Android, an activity is represent a single screen. Most applications have multiple activities to represent different screens, for example, one activity to display a list of the application settings, another activity to display the application status.
In this tutorial, we show you how to interact with activity, when a button is clicked, navigate from current screen (current activity) to another screen (another activity).

1. XML Layouts

Create following two XML layout files in “res/layout/” folder :
  1. res/layout/main.xml – Represent screen 1
  2. res/layout/main2.xml – Represent screen 2
File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="This is First Screen" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
<Button android:id="@+id/button1" android:layout_marginLeft="100dp" android:layout_marginTop="100dp" android:layout_height="wrap_content" android:text="Click Me" android:layout_width="wrap_content"></Button>
</LinearLayout>  
File : res/layout/main2.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:text="This is Second Screen" android:layout_width="wrap_content" android:id="@+id/textView1"></TextView>
    
</LinearLayout>

2. Activities

Create two activity classes :
  1. HelloAndroidActivity.java –> main.xml
  2. Button2.java –> main2.xml
To navigate from one screen to another screen, use following code :
    Intent intent = new Intent(context, anotherActivity.class);
    startActivity(intent);
File : HelloAndroidActivity.java
package com.helloexample;




import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class HelloAndroidActivity extends Activity implements  OnClickListener{
 
 private Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button)findViewById(R.id.button1);
        btn.setOnClickListener(this);
        
    }
    public void onClick(View v) {
  Intent i;
  switch (v.getId()) {
  case R.id.button1:
   i = new Intent(HelloAndroidActivity.this, Button2.class);
   
   startActivity(i);
   break;
  
  default:
   break;
  }
 }

}
File : Button2.java
package com.helloexample;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class Button2 extends Activity {
 
 private Button btn;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
       
    }
    }

3. AndroidManifest.xml

Declares above two activity classes in AndroidManifest.xml.
File : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.helloexample"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloAndroidActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
         <activity android:name=".Button2"
                  android:label="@string/app_name">
            
        </activity>
        

    </application>
</manifest>

4. Demo

Run application.
AppActivity.java (main.xml) screen is display.
When above button is clicked, it will navigate to another screen  (main2.xml).



Tuesday, March 4, 2014

Hello Android Demo With Source Code


Android Tutorial

Android, an open source operating system for mobile devices (Smartphone and tablet), led by Google. The Android SDK provides a set of tools and APIs to develop Android applications, using Java. So, if you know Java, Android programming is easy :)
In this series of tutorials, we show you the list of basic tutorials to get you start program Android easily.

Android Hello World Example

In this tutorial, we show you how to create a simple “hello world” Android project in Eclipse IDE + ADT plugin, and run it with Android Virtual Device (AVD). The Eclipse ADT plugin provided easy Android project creation and management, components drag and drop, auto-complete and many useful features to speed up your Android development cycles.
Summary steps to develop an Android application :
  1. Install Android SDK
  2. Install ADT Eclipse plugin
  3. Create an Android Virtual Device (AVD)
  4. Create Android Project with Eclipse (Wizard)
  5. Code it…
  6. Start it in Android Virtual Device (AVD)

    1. Install Android SDK

    Visit this Android SDK page, choose which platform and install it.
    In Android SDK installed folder, run “Android SDK manager”, choose what Android version you want to develop.

    2. Install ADT Eclipse plugin

    To integrate Android SDK with Eclipse IDE, you need to install Eclipse ADT plugin. Refer to this official guide – “Installing the ADT Plugin“.
    In Eclipse IDE, select “Help” -> Install New Software…”, and put below URL :   

    3. Create an Android Virtual Device (AVD)

    In Eclipse, you can access the “Android Virtual Device (AVD)” in Eclipse toolbar. Click “new” to create a AVD.

    4. Create Android Project

    In Eclipse, select “File -> New -> Project….”, “Android Project”, and input your application detail. Eclipse will create all the necessary Android project files and configuration.

    5. Hello World

    Locate the generated activity file, and modify a bit to output a string “Hello World”.
    javaFile : HelloAndroidActivity.java  
       
    package com.helloexample;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class HelloAndroidActivity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }
    }
     
  7. 6. Demo

    Run it as “Android Application“, see output.