Android - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android First App

Android First App

In this chapter, we will explain you how to create an Android Project in Eclipse. However, you need to have the basic knowledge of Eclipse to create your first Android application. Developing Android Project in Eclipse To create an Android project in Eclipse, the following steps need to be followed:
  • Open Eclipse, File->New->Android Application Project->enter App name
  • Click on Next -> Next -> Next-> choose type either activity or fragment .
  • Click on Next->Enter Activity name and Layout name->then click Finish.
Once the project is created successfully, you will see the below default screen. Let us now look into the directories and files of an Android Project.
This folder contains the source file of the project (.java).
This folder contains the resource files of the project which can provide the references to all the resources of the project (.R ).This file is automatically generated by the system and should not be modified.
This folder contains all the .class files to produce an .apk file. This is generated by the compiler.(.apk)
Assets folder provides a mode to include arbitrary files such as text, xml, fonts, music, and video into an application.(ex:.txt,.xml etc)
This folder contains all the libraries present in an application (.jar).
The AndroidManifest.xml file contains information of your package, including the components of an application such as activities, services, broadcast receivers, content providers, etc.
This directory contains draw-able objects (.jpg or .png or .xml etc).
This directory contains layout resource files which provides references to an app’s user interface.(.xml)
This directory contains different types of resource files which include:styles,string,colors, etc.(.xml)
First create MainActivity.java under src/<your package name>  MainActivity.java: MainActivity.java is the main source file (.java) which is converted to the Dalvik Executable format to run an application. It should be created under src folder. Class should extend either of the Android Main Components (Activity, Service, Content Provider, BroadcastReceiver), it depends upon what you are implementing. [java] public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //set activity content to external view 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; } @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.menu_settings) { return true; } return super.onOptionsItemSelected(item); } } [/java] create activity_main.xml under res/layout folder activity_main.xml: Activity_main.xml is a .xml file which is responsible for UI. It should be created under res/layout. [code lang="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:background="#FFFFFF" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#009ACD" android:text="@string/hello_world" /> </RelativeLayout> [/code] strings.xml: The strings.xml file is located in the res/values folder and contains all the text that an application uses. For example, the names of buttons and labels. [code lang="xml"] <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">AndroidFirstApplication</string> <string name="hello_world">AndroidFirstApplication</string> <string name="menu_settings">Settings</string> </resources> [/code] AndroidManifest.xml: The AndroidManifest.xml file contains information of your package, including components of the application such as activities, services, broadcast receivers, content providers, etc. This file works as an interface between Android OS and your application, and therefore if you do not declare your component in this file, it will not be considered by the OS. [code lang="xml"] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spl.firstproject" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/AppTheme" > <activity android:name="com.spl.firstproject.MainActivity" 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> [/code] R.java: This file is automatically generated by the system and hence the content of this file should not be altered. It is located in res/yourpackagename/R.java file. [java] public final class R { public static final class anim { public static final int abc_fade_in = 0x7f040000; public static final int abc_fade_out = 0x7f040001; } public static final class attr { public static final int actionBarDivider = 0x7f010018; public static final int actionBarItemBackground = 0x7f010019; public static final int actionBarPopupTheme = 0x7f010012; public static final int actionBarSize = 0x7f010017; } public static final class bool { public static final int abc_action_bar_embed_tabs = 0x7f060000; public static final int abc_action_bar_embed_tabs_pre_jb = 0x7f060001; public static final int abc_action_bar_expanded_action_views_exclusive = 0x7f060002; public static final int abc_config_actionMenuItemAllCaps = 0x7f060005; public static final int abc_config_allowActionMenuItemTextWithIcon = 0x7f060004; public static final int abc_config_closeDialogWhenTouchOutside = 0x7f060006; public static final int abc_config_showMenuShortcutsWhenKeyboardPresent = 0x7f060003; } public static final class color { public static final int abc_background_cache_hint_selector_material_dark = 0x7f070033; public static final int abc_background_cache_hint_selector_material_light = 0x7f070034; } public static final class dimen { public static final int abc_action_bar_content_inset_material = 0x7f080025; public static final int abc_action_bar_default_height_material = 0x7f080023; } public static final class drawable { public static final int abc_ab_share_pack_mtrl_alpha = 0x7f020000; public static final int abc_btn_borderless_material = 0x7f020001; public static final int abc_btn_check_material = 0x7f020002; } public static final class id { public static final int action_bar = 0x7f050040; public static final int action_bar_activity_content = 0x7f05001d; public static final int action_bar_container = 0x7f05003f; public static final int action_bar_root = 0x7f05003b; } public static final class integer { public static final int abc_config_activityDefaultDur = 0x7f090001; public static final int abc_config_activityShortDur = 0x7f090000; public static final int abc_max_action_buttons = 0x7f090002; } public static final class layout { public static final int abc_action_bar_title_item = 0x7f030000; public static final int abc_action_bar_up_container = 0x7f030001; public static final int abc_action_bar_view_list_nav_layout = 0x7f030002; } public static final class string { public static final int abc_action_bar_home_description = 0x7f0a0001; public static final int abc_action_bar_home_description_format = 0x7f0a0005; public static final int abc_action_bar_home_subtitle_description_format = 0x7f0a0006; } public static final class style { public static final int AlertDialog_AppCompat = 0x7f0b0040; public static final int AlertDialog_AppCompat_Light = 0x7f0b0041; } [/java] To run the app from Eclipse, first click on the project file and click the Run icon from the tool bar. Eclipse installs the app on your AVD and starts running the application. If the app is error free, the following emulator window is displayed as an output. Android Screen Orientation: The screenOrientation is an attribute of the activity. The orientation can be
  • portrait:taller • landscape:wider • unspecified:Its the default value in this case system chooses the orientation. • sensor: orientation is determined by the device orientation sensor.
You need to define it in the AndroidManifest.xml or by code. AndroidManifest.xml: [code lang="xml"] <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.spl.firstproject" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="16" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" // android:screenOrientation="landscape" android:screenOrientation="portrait" android:theme="@style/AppTheme" > <activity android:name="com.spl.firstproject.MainActivity" 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>[/code] By Code:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
OR
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Output: portrait mode: landscape mode: