Saturday, June 25, 2022

Layout Params in Android Studio

In this article we will talk about Layout Params in android studio. But first we need to know what is the layouts in android. A layout defines the visual structure for a user interface, such as an activity or widget. Moreover, we need to declare layouts in XML, including screen elements that will appear in it. Futhermore,we can add the code to the application to modify the state of screen objects at runtime, including those declared in XML.

Layout Params

Every single ViewGroup (e.g. LinearLayout, etc.) needs to store information about its children’s properties. These information are about the way its children are being laid out in the ViewGroup. Furthermore we can store these information in objects of a wrapper class ViewGroup.LayoutParams.

To include parameters specific to a particular layout type, ViewGroups use subclasses of ViewGroup.LayoutParams class. E.g. for LinearLayout it’s LinearLayout.LayoutParams.RelativeLayout it’s RelativeLayout.LayoutParams. CoordinatorLayout it’s CoordinatorLayout.LayoutParams.

Most of ViewGroups reutilize the ability to set margins for their children, so they do not subclass ViewGroup.LayoutParams directly, but they subclass ViewGroup.MarginLayoutParams instead (which itself is a subclass of ViewGroup.LayoutParams).

LayoutParams in xml

Based on the inflated layout the LayoutParams are created.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <TextView
 android:layout_width="wrap_content"
 android:layout_height="50dp"
 android:layout_gravity="right"
 android:gravity="bottom"
 android:text="Example text"
 android:textColor="@android:color/holo_green_dark"/>
 <ImageView
 android:layout_width="match_parent"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:background="@android:color/holo_green_dark"
 android:scaleType="centerInside"
 android:src="@drawable/example"/>
</LinearLayout>

All parameters that begin with layout_ specify how the enclosing layout should work. When the android system inflates the layout, it wraps the parameters in a proper LayoutParams object. Then the layout will use this object to properly position a particular View within the ViewGroup. Other attributes of a View are directly View-related and the View proceed it.

Getting LayoutParams object

etLayoutParams is a View’s method that allows to retrieve a current LayoutParams object.

Because the LayoutParams object is directly related to the enclosing ViewGroup. This method will return a non-null value only when the system attach view to the ViewGroup. You need to bare in mind that this object might not be present at all times. Especially you should not depend on having it inside View’s constructor.

public class ExampleView extends View {
 
 public ExampleView(Context context) {
 super(context);
 setupView(context);
 }
 public ExampleView(Context context, AttributeSet attrs) {
 super(context, attrs);
 setupView(context);
 }
 public ExampleView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 setupView(context);
 }
 private void setupView(Context context) {
 if (getLayoutParams().height == 50){ // DO NOT DO THIS!
 // This might produce NullPointerException
 doSomething();
 }
 }
 
 //...
}

If you want to depend on having LayoutParams object, you should use onAttachedToWindow method instead.

public class ExampleView extends View {
 public ExampleView(Context context) {
 super(context);
 }
public ExampleView(Context context, AttributeSet attrs) {
 super(context, attrs);
 }
 public ExampleView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);
 }
 @Override
 protected void onAttachedToWindow() {
 super.onAttachedToWindow();
 if (getLayoutParams().height == 50) { // getLayoutParams() will NOT return null here
 doSomething();
 }
 }
 //...
}

Casting LayoutParams object Programatically

You might need to use features that are specific to a particular ViewGroup (e.g. you might want to programmatically change rules of a RelativeLayout). For that purpose you will need to know how to properly cast the ViewGroup.LayoutParams object.

This might be a bit confusing when getting a LayoutParams object for a child View that actually is another ViewGroup.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/outer_layout"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <FrameLayout
 android:id="@+id/inner_layout"
 android:layout_width="match_parent"
 android:layout_height="50dp"
 android:layout_gravity="right"/>
</LinearLayout>

IMPORTANT: The type of LayoutParams object is directly related to the type of the ENCLOSING ViewGroup.

Incorrect casting:

FrameLayout innerLayout = (FrameLayout)findViewById(R.id.inner_layout);
FrameLayout.LayoutParams par = (FrameLayout.LayoutParams) innerLayout.getLayoutParams();
 // INCORRECT! This will produce ClassCastException

Correct casting:

FrameLayout innerLayout = (FrameLayout)findViewById(R.id.inner_layout);
LinearLayout.LayoutParams par = (LinearLayout.LayoutParams) innerLayout.getLayoutParams();
 // CORRECT! the enclosing layout is a LinearLayout

Gravity and layout gravity

We can use android:layout_gravity to set the position of an element in its Parent. Moreover, android:gravity sets the position of content inside an element.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 android:orientation="vertical">
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="vertical"
 android:layout_gravity="left"
 android:gravity="center_vertical">
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/first"
 android:background="@color/colorPrimary"
 android:gravity="left"/>
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/second"
 android:background="@color/colorPrimary"
 android:gravity="center"/>
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/third"
 android:background="@color/colorPrimary"
 android:gravity="right"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="vertical"
 android:layout_gravity="center"
 android:gravity="center_vertical">
<TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/first"
 android:background="@color/colorAccent"
 android:gravity="left"/>
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/second"
 android:background="@color/colorAccent"
 android:gravity="center"/>
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/third"
 android:background="@color/colorAccent"
 android:gravity="right"/>
 </LinearLayout>
 <LinearLayout
 android:layout_width="wrap_content"
 android:layout_height="0dp"
 android:layout_weight="1"
 android:orientation="vertical"
 android:layout_gravity="right"
 android:gravity="center_vertical">
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/first"
 android:background="@color/colorPrimaryDark"
 android:gravity="left"/>
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/second"
 android:background="@color/colorPrimaryDark"
 android:gravity="center"/>
 <TextView
 android:layout_width="@dimen/fixed"
 android:layout_height="wrap_content"
 android:text="@string/third"
 android:background="@color/colorPrimaryDark"
 android:gravity="right"/>
 </LinearLayout>
</LinearLayout>

Than You.

Learn more about LayoutParams

Read about XML layout

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -

Most Popular

Recent Comments

KU NUR RASYIDAH BINTI KU BIDE on Custom Calendar View Android | Project Java Files
SHIVANT KUMAR PANDEY on Login And Registration by Firebase
Adriane Troy U. Alariao on Custom Calendar View With Events Android