Android Space Using Kotlin With Example

In this article, we will learn about android Space View using Kotlin. We will go through various example that demonstrates how to use different attributes of Space View. For example,

In this article, we will get answer to questions like –

  • What is Space View?
  • Why should we consider Space View while designing ui for any app?
  • What are possibilities using Space View while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Android Space Output Android Space Using Kotlin

Tutorialwing Android Space Output

Getting Started

We can define android Space View widget as below –

Android Space is lightweight View subclass that are used to create gaps between different components in any android application.

Now, how do we use Space View in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as SpaceView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android Space View Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using Space View in Kotlin

Follow steps below to use Space View in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">SpaceView</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?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:gravity="center"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:gravity="center"
            android:text="Line 1, notice the SPACE below it"/>
    
        <Space
        	android:id="@+id/spaceView_ID"
            android:layout_width="match_parent"
            android:layout_height="20dp"/>
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_blue_light"
            android:gravity="center"
            android:text="Line 2, notice the SPACE above it"/>
    
    </LinearLayout>
    

    In activity_main.xml file, we have used Space widget to show space between two Textview in the application. This will be more clear when you run the application. So, for now, you just follow the steps.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.spaceview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.tutorialwing.spaceview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    		
    		// Access It using id of space view i.e. binding.spaceViewID 
    	}
    }
    

Now, run the application. We will get output as below –

Tutorialwing Android Space Output Android Space Using Kotlin

Tutorialwing Android Space Output

Different Attributes of Space View in XML

Now, we will see how to use different attributes of Android Space View using Kotlin to customise it –

Set Id of Space View

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of Space View using android:id attribute like below –

    <Space
        android:id="@+id/spaceView_ID"
        />

Here, we have set id of Space View as spaceView_ID using android:id=”” attribute. So, if we need to reference this Space View, we need to use this id – spaceView_ID.
Learn to Set ID of Space View Dynamically

Set Width of Space View

We use android:layout_width=”” attribute to set width of Space View.
We can do it as below –

    <Space
        android:id="@+id/spaceView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of Space View Dynamically

Set Height of Space View

We use android:layout_height=”” attribute to set height of Space View.
We can do it as below –

    <Space
        android:id="@+id/spaceView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of Space View Dynamically

Set Padding of Space View

We use android:padding=”” attribute to set padding of Space View.
We can do it as below –

    <Space
        android:id="@+id/spaceView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in Space View using android:padding=”” attribute.
Learn to Set Padding of Space View Dynamically

Set Margin of Space View

We use android:layout_margin=”” attribute to set margin of Space View.
We can do it as below –

    <Space
        android:id="@+id/spaceView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in Space View using android:layout_margin=”” attribute.
Learn to Set Margin of Space View Dynamically

Set Background of Space View

We use android:background=”” attribute to set background of Space View.
We can do it as below –

    <Space
        android:id="@+id/spaceView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in Space View using android:background=”” attribute.
Learn to Set Background of Space View Dynamically

Set Visibility of Space View

We use android:visibility=”” attribute to set visibility of Space View.
We can do it as below –

    <Space
        android:id="@+id/spaceView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of Space View using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of Space View Dynamically

Till now, we have see how to use android Space View using Kotlin. We have also gone through different attributes of Space View to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android Space View Widget

Below are the various attributes that are used to customise android Space View Widget. However, you can check the complete list of attributes of Space View in it’s official documentation site. Here, we are going to list some of the important attributes of this widget –

Some of the popular attributes of android Space widget are

Sr. XML Attributes Description
1 android:background It is used to set background of the view
2 android:longClickable It is used to define whether this view will respond on long click
3 android:padding Specifies padding of the view
4 android:visibility Specifies visibility of the view

We have seen different attributes of Space View and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is Space View, how can we use android Space View using Kotlin ? etc. We also went through different attributes of android Space View.

Leave a Reply