In this article, we will learn about android StackView using Kotlin. We will go through various example that demonstrates how to use different attributes of StackView. For example,
In this article, we will get answer to questions like –
- What is StackView?
- Why should we consider StackView while designing ui for any app?
- What are possibilities using StackView 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 StackView widget as below –
StackView is a widget that are used to create views like stack cards in the application. You can flip current views to create space for views just after it. Whenever current view is flipped, next view, just after it, comes forward and takes place of current view.
Now, how do we use StackView 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 StackView. 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 StackView 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 StackView in Kotlin
Follow steps below to use StackView in newly created project –
- Open res/values/strings.xml file. Then, add below code into it.
<resources> <string name="app_name">StackView</string> <string name="no_image">No image</string> </resources>
- We will need some drawable images for this application. So, we can click here to download images to be used in this application.
-
Create View for single item
We will create view for single item in stackView. Create a new file, item.xml, in res/layout folder. Then, add below code into it.
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/black"> <ImageView android:id="@+id/imageView" android:layout_width="260dp" android:layout_height="260dp" android:layout_margin="3dp" android:contentDescription="@string/no_image"/> </FrameLayout>
-
Create Adapter for StackView
We will create adapter for stackView. This will be responsible to create different views using stackView. So, create a new file, StackAdapter.kt, in main/java/com.tutorialwing.stackview package. Then, add below code into it.
package com.tutorialwing.stackview import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import android.widget.ImageView import com.tutorialwing.stackview.databinding.ItemBinding class StackAdapter internal constructor(context: Context, private val nameList: IntArray) : BaseAdapter() { private val inflater: LayoutInflater = LayoutInflater.from(context) override fun getCount(): Int { return nameList.size } override fun getItem(position: Int): Any { return nameList[position] } override fun getItemId(position: Int): Long { return position.toLong() } override fun getView(position: Int, view: View?, parent: ViewGroup): View { var convertView = view val holder: ViewHolder if (convertView == null) { val binding = ItemBinding.inflate(inflater) convertView = binding.root holder = ViewHolder() holder.imageView = binding.imageView convertView.tag = holder } else { holder = convertView.tag as ViewHolder } holder.imageView!!.setBackgroundResource(nameList[position]) return convertView } inner class ViewHolder { internal var imageView: ImageView? = null } }
In Adapter class, we have mentioned below things –
- We have defined a constructor, StackAdapter(…, …) method, that accepts context and nameList array. nameList contains the list of image to be used to create views.
- getCount() method returns the size of rows in stackView. i.e. total number of views to be created in the stackView.
- getItem() method returns the item at given index(i.e. position).
- getItemId() method returns the id of the view at given index(i.e. position).
- getView() method defines the view at given index. Note that we have used viewHolder pattern to create a view in stackView.
- 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:background="@android:color/darker_gray" android:padding="20dp" tools:context=".MainActivity"> <StackView android:id="@+id/stackView" android:layout_width="match_parent" android:layout_height="match_parent" android:animateLayoutChanges="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
In activity_main.xml file, we have defined stackView. Now, we will access this widget in java file to perform some operations in it.
-
We can also access it in Kotlin File, MainActivity.kt, as below –
package com.tutorialwing.stackview import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import com.tutorialwing.stackview.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private lateinit var binding: ActivityMainBinding private var nameList = intArrayOf( R.drawable.guava, R.drawable.jackfruit, R.drawable.mix_fruit, R.drawable.pizza, R.drawable.pomegranate, R.drawable.strawberry, R.drawable.zespri_kiwi ) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityMainBinding.inflate(layoutInflater) val view = binding.root setContentView(view) setupStackView() } private fun setupStackView() { val adapter = StackAdapter(this, nameList) binding.stackView.adapter = adapter adapter.notifyDataSetChanged() } }
In MainActivity.kt file, we have accessed stackView widget. Then, we have created an adapter and assigned it to stackView.
Now, run the application. We will get output as below –
Different Attributes of StackView in XML
Now, we will see how to use different attributes of Android StackView using Kotlin to customise it –
Set Id of StackView
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 StackView using android:id attribute like below –
<StackView android:id="@+id/stackView_ID" />
Here, we have set id of StackView as stackView_ID using android:id=”” attribute. So, if we need to reference this StackView, we need to use this id – stackView_ID.
Learn to Set ID of StackView Dynamically
Set Width of StackView
We use android:layout_width=”” attribute to set width of StackView.
We can do it as below –
<StackView android:id="@+id/stackView_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 StackView Dynamically
Set Height of StackView
We use android:layout_height=”” attribute to set height of StackView.
We can do it as below –
<StackView android:id="@+id/stackView_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 StackView Dynamically
Set Padding of StackView
We use android:padding=”” attribute to set padding of StackView.
We can do it as below –
<StackView android:id="@+id/stackView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" />
Here, we have set padding of 10dp in StackView using android:padding=”” attribute.
Learn to Set Padding of StackView Dynamically
Set Margin of StackView
We use android:layout_margin=”” attribute to set margin of StackView.
We can do it as below –
<StackView android:id="@+id/stackView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" />
Here, we have set margin of 10dp in StackView using android:layout_margin=”” attribute.
Learn to Set Margin of StackView Dynamically
Set Background of StackView
We use android:background=”” attribute to set background of StackView.
We can do it as below –
<StackView android:id="@+id/stackView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff0000" />
Here, we have set background of color #ff0000 in StackView using android:background=”” attribute.
Learn to Set Background of StackView Dynamically
Set Visibility of StackView
We use android:visibility=”” attribute to set visibility of StackView.
We can do it as below –
<StackView android:id="@+id/stackView_ID" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone" />
Here, we have set visibility of StackView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of StackView Dynamically
Till now, we have see how to use android StackView using Kotlin. We have also gone through different attributes of StackView to perform certain task. Let’s have a look at list of such attributes and it’s related task.
Different Attributes of Android StackView Widget
Below are the various attributes that are used to customise android StackView Widget. However, you can check the complete list of attributes of StackView 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 stackView inherited from AdapterViewAnimator are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animateFirstView | Specifies whether to animate the current View when the ViewAnimation is first displayed |
2 | android:inAnimation | Identifier for the animation to use when a view is shown |
3 | android:loopViews | Specifies whether the animator loops to the first view once it has reached the end of the list |
4 | android:outAnimation | Identifier for the animation to use when a view is hidden |
Some of the popular attributes of android stackView inherited from ViewGroup are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:animateLayoutChanges | Defines whether to run layout transition when there is any change in layout |
2 | android:animationCache | Defines whether to create drawing cache for children by layout animation |
3 | android:clipToPadding | Specifies whether the ViewGroup will clip its children and resize (but not clip) any EdgeEffect to its padding, if padding is not zero |
4 | android:layoutAnimation | Defines the layout animation to be used when the viewGroup is laid out for the first time |
5 | android:layoutMode | Specifies the layout mode of the viewGroup |
Some of the popular attributes of android stackView inherited from View are –
Sr. | XML Attributes | Description |
---|---|---|
1 | android:alpha | Specifies alpha to the view |
2 | android:background | Specifies drawable to the background |
3 | android:backgroundTint | Specifies tint to apply to the background |
4 | android:clickable | Specifies whether the view is clickable or not |
5 | android:elevation | Specifies elevation of the view |
6 | android:focusable | Specifies whether this view can take focus or not |
7 | android:id | Specifies id of the view |
8 | android:visibility | Specifies the visibility(VISIBLE, INVISIBLE, GONE) of the view |
We have seen different attributes of StackView and how to use it. If you wish to visit post to learn more about it
Thus, we have seen what is StackView, how can we use android StackView using Kotlin ? etc. We also went through different attributes of android StackView.
You must be logged in to post a comment.