Chapter 3 UI Components and Layouts
Chapter 3 UI Components and Layouts
OR
+ src: This folder contains the java source files for the project. In this example. there is one file.
Androidl’rojeciAciivity.java. The AndroidProjectActivity.java file is the source file for (he
activity. We write the code for this application in this .java file, The Java file is listed under (he
package name i.e. com.pkg.example for the project.
+ gen: This folder contains the R.java file, which is generate d by the compiler that references all
the resources found in this project. We cannot modify this file. All the resources in this project are
automatically compiled into this class hence we can transfer to them using the class.
+ Android libraries: This thing contains one file. android.jar, which contains all the class libraries
needed for an Android application.
+ assets: Folder contains all the resources used by this application, such as HTML,. text files.
databases, etc.,
+ bIn: This folder contains the files built by the AUT during the compilation. It generates the .apk file
(Android Package). An .apk file is the application binary of an Android application. It contains (he
entire things needed to run an Android application.
+ res: This folder contains all the resources files such as pictures, XML files for defining layouts used
in this application. It also contains some other subfolders: drawable<resolution>, layout. and values.
It can also support devices with different screen resolutions and densities.
+ AndroidManifest.xml: This is the manifest file for this Android project. ¡t contains information
about the Android application such as minimum Android version, permission to access Android
device capabilities such as internet access permission .ability to use phone permission, etc. Here we
specify the permissions and other features like intent-filters, receivers. etc required by application.
+ main.xml: The main.xml file defines the user interface for activity.
+ Layout: Contains XML layout files. Layout files are XML files which define how various Android
objects (such as textboxes. Buttons. etc) are organized on the screen.
+ Values: XML tiles which store various string values (titles. labels. etc).
During this runtime. we can load the XML UI in the onCreate () event handler in our Activity
class, using the setConteritView () method of thc Activity class:
@Override
public void onCreate(Bundle savedlnstanceState) {
super.onCreate (savedlnstanceState);
setContentView(R.layout.main);
}
During compilation time, each element in the XML file is compiled into its equivalent Android GUI
class, with an attribute represented by methods. The Android system then creates the user
interface(UI) of the Activity when it is loaded.
Views : Views are the base class for all visual interface elements (commonly known as
controls
or widgets). All user interface UI controls, including the layout classes, are derived from View.
A View is an object/widget that draws something on the screen by the help of user interact.
View Groups : View Groups are extensions of the View class that can contain multiple
child Views, In order to Extend the ViewGroup class to create compound controls made up
of interconnected child Views. The ViewGroup class is also extended to provide the Layout
Managers that help us to control layout within our Activities. AViewGroup provides the layout
in which we can order the appearance and sequence of views. Examples of View groups are
FrameLayout, LinearLayout, etc.
Fragments : Fragments, introduced in Android 3.0 which uses API level 11, are used to
encapsulate portions of your UI. This encapsulation makes Fragments particularly useful
when optimizing your UI layouts for different screen sizes and creating reusable user interface
(UI) elements. Each Fragment includes its own user interface (UI) layout arid receives the
related input events but is tightly bound to the Activity into which each must be embedded.
Fragments are similar to UI View Controllers in iPhone development.
Activities : Activities, represent a single screen that user interact .Activities are the Android
equivalent of Forms in traditional Windows desktop development. To display a UI, we assign
a View (usually a layout or Fragment) to an Activity.
ii. Border Layout: This is default layout of frame where components are added in north. south.
west and center direction.
Now this default position is border layout.center. If we are adding any component in this
layout. It occupies the total center position.
iii. Grid Layout: This layout divides the frame into row and column.
iv. Card Layout: This layout clicks the first panel so that second panel is visible and goes on.
v. Box Layout: This layout is mostly found in case of swing and in this layout panel are added
in vertical manner to a box and then box is added to frame so that the total content is now
Visible.
+ Linear layouts are one of the simplest and common types of layouts used by Android
developers to keep controls within their user interfaces. The linear layout works as much as
its name implies, it organizes the controls either a vertical or horizontal pattern. When the
layout’s orientation is set to vertical, all child controls within organized in a single column;
and when the layout’s orientation is set to horizontal, all child controls within in a single row.
+ Linear layouts can be defined within XML layout resources or programmatically in the
application’s Java code.
+ This linear layout’s orientation is set lo be vertical, causing each of ihe view controls to display
in a single column from top to bottom. Each TextView control has its text attribute set to a
color, with the same color set as the background for the control: each control is stretched to the
width of the screen by setting the controls’ layout_width attribute to fill_parent.
+ We have seen that android mobile screen size are different from device to device, sorne mobile
sizes are same. Suppose we are developing one application which contains a text field and
button, when the application changes from one mobile to the other mobile then the problem is
regarding the layout of the component. It happens because the size of screen varies.
fill_parent _width
Change the “layout width” to “fill parent”, now, the button’s width will fill in the remaining
spaces, just as big as it’s parent “RelativeLayouL”, but button’s height is still big enough to enclose its
content only.
OUTPUT
We should note that in the child LinearLayout elements, we used android:layout_height
”wrap_content” instead of fill parent. 1f we use fill parent the first name TextView and EditView
would have taken all of the available space on the screen, and the Last Name would not be
displayed. Here is what this XML displays:
Example 4:
We have to create an absolute layout in which layout width is fill_parent and layout height is fill
parent. Inside the absolute layout, we have to create one button named as Back, two textviews
named as First Name and ‘Last Name', and two EditTexts.
Example 6:
We have to create a frame layout in which width is wrap content and height is wrap content. Inside
the frame layout, we have to create an image view. We place an image inside the drawable folder.
An image view have one attribute called src, means the source of image we have to display in
Imageview.
These properties allow you to layout elements relative to other elements on screen. The value for
each element is the id of the element we are using to layout the new element. Every element that is
used in this way must have an ID defined using android:id=’@+id/viewname’ where viewname is
replaced with the desired id. You use “@id/viewname” to refer to an element by its id.
Example 9:
We have to create a table layout in which layout width is fil1parent and layout hieght is fillparent.
Inside the table layout. we have to create three table rows and a button. First and second table
rows contain one textview and one edit text. Third table row contains one check box and one radio
button.