Chapter Two-Layouts
Chapter Two-Layouts
Layout
✓ Views and View Groups
✓ Create String and Color files
✓ Access Views in Activity
✓ Difference between View Groups
✓ Menu
Activity
✓ Activity Life Cycle
Multiple Activities and Intent
✓ Pass and exchange information between activities
List Views and Adapters
Dialogs
Fragments
Views and View Groups
A Layout defines what a screen looks like (user Interface) defined
using XML.
Layouts usually contain GUI components such as buttons and text
fields.
The android apps will contain one or more activities and each activity
is a one screen of app.
The user interface in android app is made with a collection of view
and view Groups.
All layouts and GUI components are subclasses of the Android View
class.
Cont’d
<resources>
<color name=“colorprimary">#3F51B5</color>
</resources>
Use the Color resource in your layout
android:background="@color/ colorprimary" />
Most Common View Components
Text View:
❖ A Text view is used for displaying text. Here is how to define a Text
view in XML.
import package_name.databinding.ActivityMainBinding
ActivityMainBinding binding; // creating variable
binding = ActivityMainBinding.inflate(getLayoutInflater());
Accessing the views by using the reference to the binding.
TextView textView = binding.textview
textView.setText(“Some other String”);
Editable Text View (Edit Text):
❖ This is like a text view, but editable. We can use it in case of letting
the user to enter information.
❖ Here is how to define an Edit Text in XML.
<EditText Value Function
android:id="@+id/edit_text"
android:layout_width="wrap_content" Phone Provides a phone number keypad
android:layout_height="wrap_content"
android:hint="@string/edit_text" /> textPassword Displays a text entry keypad, and your
❖ To define what type of data you’re input is concealed.
expecting the user to enter we can use
textCapSentences Capitalizes the first word of a sentence.
android:inputType= “number”
❖ You can specify multiple input types
textAutoCorrect Automatically corrects the text being
using the | character input.
E.g. android:inputType="textCapSentences|textAutoCorrect"
❖Activity code for Accessing Edit Text
EditText editText = Alternatively, Using View Binding it can be
findViewById(R.id.edit_text); expressed as
String text = editText.getText().toString(); EditText editText = binding.edit_text;
String text = editText.getText().toString();
Button:
❖ Buttons are usually used to make your app do something when
they’re clicked.
❖Here is how to define a Button in XML.
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_text" />
switch(view.getId()) {
case R.id.checkbox_milk:
if (checked) //
else //
break;
case R.id.checkbox_sugar:
if (checked) //
else //
break;}}
Radio Button:
❖ These let you display multiple options to the user. The user can
select a single option.
❖ Here is how to define a Radio Button in XML.
<RadioGroup
android:id="@+id/radio_group" Activity Code for Accessing
android:layout_width="match_parent"
android:layout_height="wrap_content" RadioButton
android:orientation="vertical"> public void onRadioButtonClicked(View view) {
<RadioButton
android:id="@+id/radio_male" RadioGroup radioGroup = (RadioGroup)
android:layout_width="wrap_content" findViewById(R.id.radioGroup);
android:layout_height="wrap_content"
android:text="@string/Male“ int id = radioGroup.getCheckedRadioButtonId();
android:onClick="onRadioButtonClicked" switch(id) {
/>
<RadioButton case R.id.radio_male:
android:id="@+id/radio_female"
//
android:layout_width="wrap_content"
android:layout_height="wrap_content" break;
android:text="@string/Female"
android:onClick="onRadioButtonClicked" case R.id.radio_female:
/> //
</RadioGroup>
break; }}
Spinner:
❖ a spinner gives you a drop-down list of values from which only one
can be selected.
❖Here is how to define a Spinner in XML.
<Spinner
android:id="@+id/spinner" String.xml
android:layout_width="wrap_content" <string-array name="spinner_values">
android:layout_height="wrap_content" <item>Red</item>
android:entries="@array/spinner_values“ /> <item>White</item>
<item>Black</item>
<item>Blue</item>
</string-array>
Submit (Button)
✓ The above code displays the text inside the Button at the top of the
Button.
❖ Layout Gravity: controls the position of a view within a layout. We
can achieve this by using android:layout_gravity attribute.
✓ android:layout_gravity deals with the placement of the view itself,
and lets you control where views appear in their available space.
For e.g., android: layout_gravity= “end”
❖ Linear Layout supports assigning a weight to individual children with
the android:layout_weight attribute.
❖ This attribute assigns an "importance" value to a view in terms of how
much space it should occupy on the screen. A larger weight value
allows it to expand to fill any remaining space in the parent view.
❖Try to develop the next Layout.
Linear Layout Example
Relative Layout
❖ is a view group that displays child views in relative positions.
❖ The position of each view can be specified as relative to sibling
element (such as to the left-of or below another view) or in positions
relative to the parent Relative Layout area (such as aligned to the
bottom, left or center).
❖ A Relative Layout is a very powerful utility for designing a user
interface because it can eliminate nested view groups and keep your
layout hierarchy flat. which improves performance.
❖ If you find yourself using several nested Linear Layout groups, you
may be able to replace them with a single Relative Layout.
Attributes for Relative layout
❖ android:layout_alignParentTop: If “true”, makes the top edge of this
view match the top edge of the parent.
❖ android:layout_centerVertical: If “true”, centers this child vertically
within its parent.
❖ android:layout_below: Positions the top edge of this view below the
view specified with a resource ID.
❖ android:layout_toRightOf: Positions the left edge of this view to the
right of the view specified with a resource ID.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<TextView
android:id="@+id/sign_in"
android:layout_width=“wrap_content"
android:layout_height="wrap_content“
android:layout_alignParentTop=“true”
android:layout_centerHorizontal=“true”/>
<TextView
android:id="@+id/user_name"
android:layout_width=“wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/sign_in"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/txt_user"/>
<TextView
android:id="@id/password"
android:layout_width=“wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_name"/>
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/pasword"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
Sign in (TextView)
(TextView)UserName: enter user name(Edit Text)
(TextView)Password: enter Password(Edit Text)
Login
Frame Layout:
❖We use Frame Layout when we want our views to overlap. For
instance, suppose we want to display an image with some text overlaid
on top of it.
❖ Frame Layout stack their views on top of each other.
❖ Frame Layout is designed to block out an area on the screen to display
a single item.
❖ We define Frame Layout using <FrameLayout> element.
❖ The android:layout_width and android:layout_height attributes are
mandatory
❖ It can be difficult to organize child views in a way that's scalable to
different screen sizes without the children overlapping each other. But
we can control their position using android: layout_gravity.
Constraint Layout:
❖ Constraint Layout is a View Group (i.e., a view that holds other views)
which allows you to create large and complex layouts with a flat view
hierarchy.
❖ Allows you to position and size widgets in a very flexible way. It was
created to help reduce the nesting of views and also improve the
performance of layout files.
Advantage
✓ you can perform animations on your Constraint Layout views with
very little code.
✓ You can build your complete layout with simple drag-and-drop on
the Android Studio design mode.
✓ You can control what happens to a group of widgets through a
single line of code.
✓ Dynamically position UI elements onscreen in relation to other
elements.
✓ Constraint Layout improve performance over others layout.
❖ Important points to use constraint Layout
✓ Your Android Studio version must be 2.3 or higher.
✓ You must first include the ConstraintLayout’s support library.
✓ You must have at least one vertical and one horizontal constraints.
❖ In general, Constraint Layout is a faster, better and much more
efficient choice to designing large and complex layouts in your
Android UI.