Android Space Using Kotlin With Example




Target

Hello Readers! In this post, we are going to learn about android space using kotlin in any android application. We are also going to learn about different attributes of android space that can be used to customise this widget.

Output

Tutorialwing Android Space Output Android Space Using Kotlin

Tutorialwing Android Space Output

Getting Started

Android Space widget can be defined as below –

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

Different Attributes of Android Space 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



Example of Android Space Using Kotlin

At first, we will create an android application. Then, we will use Space Widget in the application.

1. Creating New Project in Kotlin

Follow steps below to create new project. Please ignore the steps if you have already created the project.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as SpaceView. Then, check Include Kotlin Support and click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. You will get a newly created project successfully if you have followed steps properly.

Since we have a new application, we will modify xml and kotlin file to use Switch widget in the application.

2. Modify values folder

No values folders have been modified. So, we are not going to mention them here.

3. Use Space Widget in xml file

Open src/main/res/layout/activity_main.xml file and add below code into 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: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.

Since AndroidManifest.xml file is very important in the application, we are also to see the content inside this file.

AndroidManifest.xml file

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.spaceview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of our tutorial on Android Space using Kotlin.