Android MockView Using Kotlin With Example

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

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

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

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

Output

Tutorialwing Kotlin Android MockView Using Kotlin With Example

Getting Started

We can define android MockView widget as below –

MockView is a widget that is used to create prototype of a design we want to create for our application. In other words, Suppose we don’t to create original design for our application, instead we just wish to create a mockup design and try different color combinations for original design. In that case, we can use mockView and check different color combinations.

Now, how do we use MockView 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 MockView. 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 MockView 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 MockView in Kotlin

Follow steps below to use MockView in newly created project –

  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">MockView</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.utils.widget.MockView
            android:id="@+id/mockView"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="@color/purple_500"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <androidx.constraintlayout.utils.widget.MockView
            android:id="@+id/mockView2"
            android:layout_width="wrap_content"
            android:layout_height="60dp"
            android:background="@color/teal_200"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    
  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.mockview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.tutorialwing.mockview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    
    	}
    }
    

Now, run the application. We will get output as below –
Tutorialwing Kotlin Android MockView Using Kotlin With Example

Different Attributes of MockView in XML

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

Set Id of MockView

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 MockView using android:id attribute like below –

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        />

Here, we have set id of MockView as mockView_ID using android:id=”” attribute. So, if we need to reference this MockView, we need to use this id – mockView_ID.

Set Width of MockView

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

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).

Set Height of MockView

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

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.

Set Padding of MockView

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

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in MockView using android:padding=”” attribute.

Set Margin of MockView

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

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in MockView using android:layout_margin=”” attribute.

Set Background of MockView

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

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in MockView using android:background=”” attribute.

Set Visibility of MockView

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

    <androidx.constraintlayout.utils.widget.MockView
        android:id="@+id/mockView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of MockView using android:visibility=”” attribute. Visibility can be of three types – gone, visible and invisible

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

Leave a Reply