In this article, we will learn about android Group using Kotlin. We will go through various example that demonstrates how to use different attributes of Group. For example,
In this article, we will get answer to questions like –
- What is Group?
- Why should we consider Group while designing ui for any app?
- What are possibilities using Group while designing ui? etc.
Let’s have a quick demo of things we want to cover in this tutorial –
Output
Getting Started
We can define android Group widget as below –
This is helper widget that is used to control visibility and elevation of referenced views. We can use either id or tag of the views to group them together. It makes developer’s life easy to show or hide group of views easily without having to maintain sets of view prgorammatically.
Now, how do we use Group 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 Group. 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 Group 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 Group in Kotlin
Follow steps below to use Group in newly created project –
- Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">Group</string> <string name="textview_3">TextView 3</string> <string name="textview_2">TextView 2</string> <string name="textview_1">TextView 1</string> <string name="hide_views">Hide Views</string> <string name="show_views">Show Views</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.widget.Group android:id="@+id/group" android:layout_width="wrap_content" android:layout_height="wrap_content" app:constraint_referenced_ids="textView1, textView2, textView3" app:layout_constraintStart_toStartOf="parent" /> <Button android:id="@+id/btnShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_views" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnHide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:text="@string/hide_views" app:layout_constraintLeft_toRightOf="@+id/btnShow" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="@string/textview_1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toBottomOf="@+id/btnShow" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:text="@string/textview_2" app:layout_constraintLeft_toRightOf="@+id/textView1" app:layout_constraintTop_toBottomOf="@+id/btnShow" /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="10dp" android:layout_marginTop="10dp" android:text="@string/textview_3" app:layout_constraintLeft_toRightOf="@+id/textView2" app:layout_constraintTop_toBottomOf="@+id/btnShow" /> </androidx.constraintlayout.widget.ConstraintLayout>
In our application, we will be using three textViews, 2 buttons and Group. We group textViews using their ids and Group. Two buttons will show or hide textViews using group.
- Now, we need to access group and buttons in Kotlin file to show or hide referenced TextViews. So, open Kotlin File, MainActivity.kt and add below code in it –
package com.tutorialwing.group import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.View import com.tutorialwing.group.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) setupGroup() } private fun setupGroup() { binding.btnShow.setOnClickListener { binding.group.visibility = View.VISIBLE } binding.btnHide.setOnClickListener { binding.group.visibility = View.INVISIBLE } } }
Here, we have set click listener to buttons that shows or hides Group. textViews are hidden when group is hidden and shown when group is shown.
Now, run the application. We will get output as below –
Different Attributes of Group in XML
Now, we will see how to use different attributes of Android Group using Kotlin to customise it –
Set Id of Group
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 Group using android:id attribute like below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_ID" />
Here, we have set id of Group as group_ID using android:id=”” attribute. So, if we need to reference this Group, we need to use this id – group_ID.
Set Width of Group
We use android:layout_width=”” attribute to set width of Group.
We can do it as below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_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 Group
We use android:layout_height=”” attribute to set height of Group.
We can do it as below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_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 Group
We use android:padding=”” attribute to set padding of Group.
We can do it as below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" />
Here, we have set padding of 10dp in Group using android:padding=”” attribute.
Set Margin of Group
We use android:layout_margin=”” attribute to set margin of Group.
We can do it as below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" />
Here, we have set margin of 10dp in Group using android:layout_margin=”” attribute.
Set Background of Group
We use android:background=”” attribute to set background of Group.
We can do it as below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" />
Here, we have set background of color #ff0000 in Group using android:background=”” attribute.
Set Visibility of Group
We use android:visibility=”” attribute to set visibility of Group.
We can do it as below –
<androidx.constraintlayout.widget.Group android:id="@+id/group_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
Here, we have set visibility of Group using android:visibility=”” attribute. Visibility can be of three types – gone, visible and invisible
We have seen different attributes of Group and how to use it. If you wish to visit post to learn more about it
Thus, we have seen what is Group, how can we use android Group using Kotlin ? etc. We also went through different attributes of android Group.
You must be logged in to post a comment.