Friday, 9 March 2012

Display google page on button click

****************ConnectUrlActivity.java*************




package com.sampleapp;

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

public class SampleAppActivity extends Activity {
/** Called when the activity is first created. */
          private Button btn_go;
          AlertDialog alertDialog;
          EditText nw_name;

@Override
public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         btn_go = (Button) findViewById(R.id.btn_go);
         btn_go.setOnClickListener(new OnClickListener() { 
                      @Override
                      public void onClick(View v) {
                                String url = "http://www.google.com";
                                Intent i = new Intent(Intent.ACTION_VIEW);
                                i.setData(Uri.parse(url));
                                startActivity(i);
                     }
          });
}
}

Display Alert box in Android




**************SampleAppActivity.java*************


package com.sampleapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;

public class SampleAppActivity extends Activity {
/** Called when the activity is first created. */
private Button btn_go;
AlertDialog alertDialog;
EditText nw_name;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn_go = (Button) findViewById(R.id.btn_go);
btn_go.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

alertDialog = new AlertDialog.Builder(SampleAppActivity.this).create();
alertDialog.setTitle("Values...");
alertDialog.setMessage("hi");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                      return;
                }
});
alertDialog.show();
}
});
}
}


***************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:text="@string/name"
android:layout_marginLeft="10dip"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content" />

    <EditText
android:id="@+id/nw_name"
android:text=""
android:layout_width = "fill_parent"
android:layout_height = "wrap_content" />

    <Button
android:id="@+id/btn_go"
android:text="@string/go"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
    ></Button>

</LinearLayout>

**************strings.xml***************
<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">hi, AlertActivity!</string>
<string name="app_name">SampleApp</string>
<string name="name">Name</string>
<string name="designation">Designation</string>
<string name="company">Company</string>
<string name="go">GO</string>

</resources>

Wednesday, 7 March 2012

Android Default Application


First Sample Application in Android:
==========================

SampleApp
*assets
*bin
   ->classes
-->>(java classes)
   ->res
-->>(resources)
   ->classes.dex
   ->resources.ap_
   ->SampleApp.apk
*gen
   ->com
-->>sampleapp
--->>>R.java
*res
   ->drawable-hpdi
   ->drawable-lpdi
   ->drawable-mpdi
   ->layout
-->>main.xml
   ->values
-->>strings.xml
*src
   ->com
-->>sampleapp
--->>>SampleAppActivity.java
-.classpath
-.project
-AndroidManifest.xml
-progaurd.cfg
-project.properties


****************SampleAppActivity.java***********

package com.sampleapp;

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

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

*************AndroidManifest.xml*************

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SampleAppActivity"
            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>

*******************main.xml*********************

Each and every component and the layout design is defined in this file.

<?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" />
</LinearLayout>


********************strings.xml*****************

Handles all the strings used in the project. (similar to messages.properties)

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Hello World, SampleAppActivity!</string>
    <string name="app_name">SampleApp</string>

</resources>


***********project.properties*******************

This is a Version Control Systems.

# Project target.
target=android-7


******************progaurd.cfg******************


Android Installation


Installing Android in your system:
======================

Download  'android-sdk-windows'

Download a new eclipse of ur choice: Eclipse 3.6 and greater version

Update the eclipse by the following steps:

1. Help -> Install new software -> Install window opens

2. In Work with dropdown select -> 'ADT Plugin - https://dl-ssl.google.com/android/eclipse/ '

3. select all the checkboxes -> click 'next' -> 'finish'




--- * ---

setting adb path: (To run the apk in dos prompt)
================================

Controlpanel -> System -> Advanced -> Environment variables -> path -> edit - set the adb path
adb path ------------> C:\Softwares\android-sdk-windows\platform-tools;

 --- * ---

Android Activity Life Cycle


Activity Life Cycle:

  1. The Activity class has defined dozens of callback functions, all begin with "on".
  2. Some of them are triggered by UI events, some are lifecycle related. It isn't easy for developer to remember the usage of them.
  3. An activity has essentially four states, when some event happens, like user navigates between different activities, state transites.

1. onCreate()
2. onStart()
3. onRestart()
4. onResume()
5. onPause()
6. onStop()
7. onDestroy()




Tuesday, 6 March 2012

Android Architecture


1. Applications:

These are applications written in Java

1. Some of basic applications includes an calendar, email client, SMS program, maps, making phone calls, accessing the Web browser, accessing your contacts list and others.

2. easy for device hardware, access location information, run background services, set alarms, add notifications to the status bar, and much, much more.

3. rest all layers are used by Google programmers, developers and hardware manufacturers.


2. Application Framework:

This is the skeleton or framework which all android developers has to follow.

    1. used to build an application
    2. to access data from other applications
    3. Resource Manager
    4. Notification Manager
    5. Activity Manager

    3. Libraries:

    1. consists of Android libraries written in C, C++, and used by various system
    2. to handle different kinds of data
    3. Some of these libraries includes  media, graphics, 3d, SQLite, web browser library etc.

    4. Runtime Android:

    1. Every Android application runs in its own process, with its own instance of the Dalvik virtual machine.
    2. Using Dalvik, a device can run multiple VMs efficiently.
    3. The Dalvik virtual machine is a register-based virtual machine, designed and written by Dan Bornstein with contributions from other Google engineers.
    4. The Dalvik VM executes files in the Dalvik Executable (.dex) format which is optimized for minimal memory footprint. The VM is register-based, and runs classes compiled by a Java language compiler that have been transformed into the .dex format by the included "dx" tool.

    5. Kernel – Linux:

    “Linux kernel for underlying functionality such as threading and low-level memory management."
    Let me make it simple for you, without Linux, there is no Android. 

    Android Layouts


    Android Layouts
    -----------------
    1. An Android layout is a class.
    2. It handles to arrange the components appear on the screen.
    3. Anything that is a View can be a child of a layout.
    4. All of the layouts inherit from ViewGroup (which inherits from View) so you can nest layouts.
    5. You could also create your own custom layout by making a class that inherits from ViewGroup.
    Android supports the following ViewGroups:

    * LinearLayout
    * AbsoluteLayout
    * TableLayout
    * RelativeLayout
    * FrameLayout
    * ScrollView


    RELATIVE LAYOUT
    -----------------------


    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center" >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dip"
            android:text="@string/name" />

        <EditText
            android:id="@+id/nw_name"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_marginLeft="60dip"
            android:layout_toRightOf="@+id/name"
            android:text="" />

        <TextView
            android:id="@+id/degn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/name"
            android:layout_marginTop="30dip"
            android:text="@string/designation" />

        <EditText
            android:id="@+id/nw_degn"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nw_name"
            android:layout_marginLeft="20dip"
            android:layout_toRightOf="@+id/degn"
            android:text="" />

        <TextView
            android:id="@+id/cmpy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/degn"
            android:layout_marginTop="30dip"
            android:text="@string/company" />

        <EditText
            android:id="@+id/nw_cmpy"
            android:layout_width="100dip"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nw_degn"
            android:layout_marginLeft="36dip"
            android:layout_toRightOf="@+id/cmpy"
            android:text="" />

        <Button
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/nw_name"
            android:layout_below="@+id/nw_cmpy"
            android:layout_marginTop="23dp"
            android:text="@string/next" />

        <Button
            android:id="@+id/btn_go"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/cmpy"
            android:layout_alignTop="@+id/btn_next"
            android:text="@string/go" />

    </RelativeLayout>


    TABLE LAYOUT
    -------------------


    <?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:layout_gravity="center"
        android:orientation="vertical" >

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center" >

            <TableRow>

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/name" />

                <EditText
                    android:id="@+id/nw_name"
                    android:layout_width="120dip"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dip"
                    android:hint="Name"
                    android:text="" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/degn"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/designation" />

                <EditText
                    android:id="@+id/nw_degn"
                    android:layout_width="120dip"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dip"
                    android:hint="Designation"
                    android:text="" />
            </TableRow>

            <TableRow>

                <TextView
                    android:id="@+id/cmpy"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/company" />

                <EditText
                    android:id="@+id/nw_cmpy"
                    android:layout_width="120dip"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dip"
                    android:hint="Company"
                    android:text="" />
            </TableRow>

            <TableRow>

                <Button
                    android:id="@+id/btn_go"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/go" >
                </Button>

                <Button
                    android:id="@+id/btn_next"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:text="@string/next" >
                </Button>
            </TableRow>
        </TableLayout>

    </LinearLayout>