Sunday 3 February 2013

Styles And Themes


Android style is look like cascading stylesheets in web design—they allow you to separate the design from the content.

style is a collection of properties that specify the look and format for a View.

Note:A View occupies a rectangular area on the screen.View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.).

A style can specify properties such as height, padding, font color, font size, background color, and much more.  A style is defined in an XML resource that is separate from the XML Layout.




For example, by using a style, you can take this layout XML:
<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="#00FF00"
    android:typeface="monospace"
    android:text="@string/hello" />
And turn it into this:
<TextView
    style="@style/CodeFonts"
    android:text="@string/hello" />
All of the attributes related to style have been removed from the layout XML and put into a style definition called CodeFont, which is then applied with the style attribute. You'll see the definition for this style in the following section.

Defining Styles :

You can define the style to res/values directory of your project, The name of the XML file is arbitrary(Your choice to select name of xml or random), but it must use the .xml extension and be saved in the res/values/ folder.
Here's an example file with a single style:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="CodeFonts" parent="@android:style/TextAppearance.Medium">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#00FF00</item>
        <item name="android:typeface">monospace</item>
    </style>
</resources>
Each child of the <resources> element is converted into an application resource object at compile-time, which can be referenced by the value in the <style> element's name attribute. This example style can be referenced from an XML layout as @style/CodeFonts (as demonstrated in the introduction above).
The parent attribute in the <style> element is optional and specifies the resource ID of another style from which this style should inherit properties. You can then override the inherited style properties if you want to.
you can inherit style from exisiting styles by using parent attribute.

 <style name="GreenText" parent="@android:style/TextAppearance">
        <item name="android:textColor">#00FF00</item>
    </style>




Activity Life Cycle

 onCreate():
When an activity starts its life onCreate() is called. It is called only once in the lifecycle of an activity.
onDestroy():
onDestroy() is called when an activity finishes its life cycle. It is also called once in the lifecycle of an activity.
onContentChanged():
This hook is called whenever the content view of the screen changes (due to a call to Window.setContentView or Window.addContentView). For example you add new view to activity or want to refresh the list by calling notifyDataSetChanged().
onDetachedFromWindow():
Called when the main window associated with the activity has been detached from the window manager. For example, it is called when the current Activity goes into background or another activity came infront of current activity.


 When an activity is created for the first time then system calls the OnContentChanged() method as the first method and last call by system is the OnDetachedFromWindow() method when an activity is killed
 
Methods:-

@Override
public void onContentChanged()
{
super.onContentChanged();  
Toast.makeText(getApplicationContext(),"1. onContentChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Toast.makeText(getApplicationContext(),"2. onCreate()", Toast.LENGTH_SHORT).show();

}

@Override
public void onStart()
{
     super.onStart();
    Toast.makeText(getApplicationContext(),"3. onStart()", Toast.LENGTH_SHORT).show();
}
@Override
public void onRestoreInstanceState(Bundle restoreInstanceState)
{

    Toast.makeText(getApplicationContext(),"4. onRestoreinstaneState()", Toast.LENGTH_SHORT).show();
    super.onRestoreInstanceState(restoreInstanceState);
}
@Override
public void onRestart() 
{
    super.onRestart();
    Toast.makeText(getApplicationContext(),"5. onRestart()", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostCreate(Bundle onpostcrete)
{
    super.onPostCreate(onpostcrete);
    Toast.makeText(getApplicationContext(),"6. onPostCreate()", Toast.LENGTH_SHORT).show();
//  super.onUserInteraction();

}
@Override
public void onResume()
{
     super.onResume();
    Toast.makeText(getApplicationContext(),"7. onResume()", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPostResume ()
{
    super.onPostResume();
    Toast.makeText(getApplicationContext(),"8. onPostResume()", Toast.LENGTH_SHORT).show();
}
@Override
public void onAttachedToWindow()
{
    super.onAttachedToWindow();
    //super.onSearchRequested()
    Toast.makeText(getApplicationContext(),"9. onAttachedToWindow()", Toast.LENGTH_SHORT).show();
}
@Override
public void onWindowFocusChanged(boolean bo)
{
 super.onWindowFocusChanged(true);
 Toast.makeText(getApplicationContext(),"10. onWindowFocusChanged()", Toast.LENGTH_SHORT).show();
}
@Override
public void onUserLeaveHint()
{
    super.onUserLeaveHint();
    Toast.makeText(getApplicationContext(),"11. onUserLeaveHint()", Toast.LENGTH_SHORT).show();
}
@Override
public void onUserInteraction()
{
    super.onUserInteraction();
    ii=0;
    Toast.makeText(getApplicationContext(),"12. onUserInteraction()", Toast.LENGTH_SHORT).show();
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
    super.onSaveInstanceState(savedInstanceState);
    Toast.makeText(getApplicationContext(),"13. onSaveInstanceState()", Toast.LENGTH_SHORT).show();
}
@Override
public void onPause()
{
    super.onPause();
    Toast.makeText(getApplicationContext(),"14. onPause()", Toast.LENGTH_SHORT).show();
}

@Override
public void onStop()
{
    super.onStop();
    Toast.makeText(getApplicationContext(),"15. onStop()", Toast.LENGTH_SHORT).show();
}


@Override
public void onDestroy()
{
    super.onDestroy();
    Toast.makeText(getApplicationContext(),"16. onDestroy()", Toast.LENGTH_SHORT).show();
}


@Override
public void onDetachedFromWindow()
{
    super.onDetachedFromWindow();
    Toast.makeText(getApplicationContext(),"17. onDetachedFromWindow()", Toast.LENGTH_SHORT).show();
}

@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    Toast.makeText(getApplicationContext(),"18. onConfigurationChanged()", Toast.LENGTH_SHORT).show();
}


@Override
public boolean onSearchRequested()
{
    super.onSearchRequested();
    //super.onWindowFocusChanged(boolean bo)
    Toast.makeText(getApplic`enter code here`ationContext(),"19. onSearchRequested()", Toast.LENGTH_SHORT).show();
    return false;

}

Read More..
http://developer.android.com/reference/android/app/Activity.html#onContentChanged%28%29

Friday 1 February 2013

Getting Started with android


          Getting Started with Android
   
System Requirements:

Operating Systems:

  • Windows XP (32-bit), Vista (32- or 64-bit), or Windows 7 (32- or 64-bit)
  • Mac OS X 10.5.8 or later (x86 only)
  • Linux (tested on Ubuntu Linux, Lucid Lynx)
    • GNU C Library (glibc) 2.7 or later is required.
    • On Ubuntu Linux, version 8.04 or later is required.
    • 64-bit distributions must be capable of running 32-bit applications.

Eclipse IDE:

  • Eclipse 3.6.2 (Helios) or greater
Note: Eclipse 3.5 (Galileo) is no longer supported with the latest version of ADT.
Step 1: Verify JDK is installed
        The first step is to check if you have the JDK (Java development kit) installed. If  not then
          You can download and install from oracle.com
         
Step 2: Download Eclipse
The next step is to download eclipse (the classic version is recommended). You can download it from http://www.eclipse.org/downloads
Step 3: Download the Android SDK
You can download the Android SDK from http://developer.android.com/sdk/index.html and unpack it. Note you may want to put your Android SDK in a safe spot since you’ll be using it a lot.
Once done, go to the tools folder and double click the android.bat file. This will open the Android SDK and AVD manager.



Step 4: Download Packages
Click on Available packages this will show a list of all the available Android SDK’s. Download an SDK that you want to build your app for.
To choose an SDK based on your device, go to Settings\About phone\Android version to see the version your phone is running. Then choose an SDK that is the same version as your device or earlier.

Step 5: Install ADT plug-in
Next we need to setup the ADT plug-in. To do this start eclipse go to Help > Install new software. Click Add on the top right corner

Enter the following URL: https://dl-ssl.google.com/android/eclipse/ Check developer tools, click Next, and walk through the wizard to install the tools.
Or Download ADT Plug-in from http://developer.android.com/tools/sdk/eclipse-adt.html and click Archive from Add Repository then walk through the wizard to install the tools.
After you restart Eclipse, it will prompt you for the Android SDK installation directory. Select “Use existing SDKs” and browse to the directory where you downloaded the Android SDK, and finish the wizard.
That’s it! You have successfully setup your development environment.

Hello Android!!

Assuming you have successfully installed everything, it’s time to create your first android project.
Step 1: Creating the project
  • Start eclipse
  • Go to File > New > Other > Android and select Android Project. You should see the following popup screen (yours may be slightly different or split into several steps):
·          Enter all the details as I have. Here is a brief explanation of each field:
o    Project name is the name of the directory that contains the project files (name it Quote Reader because that’s what we’ll make).
o    Build target specifies which version of Android the application is built for. I’ve selected 2.2.
o    The Application Name, Package Name are self explanatory except for the MIN SDK property. This is used for backward compatibility i.e. if your application requires a minimum api level, or is designed only to support a certain range of Android platform versions. Setting this to a value ensures that your application can only be installed on devices that are running a compatible version of the Android system.
·          Then Create New Activity Click Next.
Okay! Now that you have entered all the details press next. Eclipse will now create the project, which you can see in your project navigator.




Step 2: Understanding the project structure
        Before we get started with any code lets spend some time going through the project structure.
Here is a brief description of the folders present:
·         Source (src): This folder contains all the source files i.e. this is where all the code is.
·         Res: This is the area where you keep your images, strings and xml layouts.
·         Assets: This is another place where you can keep files. The difference between /res and /assets is that Android does not generate IDs for the assets. What this means is that to access a file in assets the developer has to provide a complete path to that file.
Now that you have a basic understanding of the project structure, let’s run the application.



Step 3: Running the application
There are two ways in which we can run the application either on an actual device or on an emulator. I will explain how to do both very briefly.
To run the application on the emulator we first need to create an AVD (Android virtual device). This is an instance of the android emulator. To do this, follow these steps:
·         Go to Window > Android SDK & AVD Manager and click New in the top right corner.
·         Enter all the required details.

  • Click on “Create AVD”, after that you can start the emulator by just selecting it from the AVD manager and clicking on start.
After the emulator starts up you can run the application that you just created. To do this right click on the project, go to Run As and select Android Application. This will launch the app on the emulator.
                   


Running the application on the device is fairly simple and requires very little effort. Here are the steps to launch an app on the device:
  • On your device, to to Settings\Application\Development and select the checkbox next to USB debugging.
  • Connect the device to your computer with a USB cable.
  • Run the project through eclipse. You will see the following popup:

  • Select the device and you’re app will be deployed.
Note: Notice that the popup above shows two options; the reason being I already have an emulator running hence I need to choose where to deploy the app i.e. either on the emulator or the device.