Android Development — Part 2 User Interface
Now, that we have installed android studio, let’s get started with it. In this section, you will talk about tools for user interface.
Activity:- It is a single focused thing the user can interaction with an app, and are also central to how a user navigates within an app.
Layouts in Android studio:- A layout defines the visual structure for a user interface, such as the UI for an activity or app widget. Their are two way to define layouts:-
1.Declare UI elements in XML( Extensible Markup Language)
2.Instantiate layout elements at runtime
Common Layouts:
- Linear Layout:-
LinearLayout
is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with theandroid:orientation
attribute. - Relative Layout:-
RelativeLayout
is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parentRelativeLayout
area (such as aligned to the bottom, left or center).android:layout_alignParentTop is an attribure for RelativeLayout.
- . Frame Layout:- Frame Layout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that’s scalable to different screen sizes without the children overlapping each other.
- Constraint Layout:-
ConstraintLayout
allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). All the power ofConstraintLayout
is available directly from the Layout Editor's visual tools, because the layout API and the Layout Editor were specially built for each other. So you can build your layout withConstraintLayout
entirely by drag-and-dropping instead of editing the XML.
android.widget :
The widget package contains (mostly visual) UI elements to use on your Application screen. To create your own widget, extend View
or a subclass.
Some of the widgets used :-
- TextView:-
- ImageView
- ViewGroup
- Button
- Label
Example of an layout:-
<?xml version="1.0" encoding="utf-8"?>
<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" >
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<TextView
android:id="@+id/display_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
This layout uses RelativeLayout with EditText, TextView and Button as widgets. And those starting with android: gives attributes of the widget. Every attribute properties id defined between “ ”.F or Example android:id=”@+id/name” where
@ means it reads from res folder for project view ,
+means id is defined for first time
id defines resource type
name defines any name give to define id.
In android
dp => density independent pixels is used to defined size .
sp => scale independent pixels is used to define text size.
These only some of the terms for more detail information check android documentation.