Defining view components within a layout is a critical part of Android development. Let's take a look at common views as well as common attributes of views.
The six most common views are:
Any view can have an identifier attached that uniquely names that view for later access. You can assign a view an id within the XML layout:
<Button android:id="@+id/my_button" />
This id can then be accessed within the Java code for the corresponding activity (in onCreate
of Activity for example):
Button myButton = findViewById(R.id.my_button);
val myButton: Button = findViewById(R.id.my_button)
Another important note is that any view with an id specified will automatically retain its state on a configuration change (i.e phone orientation switch).
Every view has height and width properties controlling the size of the view. Height and width have to be defined in the XML for every view with:
<TextView
android:layout_width="165dp"
android:layout_height="wrap_content" />
This can take the form of wrap_content
(adjust height and width to the content size), match_parent
(adjust height and width to the full size of the parent container), and a dimensions value such as 120dp
. This can be changed at runtime in a number of ways:
// Change the width or height
int newInPixels = 50;
view.setLayoutParams(new LayoutParams(newInPixels, newInPixels));
// Trigger invalidation of the view to force adjustment
view.requestLayout();
// Change the width or height
val newInPixels = 50
view.layoutParams = ViewGroup.LayoutParams(newInPixels,newInPixels)
// Trigger invalidation of the view to force adjustment
view.requestLayout()
Or we can change just the width or height individually:
int newDimensionInPixels = 50;
view.getLayoutParams().width = newDimensionInPixels;
view.getLayoutParams().height = newDimensionInPixels;
// Trigger invalidation of the view to force adjustment
view.requestLayout();
val newDimensionInPixels = 50
view.layoutParams.width = newDimensionInPixels
view.layoutParams.height = newDimensionInPixels
// Trigger invalidation of the view to force adjustment
view.requestLayout()
We can also set these dimensions in dp
rather than pixels with:
int newDimensionInPixels = 50;
// convert to 50dp
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, newDimensionInPixels,
getResources().getDisplayMetrics());
view.getLayoutParams().width = newDimensionInPixels;
view.getLayoutParams().height = dimensionInDp;
// Trigger invalidation of the view to force adjustment
view.requestLayout();
val newDimensionInPixels = 50
// convert to 50dp
val dimensionInDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
newDimensionInPixels.toFloat(),resources.displayMetrics)
view.layoutParams.width = newDimensionInPixels
view.layoutParams.height = dimensionInDp
// Trigger invalidation of the view to force adjustment
view.requestLayout()
Margins and padding values for views allows us to position and space elements in a layout.
An example of setting margins and padding:
<LinearLayout>
<TextView android:layout_margin="5dp" android:padding="5dp">
<Button layout_marginBottom="5dp">
</LinearLayout>
Gravity can be used to define the direction of the contents of a view.
An example of applying gravity:
<TextView
android:gravity="left"
android:layout_gravity="right"
android:layout_width="165dp" android:layout_height="wrap_content"
android:textSize="12sp" android:text="@string/hello_world" />
Every view has many different attributes which can be applied to manage various properties.
android:layout_width
android:textColor
Common view attributes include:
Attribute | Description | Example Value |
---|---|---|
android:background |
Background for the view | #ffffff |
android:onClick |
Method to invoke when clicked | onButtonClicked |
android:visibility |
Controls how view appears | invisible |
android:hint |
Hint text to display when empty | @string/hint |
android:text |
Text to display in view | @string/foo |
android:textColor |
Color of the text | #000000 |
android:textSize |
Size of the text | 21sp |
android:textStyle |
Style of the text formatting | bold |
Review the View docs and TextView docs for a list of additional attributes. An example of setting view attributes:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:background="#000"
android:textColor="#fff"
android:layout_centerHorizontal="true"
/>