Android App basic structure tutorial


In this lesson, you'll learn about the basic structure of an Android app with a basic user interface. First, run this app. The XML file is the App Manifest. The activity element tells the system that MainActivity is the launch activity of your app. The Java file contains the class MainActivity which controls the launch activity. The onCreate method is named when the activity is made. The method call setContentView() sets the layout file main.xml as the user interface content. This is the user interface layout XML file. It contains a TextView element with the android:text attribute Hello World. Now we change the text shown by the app to Hi World. Then press runs again. Increase the size of the text by adding an android:textSize attribute with value 30sp to the TextView element. Hint: Just type textsi and use code completion. Now increase the text size even more to 60sp. Tap the 'Design' button in the action bar and use the designer to do this. Congratulations! You've learned the app basics. You can play with the code on your own now. Try different values for android:text and android:textSize and run the app as often as you like.

REQUIREMENTS:
Download:
Eclipse

Instruction:
After you download all the files needed in this tutorial, we need to install and set-up a java JDK path to our environment variables, the step below will show you how.

--Go to Java JDK installation directory in "C:\Program Files\Java\jdk1.7.0\bin" Copy this path
--Go to the environment path by Clicking: Window taskbar--->right click Computer--->Click Advance System setting ---> then go to Advanced tab Then select "Environment Variable" then create new Variable name and variable value by clicking the New radio button. Within the "Variable name" input these "JAVA_HOME" then in the "Variable value" point it to your C:\Program Files\Java\jdk1.7.0\bin 
that's it we are on a way of creating our first game and deploy it to a different platform.

After that we will also set-up Android SDK, to its path by extracting first it to the directory you want for example in "drive D:" or "drive C:"  once we did this, we can now copy the root folder of this SDK which is likely to this format "D:\Android SDK" copy it then add it also to the "Environment path" like what you did before in "Java JDK".



COMPLETE CODE SAMPLE

MAIN.XML



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical">

<TextView
android:textSize="60sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi World"/>


</LinearLayout>

MainActivity



package com.aide.trainer.myapp;

import android.app.*;
import android.os.*;

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


ANDROID MANIFEST.XML

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.aide.trainer.myapp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk 
        android:minSdkVersion="11" 
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Holo.Light"
            android:name=".MainActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>



Post a Comment

0 Comments