3 SimpleWidgets
3 SimpleWidgets
Contents
• Views
• ViewGroups
• View hierarchy
Views
• Everything you see is a view
• Examples of views Views
Button CheckBox
EditText RadioButton
Slider Switch
Android Widgets
Create views and layouts
● Android Studio layout editor: visual representation of XML
● XML editor
● Java code
Android Studio layout editor
• XML layout file
• Design and Text tabs
• Palette pane
• Component Tree
• Design and blueprint
panes
• Attributes tab
View 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"
/>
View attributes 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"
Create View in Java code
In an Activity:
• Button • AdapterView
• TextView - ListView
• EditText - Spinner
• RecyclerView
• ImageView
• …
• CheckBox
• RadioButton
• WebView
ViewGroups
● ViewGroup contains "child" views
ViewGroups
● Common Layout Classes
● ConstraintLayout: Connect views with constraints
● LinearLayout: Horizontal or vertical row
● TableLayout: Rows and columns
● FrameLayout: Shows one child of a stack of children
ViewGroups
• Common Layout Classes
ViewGrou
View View
p
linearL.addView(myText);
setContentView(linearL);
Set width and height in Java code
LinearLayout.LayoutParams layoutParams =
new Linear.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_CONTENT);
myView.setLayoutParams(layoutParams);
LinearLayout
• A box model – widgets or child containers are
lined up in a column or row, one after the next.
• Special attributes:
– orientation: indicates whether the LinearLayout
LinearLayout
represents a row or a column (vertical or horizontal)
– weight: indicates whether the LinearLayout represents
a row or a column
– gravity: is used to indicate how a control will align on
the screen
LinearLayout - Special attributes
• orientation
– Name: android:orientation
– Value: vertical, horizontal
LinearLayout - Special attributes
• weight
– Name: android:layout_weight
– Value: 1, 2, 3, …
LinearLayout - Special attributes
• gravity
– Name: android:layout_gravity
– Value: left, center, right, top, bottom, etc.
ConstraintLayout
Self-study
- https://developer.android.com/training/constraint-layout
- https://developer.android.com/codelabs/constraint-layout#0
Classwork
• Design one of Login Screens as below:
References
• https://pttrns.com/android-patterns
• https://google-developer-training.github.io/android-developer-funda
mentals-course-concepts-v2/