Monday, July 7, 2014

Android Training - Getting Started - Day 1

Few important items to note in the first application is 

AndroidManifest.xml is having tag which has android:minSdkVersion, android:targetSdkVersion values. The former should be lowest as possible to support various set of devices and the latter should be high as possible to target latest set of devices. 

src/ contains the source which includes the main Activity. res/ directory cottons the below folder 

/drawble- => drawable elements such as bitmaps for the designated screen density 
/layout => contains the layout xml files that defines app user interface 
/values => Contains collection of resources such as strings and colour values 

Running on real devices

Installing the device drives a good documentation is available at http://developer.android.com/tools/extras/oem-usb.html
To enable development mode, the instructions is given at : http://developer.android.com/training/basics/firstapp/running-app.html
to give a short note on this, on devices 3.2 or older, the debug option can be found under Settings -> Applications > Development 
on 4.0 and newer , Settings -> Developer options 
on 4.2 and newer Developer Options is hidden by default. To enable it, Settings -> About Phone, and tap build number seven times and now can return to the previous screen to find Developer Options

To install on to the device, 
1. Change Directories to the root of Android project and execute 

ant debug 

adb install bin/MyFirstApp-debug.apk 

User Interface

User interface is a collection of Views and Viewgroups. Below is the hierarchy. Views are ui widgets such as buttons, Text fields etc. ViewGroups are hidden elements by default and contains these views and defines the layout for these views. 

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

LinearLayout is a subclass of ViewGroup which lays out its child subviews in either vertical or horizontal orientation. 

To add a TextField to this, the below needs to be added in the XML file 

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message">

This above references edit_message property and it is from the strings resource file. The strings resource file can be modified like below so that it picks the value from this place. 

My First App
Enter a message

We can define the width occupied by a view using the layout weight property. The weight value is the amount of remaining space each view should consume. relative to the amount of space consumed by the sibling views. For e.g. if the application gives one view as weight of 2 and another weight of 1, the sum is 3. so the first view will fill the 2/3 of the space and second will fill the rest. now if we add third view with the weight value as 1, then the first view will get 2/4, i.e half of the remaining space while the other two will get 1/4 of the space. 

Default width of each view is 0. so, if we don't specify any weight for a view, then that widget will fill the remaining space after all views are given their requested space. 

With the above in hand, in order to improve the layout efficiency, application can provide the width of the view as 0dp so that the layout engine don't calculate the width and eventually discard it because it not used and overridden by the weight property. 

Inorder to invoke another Activity, application needs to build and intent. the Intent class is in the package android.content.Intent. 
The code to pass the display message to the new activity can be something like below 

Intent intent = new Intent(this, DisplayMessageActivity.class);
intent.putExtra(EXTRA_MESSAGE, message);

References: 
http://developer.android.com/training/basics/firstapp/starting-activity.html

No comments:

Post a Comment