Android Space Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android space view. We are also going to learn about different attributes of android space that can be used to customise it.

Output

Tutorialwing android space widget output Android space widget tutorial with example

Tutorialwing android space widget output

Getting Started

Android Space can be defined as below –

Space view is a lightweight view subclass that are used to create gaps between different views.

Attributes of Android Space Widget

Some of the popular attributes of space views are –

Sr. XML Attributes Description
1 android:background Sets background of the view
2 android:longClickable Defines whether this view will respond on long click
3 android:padding Sets padding of the view
4 android:visibility Sets visibility of the view



Example of Android Space Widget

At first, we will create android application. Then, we will use android space widget in this application.

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as SpaceView. Then, 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. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use space widget in the application.

2. Modify Values folder

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

3. Use Space Widget in xml file

Open res/layout/activity_main.xml file. Then, 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 textViews.

Since androidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml

Code inside src/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 you run the application, you will get output as shown above.

That’s end of tutorial on Android Space Widget.

Leave a Reply