Thursday, September 15, 2011

How to Set up a Basic Android Project


  1. Select File > New > Project.
  2. Select Android > Android Project, and click Next.
  3. Select the contents for the project:
    • Enter a Project Name. This will be the name of the folder where your project is created.
    • Under Contents, select Create new project in workspace. Select your project workspace location.
    • Under Target, select an Android target to be used as the project's Build Target. The Build Target specifies which Android platform you'd like your application built against.
      Select the lowest platform with which your application is compatibl
        • Note: You can change your the Build Target for your project at any time: Right-click the project in the Package Explorer, select Properties, select Androidand then check the desired Project Target.
    • Under Properties, fill in all necessary fields.
      • Enter an Application name. This is the human-readable title for your application — the name that will appear on the Android device.
      • Enter a Package name. This is the package namespace (following the same rules as for packages in the Java programming language) where all your source code will reside.
      • Select Create Activity (optional, of course, but common) and enter a name for your main Activity class.
      • Enter a Min SDK Version. This is an integer that indicates the minimum API Level required to properly run your application. Entering this here automatically sets the minSdkVersion attribute in the <uses-sdk> of your Android Manifest file. If you're unsure of the appropriate API Level to use, copy the API Level listed for the Build Target you selected in the Target tab.






  • 4.Click Finish.



  • For Example i have created a Android Project named as "YellAdworks".
    Once you have created your android project your workspace should look like this.










  • Your Activity File should look like this.




  • package com.sai.sample;




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

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

    Your AndridManifest.xml file plays a vital role as the User-Permissions needs to be specified in the XML file.

    Click on the AndroidManiFest.xml.

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

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

    Now go to folder res/layout/main.xml

    This xml file helps to develop how your UI should look on the Android Device.

    Graphical Layout:



    XML view:


    Lets edit the TextView to our own choice.


    Now see lets the Graphical View of the Screen.


    Lets see how we can Add buttons and EditText box on the Screen.
    Just Drag ans Drop the Button from the Form Widget into the Screen.

    I have added two buttons in the Graphical Layout.

    Now we need the Name the Buttons as well as Change the size of the Buttons.

    Get into the XML file and see the Button Values are added into it.



    Now let's Rename the Button as well as the Size of the Button.

    Now make the changes for the Button Property as 

    <Button android:layout_weight="0.04"
     android:text="Click" 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content">
     </Button>
    <Button android:layout_weight="0.04" 
    android:text="Click" 
    android:id="@+id/button2"
     android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      </Button>

    Save the XMl file.


    We have changed the Button Size as well as Name .

    Okay ,What actions these Buttons can do .

    For example :

    When we click on the First Button , a message should be displayed as "You have clicked the First Button"



  • When we click on the Second Button , a message should be displayed as "You have clicked the Second Button"

    We need to set the property for both the Buttons and make a Toast Message.

    Now get into the YellAdworksActivity.Java file

    package com.sai.sample;

    import android.app.Activity;
    import android.os.Bundle;
    import android.text.format.Time;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.Toast;

    public class YellAdworksActivity extends Activity implements OnClickListener  {
        /** Called when the activity is first created. */
    private Button button1;
    private Button button2;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
             button1=(Button)findViewById(R.id.button1);
             button2=(Button)findViewById(R.id.button2);
            button1.setOnClickListener(this);
            button2.setOnClickListener(this);
        }

    @Override
    public void onClick(View v) {
    if(button1==v){
    Toast.makeText(YellAdworksActivity.this, "You have clicked the First Button", Toast.LENGTH_SHORT).show();
    }
    else if(button2==v)
    {
    Toast.makeText(YellAdworksActivity.this, "You have clicked the Second Button", Toast.LENGTH_SHORT).show();
    }
    }
    }


    The method OnClick() will execute only when the buttons are click or any ClickEvent are performed.

    Run as Android Application .

    When the First Button is clicked.


    When the Second Button is clicked.

    Note: If you guys are unaware on how to setup an Android Emulator .Please follow this link 




    No comments:

    Post a Comment