SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
UI - Layout
Topics
• Declaring Layout
• Layout File
• Attributes
> ID
> Layout parameters
• Types of Layout
> LinearLayout
> RelativeLayout
> TableLayout
> FrameLayout
> Tab layout
Declaring Layout
What is a Layout?
• Your layout is the architecture for the user
interface in an Activity.
• It defines the layout structure and holds all the
elements that appear to the user.
How to declare a Layout? Two Options
• Option #1: Declare UI elements in XML (most
common and preferred)
> Android provides a straightforward XML vocabulary
that corresponds to the View classes and subclasses,
such as those for UI controls called widgets
(TextView, Button, etc.) and layouts.
• Option #2: Instantiate layout elements at
runtime (in Java code)
> Your application can create View and ViewGroup
objects (and manipulate their properties)
programmatically (in Java code).
Example of using both options
• You can use either or both of these options for
declaring and managing your application's UI
• Example usage scenario
> You could declare your application's default layouts
in XML, including the screen elements that will
appear in them and their properties.
> You could then add code in your application that
would modify the state of the screen objects,
including those declared in XML, at run time.
Advantages of Option #1: Declaring UI
in XML
• Separation of the presentation from the code
that controls its behavior
> You can modify UI without having to modify your
source code and recompile
> For example, you can create XML layouts for
different screen orientations, different device screen
sizes, and different languages
• Easier to visualize the structure of your UI
(without writing any code)
> Easier to design/debug UI
> Visualizer tool (like the one in Eclipse IDE)
Layout File
Layout File Structure
• Each layout file must contain exactly one root
element, which must be a View (Button, for
example) or ViewGroup object (LinearLayout,
for example).
• Once you've defined the root element, you can
add additional layout objects or widgets (View)
as child elements to gradually build a View
hierarchy that defines your layout.
Example: Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>
Where to create Layout file?
• Save the file with the .xml extension, in your
Android project's res/layout/ directory
Load the Layout XML Resource
• When you compile your application, each XML
layout file is compiled into a View resource.
• You should load the layout resource from your
application code, in your Activity.onCreate()
callback implementation.
> By calling setContentView(), passing it the reference
to your layout resource in the form of:
R.layout.layout_file_name
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
}
Attributes
Attributes
• Every View and ViewGroup object supports
their own variety of attributes.
> Some attributes are specific to a View object (for
example, TextView supports the textSize attribute),
but these attributes are also inherited by any View
objects that may extend this class.
> Some are common to all View objects, because they
are inherited from the root View class (like the id
attribute).
> Other attributes are considered "layout parameters,"
which are attributes that describe certain layout
orientations of the View object, as defined by that
object's parent ViewGroup object.
> These attributes are typically in XML form
Attributes: ID
ID Attribute
• Any View object may have an integer ID
associated with it, to uniquely identify the View
within the tree.
• When the application is compiled, this ID is
referenced as an integer, but the ID is typically
assigned in the layout XML file as a string, in
the id attribute.
• Syntax
> android:id="@+id/my_button"
ID Attribute - Android Resource ID
• There are a number of other ID resources that
are offered by the Android framework.
• When referencing an Android resource ID, you
do not need the plus-symbol, but must add the
android package namespace
> android:id="@android:id/empty"
• With the android package namespace in place,
we're now referencing an ID from the android.R
resources class, rather than the local resources
class.
How to reference views in Java code?
• Assuming a view/widget is defined in the layout
file with a unique ID
<Button android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
• Then you can make a reference to the view
object via findViewById(R.id.<string-id>).
Button myButton = (Button) findViewById(R.id.my_button);
Attributes:
Layout Parameters
What Are Layout Parameters?
• XML layout attributes named layout_something
define layout parameters for the View that are
appropriate for the ViewGroup in which it
resides
• Every ViewGroup class implements a nested
class that extends ViewGroup.LayoutParams.
> This subclass contains property types that define the
size and position for each child view, as appropriate
for the view group.
Parent view group defines layout parameters for each child view
(including the child view group)
layout_width & layout_height
• wrap_content
> tells your view to size itself to the dimensions
required by its content
• fill_parent
> tells your view to become as big as its parent view
group will allow.
• match_parent
> Same as fill_parent
> Introduced in API Level 8
<Button android:id="@+id/my_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/my_button_text"/>
layout_weight
• Is used in LinearLayouts to assign "importance"
to Views within the layout.
• All Views have a default layout_weight of zero
• Assigning a value higher than zero will split up
the rest of the available space in the parent
View, according to the value of each View's
layout_weight and its ratio to the overall
layout_weight specified in the current layout for
this and other View elements.
Layout Types
Layout Types
• All layout types are subclass of ViewGroup class
• Layout types
> LinearLayout
> RelativeLayout
> TableLayout
> FrameLayout
> Tab layout
LinearLayout
• Aligns all children in a single direction —
vertically or horizontally, depending on how you
define the orientation attribute.
• All children are stacked one after the other, so
a vertical list will only have one child per row,
no matter how wide they are
• A LinearLayout respects
> margins between children
> gravity (right, center, or left alignment) of each child.
> weight to each child
RelativeLayout
• RelativeLayout lets child views specify their
position relative to the parent view or to each
other (specified by ID)
> You can align two elements by right border, or make
one below another, centered in the screen, centered
left, and so on
• Elements are rendered in the order given, so if
the first element is centered in the screen,
other elements aligning themselves to that
element will be aligned relative to screen
center.
RelativeLayout Example
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue"
android:padding="10px" >
<TextView android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Type here:" />
<EditText android:id="@+id/entry"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/editbox_background"
android:layout_below="@id/label" />
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
android:layout_marginLeft="10px"
android:text="OK" />
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/ok"
android:layout_alignTop="@id/ok"
android:text="Cancel" />
</RelativeLayout>
TableLayout
• TableLayout positions its children into rows and
columns
• TableRow objects are the child views of a
TableLayout (each TableRow defines a single
row in the table).
• Each row has zero or more cells, each of which
is defined by any kind of other View.
• Columns can be hidden, marked to stretch and
fill the available screen space, or can be
marked as shrinkable to force the column to
shrink until the table fits the screen.
TableLayout Example
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="@string/table_layout_4_open"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_open_shortcut"
android:gravity="right"
android:padding="3dip" />
</TableRow>
<TableRow>
<TextView
android:text="@string/table_layout_4_save"
android:padding="3dip" />
<TextView
android:text="@string/table_layout_4_save_shortcut"
android:gravity="right"
android:padding="3dip" />
</TableRow>
</TableLayout>
FrameLayout
• FrameLayout is the simplest type of layout
object. It's basically a blank space on your
screen that you can later fill with a single object
> For example, a picture that you'll swap in and out.
• All child elements of the FrameLayout are
pinned to the top left corner of the screen; you
cannot specify a different location for a child
view.
> Subsequent child views will simply be drawn over
previous ones, partially or totally obscuring them
(unless the newer object is transparent).
FrameLayout Example
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="yellowyellowyellow"
android:gravity="bottom"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="120dip"/>
<TextView
android:text="greengreengreen"
android:gravity="bottom"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="90dip" />
<TextView
android:text="blueblueblue"
android:gravity="bottom"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="60dip" />
<TextView
android:text="redredred"
android:gravity="bottom"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="30dip"/>
</FrameLayout>
Thank you

Weitere ähnliche Inhalte

Was ist angesagt?

Android application structure
Android application structureAndroid application structure
Android application structureAlexey Ustenko
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in androidPrawesh Shrestha
 
Layouts in android
Layouts in androidLayouts in android
Layouts in androidDurai S
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android DivyaKS12
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming BasicsEueung Mulyana
 
Introduction To Mobile Application Development
Introduction To Mobile Application DevelopmentIntroduction To Mobile Application Development
Introduction To Mobile Application DevelopmentSyed Absar
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android StudioSuyash Srijan
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studioParinita03
 
android layouts
android layoutsandroid layouts
android layoutsDeepa Rani
 
Presentation on Android application
Presentation on Android applicationPresentation on Android application
Presentation on Android applicationAtibur Rahman
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
Android application development ppt
Android application development pptAndroid application development ppt
Android application development pptGautam Kumar
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event HandlingNikmesoft Ltd
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on AndroidJomar Tigcal
 
Android studio 2.0: default project structure
Android studio 2.0: default project structureAndroid studio 2.0: default project structure
Android studio 2.0: default project structureVyara Georgieva
 
Android studio installation
Android studio installationAndroid studio installation
Android studio installationPoojaBele1
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android pptTaha Malampatti
 
Mobile application development ppt
Mobile application development pptMobile application development ppt
Mobile application development ppttirupathinews
 

Was ist angesagt? (20)

Android application structure
Android application structureAndroid application structure
Android application structure
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
Android Layout.pptx
Android Layout.pptxAndroid Layout.pptx
Android Layout.pptx
 
Android Location and Maps
Android Location and MapsAndroid Location and Maps
Android Location and Maps
 
Layouts in android
Layouts in androidLayouts in android
Layouts in android
 
UI controls in Android
UI controls in Android UI controls in Android
UI controls in Android
 
Android Programming Basics
Android Programming BasicsAndroid Programming Basics
Android Programming Basics
 
Introduction To Mobile Application Development
Introduction To Mobile Application DevelopmentIntroduction To Mobile Application Development
Introduction To Mobile Application Development
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
 
Creating the first app with android studio
Creating the first app with android studioCreating the first app with android studio
Creating the first app with android studio
 
android layouts
android layoutsandroid layouts
android layouts
 
Presentation on Android application
Presentation on Android applicationPresentation on Android application
Presentation on Android application
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Android application development ppt
Android application development pptAndroid application development ppt
Android application development ppt
 
[Android] Widget Event Handling
[Android] Widget Event Handling[Android] Widget Event Handling
[Android] Widget Event Handling
 
Location-Based Services on Android
Location-Based Services on AndroidLocation-Based Services on Android
Location-Based Services on Android
 
Android studio 2.0: default project structure
Android studio 2.0: default project structureAndroid studio 2.0: default project structure
Android studio 2.0: default project structure
 
Android studio installation
Android studio installationAndroid studio installation
Android studio installation
 
Introduction to Android ppt
Introduction to Android pptIntroduction to Android ppt
Introduction to Android ppt
 
Mobile application development ppt
Mobile application development pptMobile application development ppt
Mobile application development ppt
 

Andere mochten auch

Android Layout
Android LayoutAndroid Layout
Android Layoutmcanotes
 
Android networking in Hindi
Android networking in Hindi Android networking in Hindi
Android networking in Hindi Vipin sharma
 
Learn java in hindi
Learn java in hindiLearn java in hindi
Learn java in hindiVipin sharma
 
Lecture Slides for List Views [Android ]
Lecture Slides for List Views [Android ]Lecture Slides for List Views [Android ]
Lecture Slides for List Views [Android ]Nehil Jain
 
Making your ui look good on android
Making your ui look good on androidMaking your ui look good on android
Making your ui look good on androidthe100rabh
 
Best Practices for Android UI by RapidValue Solutions
Best Practices for Android UI by RapidValue SolutionsBest Practices for Android UI by RapidValue Solutions
Best Practices for Android UI by RapidValue SolutionsRapidValue
 
Intent in android
Intent in androidIntent in android
Intent in androidDurai S
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversCodeAndroid
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application DevelopmentBenny Skogberg
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project PresentationLaxmi Kant Yadav
 

Andere mochten auch (18)

Android Ui
Android UiAndroid Ui
Android Ui
 
Android Layout
Android LayoutAndroid Layout
Android Layout
 
Resume1
Resume1Resume1
Resume1
 
Android networking in Hindi
Android networking in Hindi Android networking in Hindi
Android networking in Hindi
 
Learn java in hindi
Learn java in hindiLearn java in hindi
Learn java in hindi
 
Input and Output Control
Input and Output ControlInput and Output Control
Input and Output Control
 
Java
JavaJava
Java
 
Lecture Slides for List Views [Android ]
Lecture Slides for List Views [Android ]Lecture Slides for List Views [Android ]
Lecture Slides for List Views [Android ]
 
Making your ui look good on android
Making your ui look good on androidMaking your ui look good on android
Making your ui look good on android
 
Best Practices for Android UI by RapidValue Solutions
Best Practices for Android UI by RapidValue SolutionsBest Practices for Android UI by RapidValue Solutions
Best Practices for Android UI by RapidValue Solutions
 
Intent in android
Intent in androidIntent in android
Intent in android
 
Android: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast ReceiversAndroid: Intent, Intent Filter, Broadcast Receivers
Android: Intent, Intent Filter, Broadcast Receivers
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android studio
Android studioAndroid studio
Android studio
 
Android UI Design Tips
Android UI Design TipsAndroid UI Design Tips
Android UI Design Tips
 
Android ppt
Android pptAndroid ppt
Android ppt
 
Android Application Development
Android Application DevelopmentAndroid Application Development
Android Application Development
 
Android Project Presentation
Android Project PresentationAndroid Project Presentation
Android Project Presentation
 

Ähnlich wie Android ui layout

Building a simple user interface lesson2
Building a simple user interface lesson2Building a simple user interface lesson2
Building a simple user interface lesson2Kalluri Vinay Reddy
 
View groups containers
View groups containersView groups containers
View groups containersMani Selvaraj
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-TejamFandat
 
Android training day 2
Android training day 2Android training day 2
Android training day 2Vivek Bhusal
 
01 09 - graphical user interface - basic widgets
01  09 - graphical user interface - basic widgets01  09 - graphical user interface - basic widgets
01 09 - graphical user interface - basic widgetsSiva Kumar reddy Vasipally
 
Android Training (Android UI)
Android Training (Android UI)Android Training (Android UI)
Android Training (Android UI)Khaled Anaqwa
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfacesC.o. Nieto
 
Android Screen Containers & Layouts
Android Screen Containers & LayoutsAndroid Screen Containers & Layouts
Android Screen Containers & LayoutsVijay Rastogi
 
Android Tutorial
Android TutorialAndroid Tutorial
Android TutorialFun2Do Labs
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - AndroidWingston
 
Android layouts course-in-mumbai
Android layouts course-in-mumbaiAndroid layouts course-in-mumbai
Android layouts course-in-mumbaivibrantuser
 
Android | Busy Java Developers Guide to Android: UI | Ted Neward
Android | Busy Java Developers Guide to Android: UI | Ted NewardAndroid | Busy Java Developers Guide to Android: UI | Ted Neward
Android | Busy Java Developers Guide to Android: UI | Ted NewardJAX London
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_programEyad Almasri
 

Ähnlich wie Android ui layout (20)

Building a simple user interface lesson2
Building a simple user interface lesson2Building a simple user interface lesson2
Building a simple user interface lesson2
 
View groups containers
View groups containersView groups containers
View groups containers
 
Ui 5
Ui   5Ui   5
Ui 5
 
mobile application development -unit-3-
mobile application development  -unit-3-mobile application development  -unit-3-
mobile application development -unit-3-
 
Android training day 2
Android training day 2Android training day 2
Android training day 2
 
01 09 - graphical user interface - basic widgets
01  09 - graphical user interface - basic widgets01  09 - graphical user interface - basic widgets
01 09 - graphical user interface - basic widgets
 
Android Training (Android UI)
Android Training (Android UI)Android Training (Android UI)
Android Training (Android UI)
 
Android UI Development
Android UI DevelopmentAndroid UI Development
Android UI Development
 
04 user interfaces
04 user interfaces04 user interfaces
04 user interfaces
 
01 08 - graphical user interface - layouts
01  08 - graphical user interface - layouts01  08 - graphical user interface - layouts
01 08 - graphical user interface - layouts
 
Android Screen Containers & Layouts
Android Screen Containers & LayoutsAndroid Screen Containers & Layouts
Android Screen Containers & Layouts
 
Android programming basics
Android programming basicsAndroid programming basics
Android programming basics
 
Android Application Fundamentals.
Android Application Fundamentals.Android Application Fundamentals.
Android Application Fundamentals.
 
Android xml-based layouts-chapter5
Android xml-based layouts-chapter5Android xml-based layouts-chapter5
Android xml-based layouts-chapter5
 
Android Tutorial
Android TutorialAndroid Tutorial
Android Tutorial
 
03 layouts & ui design - Android
03   layouts & ui design - Android03   layouts & ui design - Android
03 layouts & ui design - Android
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Android layouts course-in-mumbai
Android layouts course-in-mumbaiAndroid layouts course-in-mumbai
Android layouts course-in-mumbai
 
Android | Busy Java Developers Guide to Android: UI | Ted Neward
Android | Busy Java Developers Guide to Android: UI | Ted NewardAndroid | Busy Java Developers Guide to Android: UI | Ted Neward
Android | Busy Java Developers Guide to Android: UI | Ted Neward
 
Lec005 android start_program
Lec005 android start_programLec005 android start_program
Lec005 android start_program
 

Mehr von Krazy Koder (20)

2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xd
2310 b xd2310 b xd
2310 b xd
 
2310 b xc
2310 b xc2310 b xc
2310 b xc
 
2310 b xb
2310 b xb2310 b xb
2310 b xb
 
2310 b 17
2310 b 172310 b 17
2310 b 17
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 16
2310 b 162310 b 16
2310 b 16
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 15
2310 b 152310 b 15
2310 b 15
 
2310 b 14
2310 b 142310 b 14
2310 b 14
 
2310 b 13
2310 b 132310 b 13
2310 b 13
 
2310 b 12
2310 b 122310 b 12
2310 b 12
 
2310 b 11
2310 b 112310 b 11
2310 b 11
 
2310 b 10
2310 b 102310 b 10
2310 b 10
 
2310 b 09
2310 b 092310 b 09
2310 b 09
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 08
2310 b 082310 b 08
2310 b 08
 
2310 b 07
2310 b 072310 b 07
2310 b 07
 

Kürzlich hochgeladen

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

Android ui layout

  • 2. Topics • Declaring Layout • Layout File • Attributes > ID > Layout parameters • Types of Layout > LinearLayout > RelativeLayout > TableLayout > FrameLayout > Tab layout
  • 4. What is a Layout? • Your layout is the architecture for the user interface in an Activity. • It defines the layout structure and holds all the elements that appear to the user.
  • 5. How to declare a Layout? Two Options • Option #1: Declare UI elements in XML (most common and preferred) > Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for UI controls called widgets (TextView, Button, etc.) and layouts. • Option #2: Instantiate layout elements at runtime (in Java code) > Your application can create View and ViewGroup objects (and manipulate their properties) programmatically (in Java code).
  • 6. Example of using both options • You can use either or both of these options for declaring and managing your application's UI • Example usage scenario > You could declare your application's default layouts in XML, including the screen elements that will appear in them and their properties. > You could then add code in your application that would modify the state of the screen objects, including those declared in XML, at run time.
  • 7. Advantages of Option #1: Declaring UI in XML • Separation of the presentation from the code that controls its behavior > You can modify UI without having to modify your source code and recompile > For example, you can create XML layouts for different screen orientations, different device screen sizes, and different languages • Easier to visualize the structure of your UI (without writing any code) > Easier to design/debug UI > Visualizer tool (like the one in Eclipse IDE)
  • 9. Layout File Structure • Each layout file must contain exactly one root element, which must be a View (Button, for example) or ViewGroup object (LinearLayout, for example). • Once you've defined the root element, you can add additional layout objects or widgets (View) as child elements to gradually build a View hierarchy that defines your layout.
  • 10. Example: Layout File <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a TextView" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, I am a Button" /> </LinearLayout>
  • 11. Where to create Layout file? • Save the file with the .xml extension, in your Android project's res/layout/ directory
  • 12. Load the Layout XML Resource • When you compile your application, each XML layout file is compiled into a View resource. • You should load the layout resource from your application code, in your Activity.onCreate() callback implementation. > By calling setContentView(), passing it the reference to your layout resource in the form of: R.layout.layout_file_name public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); }
  • 14. Attributes • Every View and ViewGroup object supports their own variety of attributes. > Some attributes are specific to a View object (for example, TextView supports the textSize attribute), but these attributes are also inherited by any View objects that may extend this class. > Some are common to all View objects, because they are inherited from the root View class (like the id attribute). > Other attributes are considered "layout parameters," which are attributes that describe certain layout orientations of the View object, as defined by that object's parent ViewGroup object. > These attributes are typically in XML form
  • 16. ID Attribute • Any View object may have an integer ID associated with it, to uniquely identify the View within the tree. • When the application is compiled, this ID is referenced as an integer, but the ID is typically assigned in the layout XML file as a string, in the id attribute. • Syntax > android:id="@+id/my_button"
  • 17. ID Attribute - Android Resource ID • There are a number of other ID resources that are offered by the Android framework. • When referencing an Android resource ID, you do not need the plus-symbol, but must add the android package namespace > android:id="@android:id/empty" • With the android package namespace in place, we're now referencing an ID from the android.R resources class, rather than the local resources class.
  • 18. How to reference views in Java code? • Assuming a view/widget is defined in the layout file with a unique ID <Button android:id="@+id/my_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/my_button_text"/> • Then you can make a reference to the view object via findViewById(R.id.<string-id>). Button myButton = (Button) findViewById(R.id.my_button);
  • 20. What Are Layout Parameters? • XML layout attributes named layout_something define layout parameters for the View that are appropriate for the ViewGroup in which it resides • Every ViewGroup class implements a nested class that extends ViewGroup.LayoutParams. > This subclass contains property types that define the size and position for each child view, as appropriate for the view group.
  • 21. Parent view group defines layout parameters for each child view (including the child view group)
  • 22. layout_width & layout_height • wrap_content > tells your view to size itself to the dimensions required by its content • fill_parent > tells your view to become as big as its parent view group will allow. • match_parent > Same as fill_parent > Introduced in API Level 8 <Button android:id="@+id/my_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/my_button_text"/>
  • 23. layout_weight • Is used in LinearLayouts to assign "importance" to Views within the layout. • All Views have a default layout_weight of zero • Assigning a value higher than zero will split up the rest of the available space in the parent View, according to the value of each View's layout_weight and its ratio to the overall layout_weight specified in the current layout for this and other View elements.
  • 25. Layout Types • All layout types are subclass of ViewGroup class • Layout types > LinearLayout > RelativeLayout > TableLayout > FrameLayout > Tab layout
  • 26. LinearLayout • Aligns all children in a single direction — vertically or horizontally, depending on how you define the orientation attribute. • All children are stacked one after the other, so a vertical list will only have one child per row, no matter how wide they are • A LinearLayout respects > margins between children > gravity (right, center, or left alignment) of each child. > weight to each child
  • 27. RelativeLayout • RelativeLayout lets child views specify their position relative to the parent view or to each other (specified by ID) > You can align two elements by right border, or make one below another, centered in the screen, centered left, and so on • Elements are rendered in the order given, so if the first element is centered in the screen, other elements aligning themselves to that element will be aligned relative to screen center.
  • 28. RelativeLayout Example <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/blue" android:padding="10px" > <TextView android:id="@+id/label" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Type here:" /> <EditText android:id="@+id/entry" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@android:drawable/editbox_background" android:layout_below="@id/label" /> <Button android:id="@+id/ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/entry" android:layout_alignParentRight="true" android:layout_marginLeft="10px" android:text="OK" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/ok" android:layout_alignTop="@id/ok" android:text="Cancel" /> </RelativeLayout>
  • 29. TableLayout • TableLayout positions its children into rows and columns • TableRow objects are the child views of a TableLayout (each TableRow defines a single row in the table). • Each row has zero or more cells, each of which is defined by any kind of other View. • Columns can be hidden, marked to stretch and fill the available screen space, or can be marked as shrinkable to force the column to shrink until the table fits the screen.
  • 30. TableLayout Example <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchColumns="1"> <TableRow> <TextView android:text="@string/table_layout_4_open" android:padding="3dip" /> <TextView android:text="@string/table_layout_4_open_shortcut" android:gravity="right" android:padding="3dip" /> </TableRow> <TableRow> <TextView android:text="@string/table_layout_4_save" android:padding="3dip" /> <TextView android:text="@string/table_layout_4_save_shortcut" android:gravity="right" android:padding="3dip" /> </TableRow> </TableLayout>
  • 31. FrameLayout • FrameLayout is the simplest type of layout object. It's basically a blank space on your screen that you can later fill with a single object > For example, a picture that you'll swap in and out. • All child elements of the FrameLayout are pinned to the top left corner of the screen; you cannot specify a different location for a child view. > Subsequent child views will simply be drawn over previous ones, partially or totally obscuring them (unless the newer object is transparent).
  • 32. FrameLayout Example <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="yellowyellowyellow" android:gravity="bottom" android:background="#aaaa00" android:layout_width="wrap_content" android:layout_height="120dip"/> <TextView android:text="greengreengreen" android:gravity="bottom" android:background="#00aa00" android:layout_width="wrap_content" android:layout_height="90dip" /> <TextView android:text="blueblueblue" android:gravity="bottom" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="60dip" /> <TextView android:text="redredred" android:gravity="bottom" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="30dip"/> </FrameLayout>