Tuesday, 29 July 2014

4. Hello World

Hope you have successfully created a virtual device on your system and are familiar with basic with your Eclipse window. Now you are ready to start a new project. Steps are given below with the need of each . Tips and tricks are included wherever required.

So open your Eclipse and follow !!.


  1. Go to file menu, under New section click on 'Android application project'. A window will be opened as shown below.Now fill the details as you want.
    1.  App name: the name you want to give to your App.
    2.  Project Name: it will be filled automatically, but you can change if you want.
    3.  Package Name: it will e filled automatically, but you can change if you want.(don't use   co.example.*    if want to publish your app)
    4. Min required SDK: the oldest version of android that may be able to run this app.
    5. Target SDK: the version of android for which you are developing your app.
    6. Compile with: Choose the version you have selected above (target SDK) .
    7. Theme: there are some template theme available in SDK,You can choose any of them or choose none.
    8. Click on next.
  2.  Now you will see screen like this. Leave the settings to default. This enables you to make your own App icon and activity in further steps. Click next.
  3. Now you are on Launcher Icon setup. choose a icon for your app using the options provided. You can choose a text, a clip art or from a image file.
  4. Now you have to select the type of activity.
    1. Blank Activity creates a normal activity (Activity -display area of your screen).
    2. Fullscreen Activity: Notification area is not visible. Generally used for games.
    3. Master /detail flow: Like a wizard window. Wizard window -the one like you ae using now on Eclipse.
  5.  Now you have to enter details for your Activity. Enter any name you want. Fragment Activity is pat of your Activity. You can design fragment which gets super imposed on your main Activity. Though you can design your main Activity separately.Navigation type is the type of navigation gestures you want to switch between your activities.
  6.  content of your fragment.xml file
    1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="@dimen/activity_vertical_margin"
          android:paddingLeft="@dimen/activity_horizontal_margin"
          android:paddingRight="@dimen/activity_horizontal_margin"
          android:paddingTop="@dimen/activity_vertical_margin"
          tools:context="com.example.helloworld.MainActivity$PlaceholderFragment" >

          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/hello_world" />

      </RelativeLayout>
  7.  Content of MainActivity.java file:
    1. package com.example.helloworld;

      import android.app.Activity;
      import android.app.ActionBar;
      import android.app.Fragment;
      import android.os.Bundle;
      import android.view.LayoutInflater;
      import android.view.Menu;
      import android.view.MenuItem;
      import android.view.View;
      import android.view.ViewGroup;
      import android.os.Build;

      public class MainActivity extends Activity {

          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              if (savedInstanceState == null) {
                  getFragmentManager().beginTransaction()
                          .add(R.id.container, new PlaceholderFragment()).commit();
              }
          }

          @Override
          public boolean onCreateOptionsMenu(Menu menu) {

              // Inflate the menu; this adds items to the action bar if it is present.
              getMenuInflater().inflate(R.menu.main, menu);
              return true;
          }

          @Override
          public boolean onOptionsItemSelected(MenuItem item) {
              // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
              int id = item.getItemId();
              if (id == R.id.action_settings) {
                  return true;
              }
              return super.onOptionsItemSelected(item);
          }

          /**
           * A placeholder fragment containing a simple view.
           */
          public static class PlaceholderFragment extends Fragment {

              public PlaceholderFragment() {
              }

              @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
                      Bundle savedInstanceState) {
                  View rootView = inflater.inflate(R.layout.fragment_main, container,
                          false);
                  return rootView;
              }
          }

      }
  8. Explanation of above code is done here.
  9. The fragment.xml can be edited graphically too. This will be updated here soon!!.
  10. Now you are done with coding Part too. Its time to build and run your project. Now go to Project menu and click on Clean. This rebuilds your project and refreshes all the linked files. Sometimes you see many errors Related 'R' Class, this step generally solves this problem. More Info will be given on R class in further posts.
  11. Now select 'Clean Projects selected below' and tick your project (helloWorld here). This will only build the selected projects else you end up updating all your projects in your workspace which eats up lots of time. 
  12.  Now select your project in project explorer (generally on left side of the window ) and the click on run (Either on menubar or on the toolbar). Then you will see another window as shown below. Now select 'Android Application' and click on 'OK'
  13.  Now select your AVD (Android Virtual Device) that you have already created (if not then click here) if prompted. Now your Virtual device will be loaded and will run your Application when its ready. You may need to unlock the phone (AVD) to see your Application Activity.



Note : If AVD doesn't launches your application even after unlocking and you see error like "connection lost" in console. Then you may have to repeat last two steps again.

Feel free to give any suggestions and reviews. For any doubt comment below.

No comments:

Post a Comment