PK-Android User Interface
PK-Android User Interface
Interface
Contents
Views, view groups, and view hierarchy
Layouts in XML and Java code
Drawable Resources
Screen Measurements
Keep It Simple!
“The more users’ expectations prove right,
the more they will feel in control of the system
and the more they will like it.“
Jakob Nielson
View
Everything you see is a view
If you look at your mobile
device,
every user interface element
that you see is a View.
Views
What is a view?
Views are Android's basic user interface building
blocks.
display text (TextView class), edit text (EditText class)
buttons (Button class), menus, other controls
scrollable (ScrollView, RecyclerView)
show images (ImageView)
Button CheckBox
RadioButton
EditText
Switch
SeekBar
Creating and laying out
views
Graphically within Android Studio
XML Files
Programmatically
Views defined in XML
<TextView
android:id="@+id/show_count"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/myBackgroundColor"
android:text="@string/count_initial_value"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/count_text_size"
android:textStyle="bold"
/>
Mandatory attributes
View properties in XML
android:<property_name>="<property_value>"
Example: android:layout_width="match_parent"
android:<property_name>="@<resource_type>/resource_id"
Example: android:text="@string/button_label_next"
android:<property_name>="@+id/view_id"
Example: android:id="@+id/show_count"
Specifying size of View
Views and ViewGroups, at minimum, must have the
android:layout_width and android:layout_height
attributes
You can specify width and height with exact
measurements, though you probably won't want to do
this often. More often, you will use one of these
constants to set the width or height:
wrap_content tells your view to size itself to the dimensions
required by its content.
match_parent (named fill_parent before API Level 8) tells your
view to become as big as its parent view group will allow.
match_parent vs wrap_content
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
match_parent vs wrap_content
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
Specifying alignment of View
View alignment can be specified using View's
gravity and layout_gravity attributes
gravity is the alignment of the contents or child
Views inside of a View
layout_gravity is the alignment of the View
relative to its parent container
android:gravity
LinearLayout.LayoutParams layoutParams =
new Linear.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
myView.setLayoutParams(layoutParams);
View and
ViewGroup
Attributes
Attributes
Every View and ViewGroup object supports their own
variety of XML attributes.
Some attributes are specific to a View object
for example, TextView supports the textSize attribute
The at-symbol (@) at the beginning of the string indicates that the XML parser should
parse and expand the rest of the ID string and identify it as an ID resource.
The plus-symbol (+) means that this is a new resource name that must be created and
added to our resources (in the R.java file).
XML Layout Attributes
xmlns:android
namespace declaration that tells the Android tools that you are
going to refer to common attributes defined in the Android
namespace. The outer most tag in every Android layout file must
have this attribute.
android:layout_width
This attribute defines how much of the available width on the
screen this View should consume.
android:layout_height refers to available screen height.
android:text
This sets the text that the TextView should display.
Layout Attributes
Attribute Description
layout_width specifies width of View or ViewGroup
layout_height specifies height
layout_marginTop extra space on top
layout_marginBottom extra space on bottom side
layout_marginLeft extra space on left side
layout_marginRight extra space on right side
layout_gravity how child views are positioned
layout_weight how much extra space in layout should be allocated to
View (only when in LinearLayout or TableView)
layout_x x-coordinate
layout_y y-coordinate
Resources
Resources
Separate static data from code in your layouts.
Strings, dimensions, images, menu text, colors,
styles
Useful for localization
Where are the resources in
your project?
resources and
resource files
stored in res folder
Refer to resources in code
Layout:
R.layout.activity_main
setContentView(R.layout.activity_main);
View:
R.id.recyclerview
rv = (RecyclerView) findViewById(R.id.recyclerview);
String:
In Java: R.string.title
In XML: android:text="@string/title"
Measurements
Use device-independent units
Device Independent Pixels (dp) - for Views
Scale Independent Pixels (sp) - for text
FORM PENDAFTARAN
LOGO
Harus bisa
menghandle multi
screen (5 inch dan FirstName LastName
10 inch)
Password
SAVE
Praktikum 2
Buat Layout landscape dari Praktikum 1 untuk menghandle landscape
tanpa mempengaruhi layout portrait (praktikum 1). Lihat contoh layout
di halaman selanjutnya