Hello World in Android

How to create your first application in Android:

Create a Project :

  • Go to File click on new and choose Android application Project.
  • Give a Application name.
  • Click on Next -> Next -> Next -> Next -> finish.


Create an Activity :

Project is created. Go to src folder.

Open MainActivity.java.

public class MainActivity extends Activity {
       Button register;

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

       @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;
       }
}

Class is extending an Activity class.
Every class which will be an activity must extends Activity class.
Override the onCreate method.


About Layout:

Activity in android doesn't contain anything of its own.
It loads xml layout view on the activity page.

                setContentView(R.layout.activity_main);
      
      Here, activity_main is an xml file

Edit Layout(activity_main.xml):

<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=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>



Change XML File:
  • Previous figure shows the graphical view of an activity.
  • Go to XML view.
  • Change the android:text project of textview.
  • To change go to strings.xml file in values folder.


                <string name="hello_world">Hello welcome to world of android!</string>

Save and Run the project

  • Save the changes and right click on the project.
  • Choose run as - > Android application project.
  • Wait some time. Project will run on emulator.






Reach us At: - 0120-4029000 / 24 / 25 / 27 / 29 Mbl: 9953584548
Write us at: - smruti@apextgi.com and pratap@apextgi.com


1 comment: