วันพฤหัสบดีที่ 28 มิถุนายน พ.ศ. 2555

intent Android example

--> Intent Android Example

 Package Explorer

Intent Android Activity.java

package android.example.intent;

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

public class IntentAndroidActivity extends Activity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final Button btn1 = (Button)findViewById(R.id.button1);      
        btn1.setOnClickListener(new View.OnClickListener() {
           
            public void onClick(View v) {                  
                  startActivity(new Intent("android.example.intent.activity2"));
                  }
            });
       
        // Calling Built-In Applications Using Intents
       
        final Button btn2 = (Button)findViewById(R.id.button2);      
        btn2.setOnClickListener(new View.OnClickListener() {                 
            public void onClick(View v) {
                 
                  Intent i = new Intent(android.content.Intent.ACTION_VIEW,                          
                                          Uri.parse("http://www.google.com"));                 
                  startActivity(i);
                  }
        });
       
        final Button btn3 = (Button)findViewById(R.id.button3);      
        btn3.setOnClickListener(new View.OnClickListener() {                 
            public void onClick(View v) {
                 
                  Intent i = new Intent(android.content.Intent.ACTION_DIAL,
                              Uri.parse("tel:+651234567"));                  
                  startActivity(i);
                  }
        });
    }
}

Activity2.java

package android.example.intent;

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

public class activity2 extends Activity {
     
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
                                      
    }// onCreate
      
}

 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical" >

    <TextView
        android:layout_width="224dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Android Intent"
        android:textColor="#FF00FF"
        android:textSize="30dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:textSize="26dp"
        android:text="Intent to Activity2" />

<Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:textSize="26dp"
        android:text="Intent.ACTION_VIEW" />

<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:textSize="26dp"
        android:text="Intent.ACTION_DIAL" />


 <TextView
     android:id="@+id/textView1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_marginLeft="20dp"
     android:layout_marginTop="180dp"
     android:text="main.xml"
     android:textAppearance="?android:attr/textAppearanceMedium"
     android:textColor="#FF0000"
     android:textSize="26dp" />

</LinearLayout>


 activity2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFBB33"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:text="Activity 2"
                android:textColor="#FF00FF"
                android:textSize="30dp" />

            <Button
                android:id="@+id/button1"
                android:layout_width="66dp"
                android:layout_height="38dp"
                android:background="#00FF00"
                android:text="Button 1" />

            <LinearLayout
                android:id="@+id/linearLayout1"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >

                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:background="@drawable/ic_launcher"
                    android:text="Button" />

                <Button
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />

                <Button
                    android:id="@+id/button5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />

                <Button
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />

                <Button
                    android:id="@+id/button7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="Button" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>


    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="300dp"
        android:text="activity2.xml"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FF0000" />

</LinearLayout>

AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.example.intent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".IntentAndroidActivity"
            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=".activity2"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.example.intent.activity2" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
       
    </application>

</manifest>


Calling Built-In Applications Using Intents

 
 
In the first button, you create an Intent object and then pass two arguments to its constructor the
action and the data:

Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(http://www.amazon.com));
startActivity(i);

Intent i = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse(tel:+651234567)); 
startActivity(i);

Understanding the Intent Object
You can also create an Intent object by passing in an action constant and data, such as the following:

Intent i = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(http://www.amazon.com));
startActivity(i);

The action portion defines what you want to do, while the data portion contains the data for the target
activity to act upon. You can also pass the data to the Intent object using the setData() method:

Intent i = new Intent(android.content.Intent.ACTION_VIEW); 
i.setData(Uri.parse(http://www.amazon.com));

For some intents, there is no need to specify the data. For example, to select a contact from the Contacts
application, you specify the action and then indicate the MIME type using the setType() method:

Intent i = new Intent(android.content.Intent.ACTION_PICK);
i.setType(ContactsContract.Contacts.CONTENT_TYPE);

To summarize, an Intent object can contain the following information:
➤➤ Action
➤➤ Data
➤➤ Type


Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android
-->

วันจันทร์ที่ 18 มิถุนายน พ.ศ. 2555

Android Button Example Code

--> Android Button Example Code


Java code AndroidButtonActivity.java

package android.example.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ToggleButton;

public class AndroidButtonActivity extends Activity {
     
      int count1=0;
      int count2=0;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button btn1 = (Button)findViewById(R.id.button1);      
        Button btn2 = (Button)findViewById(R.id.button2);       
        final ToggleButton btnToggle = (ToggleButton)findViewById(R.id.toggleButton);
  
        final TextView tv1 = (TextView)findViewById(R.id.textView1);
        final TextView tv2 = (TextView)findViewById(R.id.textView2);
        final TextView tv3 = (TextView)findViewById(R.id.textView3);

        btn1.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {                  
                        // Do Something When Button Click                    
                        count1++;
                        tv1.setText("Normal Button Click " + count1);           
                  }  
        });
       
        btn2.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {                  
                        // Do Something When Button Click
                        count2++;                    
                        tv2.setText("Small Button Click " + count2);            
                  }  
        });
              
        btnToggle.setOnClickListener(new View.OnClickListener() {
                  public void onClick(View v) {
               
                        if(btnToggle.isChecked()){         
                              tv3.setText("Toggle Button ON ");
                        }  
                        else{            
                              tv3.setText("Toggle Button OFF");
                        }
                  }
        });
    }
}

 Android Button Example code - AndroidMainifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.example.button"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidButtonActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Download Code
Android Button Example Code 

More Info
Android Menu Example Code
-->


Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android

วันเสาร์ที่ 2 มิถุนายน พ.ศ. 2555

Hello Android The First App


Hello Android The First App. 


Code for HelloAndroid.java 

























package com.example.hello;

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);       
       
    }
}

 
Code XML in main.xml
 
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0000FF"
        android:text="Medium Text Blue Colour"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text Red Colour"
        android:textColor="#FF0000"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

For Change Text colour
 
#FF0000 
 mean  Red = FF(in hex ) Green = 00  Blue = 00  ( FF hex = 255 decimal )
 
Download Sourcecode in Full Project here !


วันศุกร์ที่ 1 มิถุนายน พ.ศ. 2555

Android Development Tools



Android Development Tools

The Android Software Development Kits ( Android SDK ) is comprised of the platform, tools, sample code, and documentation
needed to develop Android applications. It is built as an add-on to the Java Development
Kit and has an integrated plugin for the Eclipse Integrated Development Environment.

Here's an overview of the steps you must follow to set up the Android SDK:
Prepare your development computer and ensure it meets the system requirements.
  1. JDK ( Java Development kit ) includes JRE
  2. Eclipse with Android Development kit ( SDK )  For SDK If you're on Windows, download the installer for help with the initial setup.
  3. ADT Plugin for Eclipse (if you'll be developing in Eclipse).
To get started, download the appropriate package.











1.JDK (Java Development Kit), which includes the JRE. ( I use Java SE 7 )










2.Eclipse  ( I use Eclipse Classic 3.7.1 )and  Android SDK ( lasted Version now android -sdk r18 )

 
3.Android Development tools ADT Plugin for Eclipse


Installing and Upgrading
There are many places on the Internet that discuss detailed step-by-step instructions on how to install the Android SDK. For example, all the necessary links can be found on the Google website http://developer.android.com/sdk/.Therefore, the general procedure outlined here serves to emphasize the most common installation steps for reference.These steps should be done on a host computer used as the development environment.

1. Install the Java Development Kit (for example, install JDK 6.0 for use with
Android 2.1 or above; JDK 5.0 is the minimum version needed for any earlier version
of Android).

2. Install Eclipse Classic (for example, version 3.7.1). In the case of Windows, this just
needs to be unzipped in place and is ready to use.

3. Install the Android SDK starter package (for example, version r16). In the case of
Windows, this just needs to be unzipped in place and is ready to use.

4. Start Eclipse and select Help → Install New Software..., and then type https://
dl-ssl.google.com/android/eclipse/ and install the Android DDMS and Android
Development Tools.

5. In Eclipse, select Window → Preferences... (on a Mac, select Eclipse → Preferences)
and select Android. Browse to the location where the SDK was unzipped
and apply.

6. In Eclipse, select Window → Android SDK and AVD Manager → Available
Packages, and then choose the necessary APIs to install (for example, Documentation
for Android SDK,API 8; SDK Platform Android 2.2,API 8; Google APIs by
Google Inc.; and Android API 8).

7. From the same Android SDK and AVD Manager menu, create an Android virtual
device to run the emulator or install USB drivers to run applications on a pluggedin
phone.

8. In Eclipse, select Run →Run Configurations... and create a new run configuration
to be used with each Android application (or similar for a Debug Configuration).
Android JUnit tests can be configured here, too.