Webocreation

Wednesday, March 28, 2012

Android Development, Understanding Hello World



hello world android application development



With that confirmed, let’s take a step back and have a real look at your first Android application. Activity is the base class for the visual, interactive components of your application; it is roughly equivalent to a Form in traditional desktop development. The following snippet shows the skeleton code for an Activity-based class; note that it extends Activity, overriding the onCreate method.

package com.paad.helloworld;
import android.app.Activity;
import android.os.Bundle;
public class HelloWorld extends Activity {
    /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle icicle) {
       super.onCreate(icicle);
   }
}



What’s missing from this template is the layout of the visual interface. In Android, visual components
are called Views, which are similar to controls in traditional desktop development.

In the Hello World template created by the wizard, the onCreate method is overridden to call setContentView, which lays out the user interface by infl ating a layout resource, as highlighted below:
@Override


public void onCreate(Bundle icicle) {
     super.onCreate(icicle);
     setContentView(R.layout.main);
}
The resources for an Android project are stored in the res folder of your project hierarchy, which includes drawable, layout, and values subfolders. The ADT plug-in interprets these XML resources to provide design time access to them through the R variable.

The following code snippet shows the UI layout defined in the main.xml file created by the Android
project template:

<?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:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Hello World, HelloWorld”
/>

</LinearLayout>

Defining your UI in XML and inflating it is the preferred way of implementing your user interfaces, as it neatly decouples your application logic from your UI design.
To get access to your UI elements in code, you add identifier attributes to them in the XML definition. You can then use the findViewById method to return a reference to each named item. The following XML snippet shows an ID attribute added to the TextView widget in the Hello World template:

<TextView
android:id=”@+id/myTextView”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”Hello World, HelloWorld”
/>

And the following snippet shows how to get access to it in code:
TextView myTextView = (TextView)findViewById(R.id.myTextView);

Alternatively (although it’s not considered good practice), if you need to, you can create your layout directly in code as shown below:

public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout.LayoutParams lp;
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams textViewLP;
textViewLP = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TextView myTextView = new TextView(this);
myTextView.setText(“Hello World, HelloWorld”);
ll.addView(myTextView, textViewLP);
this.addContentView(ll, lp);
}

All the properties available in code can be set with attributes in the XML layout. As well as allowing
easier substitution of layout designs and individual UI elements, keeping the visual design decoupled
from the application code helps keep the code more concise.

5 comments:

  1. I appreciate from this post and its seems looking so informative ad networks for good target for Indian's. Thanks for sharing with us.
    http://packersmoversbangalore.in/

    ReplyDelete
  2. Thanks for sharing such a great article and it’s helpful for everyone. Great Post..
    http://packers-and-movers-bangalore.in/

    ReplyDelete
  3. Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing ! Kindly Visit Us @ Packers And Movers Mumbai

    Packers And Movers Bhandup Mumbai
    Packers And Movers Bhavani Shankar Road Mumbai
    Packers And Movers Borivli Mumbai
    Packers And Movers Chakala MIDC Mumbai

    ReplyDelete
  4. Get Shifting/Relocation Quotation from ###Packers and Movers Delhi. Packers and Movers Delhi 100% Affordable and Reliable ***Household Shifting Services. Compare Transportation Charges and Save Time, Verified and Trusted Packers and Movers in Delhi, Cheap and Safe Local, Domestic House Shifting @
    Packers And Movers Delhic

    ReplyDelete
  5. Ceramic vs Titanium Art - TITIAN ART
    In ceramics, the titanium earrings sensitive ears whole titanium security metal part of the resin is called ceramic. In this case, there is titanium drill bit set a piece of ceramic on the surface of 2017 ford fusion energi titanium the titanium eyeglass frames handle.

    ReplyDelete