Professional Documents
Culture Documents
Slide 4
Slide 4
Views
Unequal distribution
You can also create linear layouts where the child elements use different amounts of space on the
screen:
If there are three text fields and two of them declare a weight of 1, while the other is given no
weight, the third text field without weight doesn't grow. Instead, this third text field occupies only
the area required by its content. The other two text fields, on the other hand, expand equally to fill
the space remaining after all three fields are measured.
If there are three text fields and two of them declare a weight of 1, while the third field is then given
a weight of 2 (instead of 0), then it's now declared more important than both the others, so it gets
half the total remaining space, while the first two share the rest equally.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_chat"/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:textAppearance="?android:textAppearanc
eMedium"
android:hint="Send Message"/>
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_send"/>
</LinearLayout>
Android Relative Layout
Android Relative Layout enables you to specify how child views are positioned
relative to each other. The position of each view can be specified as relative to
sibling elements or relative to the parent.
android:layout_below
Positions the top edge of this view below the view specified with a resource ID.
Positioning Views
The value for each layout property is either a boolean to enable a
layout position relative to the parent RelativeLayout or an ID that
references another view in the layout against which the view
should be positioned.
Improve layout performance
Layouts are a key part of Android
applications that directly affect the
user experience. If implemented
poorly, your layout can make your
app memory-intensive with slow UIs.
The Android SDK includes tools to
help identify problems in your layout
performance. With this
documentation, you can
implement smooth scrolling
interfaces with a minimal memory
footprint.
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#C0C0C0"
android:text="CLAUSTROPHOBIA"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="#C0C0C0"
android:text="CLAUSTROPHOBIA"/>
Layout Attributes…..
Here width and height are the dimension of the layout/view which can be specified in
terms of dp (Density-independent Pixels), sp ( Scale-independent Pixels), pt ( Points which is
1/72 of an inch), px( Pixels), mm ( Millimeters) and finally in (inches).
You can specify width and height with exact measurements but more often, you will use
one of these constants to set the width or height −
android:layout_width=wrap_content tells your view to size itself to the dimensions required
by its content.
android:layout_width=fill_parent tells your view to become as big as its parent view.
Gravity attribute plays important role in positioning the view object and it can take one or
more (separated by '|') of the following constant values.
View Identification
A view object may have a unique ID assigned to it which will identify the View
uniquely within the tree. The syntax for an ID, inside an XML tag is −
android:id="@+id/my_button"
android:hint android:inputType
Hint text to display when the text is empty. The type of data being placed in a text field. Phone, Date,
Time, Number, Password etc.
android:text android:textSize
Text to display. Size of the text. Recommended dimension type for text is
"sp" for scaled-pixels (example: 15sp).
Code-Text View
- Button Control
A Button is a Push-button which can be pressed, or clicked, by the
user to perform an action.
Button Attributes
Following are the important attributes related to Button
control. You can check Android official documentation for
complete list of attributes and related methods which you
can use to change these attributes are run time.
Inherited from android.widget.TextView Class −
Button Code
Image-View
In Android, ImageView class is used to display an image file in application. Image file is easy
to use but hard to master in Android, because of the various screen sizes in Android devices.
An android is enriched with some of the best UI design widgets that allows us to build good
looking and attractive UI based application.
ImageView comes with different configuration options to support different scale types. Scale
type options are used for scaling the bounds of an image to the bounds of the imageview. Some
of them scaleTypes configuration properties are center, center_crop, fit_xy, fitStart etc.
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion" />
Attributes of ImageView
src: src is an attribute used to set a source file or you can say image
in your imageview to make your layout attractive
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion" /><!--set the source of an image view-->
In Java:
We can also set the source image at run time programmatically
in java class. For that we use setImageResource() method as shown in below
example code.
ImageView simpleImageView=(ImageView) findViewById(R.id.simpleImageView);
simpleImageView.setImageResource(R.drawable.lion);//set the source in java class
Attributes of ImageView
padding: padding attribute is used to set the padding from left, right, top or bottom of the
Imageview.
paddingRight: set the padding from the right side of the image view.
paddingLeft: set the padding from the left side of the image view.
paddingTop: set the padding from the top side of the image view.
paddingBottom: set the padding from the bottom side of the image view.
padding: set the padding from the all side’s of the image view.
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000"
android:src="@drawable/lion"
android:padding="30dp"/><!--set 30dp padding
from all the sides-->
Attributes of ImageView
<ImageView
android:id="@+id/simpleImageView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/lion"
android:scaleType="fitXY"/><!--set scale type fit xy-->
CENTER: center is a scale type used in android to
center the image to the ImageView but does not
scale the image.