inflate()
is called. For example, setting a layout to be inflated within a ViewStub.setVisibility(int)
or inflate()
. The output of this method will be an integer representing the layout resource.getLayoutResource()
and substitutes the current StubbedView with the inflated layout resource in its parent view. This is commonly used to dynamically replace views in Android layouts.Attributes | Description |
---|---|
id | To uniquely identify a ViewStub |
inflated | To set the id of the inflated View. This can be done programmatically using the setInflatedId() method. |
layout | To supply an identifier for the layout resource to inflate when the ViewStub becomes visible or when forced to do so. This can be set programmatically using the setLayoutResource(int) method. |
A sample GIF is provided below to help you understand the concepts discussed in this article.
To start a new project in Android Studio, you can follow the steps outlined in the guide on creating a project in Android Studio. This guide offers instructions in both Java and Kotlin programming languages for Android development.
Example: You can create a new project by selecting "File" > "New" > "New Project" in Android Studio and then following the setup wizard.
In this step, you will place an image in the drawable folder of your app. Different resolutions may require images of varying sizes, which can be organized under different drawable folders within the 'res' directory.
Example: You can add an image by copying it into the 'drawable' folder in your project's resources directory.
Proceed to the 'activity_main.xml' file, which defines the user interface (UI) of your project. Below is a snippet of code from the 'activity_main.xml' file with explanatory comments included.
Example: In the XML file, you can define the layout of your app's main activity using various XML tags such as LinearLayout, Button, and ViewStub.
To create a new layout XML file, navigate to the 'res' > 'layout' directory in your app, right-click, select 'New' > 'XML' > 'Layout XML file', and name the file as 'custom_viewstub'.
Example: You can create a new XML layout file by right-clicking on the 'layout' folder, selecting 'New', then 'XML', and finally 'Layout XML file' in Android Studio.
LinearLayout
with match_parent
width and height attributes, and a vertical orientation.ImageView
with a width of wrap_content
, height of 200dp
, and centered gravity, referencing an image named cryptocurrency
.TextView
with attributes for width, height, gravity, text content (e.g., "CRYPTOCURRENCY"), and text color.Open the MainActivity File and examine the provided code snippet below. The code includes comments to aid in understanding.
import | android.app.Activity | android.os.Bundle | android.view.View | android.view.ViewStub | android.widget.Button |
class MainActivity : Activity() { | private lateinit var simpleViewStub: ViewStub | lateinit var showButton: Button | lateinit var hideButton: Button | ||
override fun onCreate(savedInstanceState: Bundle?) { | super.onCreate(savedInstanceState) | setContentView(R.layout.activity_main) | |||
// get the reference of ViewStub | simpleViewStub = findViewById(R.id.viewStubToLearn) | simpleViewStub.inflate() | |||
// get the reference of show button | showButton = findViewById(R.id.showButtonForViewStub) | ||||
// get the reference of hide button | hideButton = findViewById(R.id.hideButtonForViewStub) | ||||
// perform click event on show button | showButton.setOnClickListener { | simpleViewStub.visibility = View.VISIBLE | } | ||
// perform click event on hide button | hideButton.setOnClickListener { | simpleViewStub.visibility = View.GONE | } |
MainActivity
class extends AppCompatActivity
.ViewStub
and Button
.ViewStub
to dynamically inflate layouts.ViewStub
.onCreate
method is a crucial part of an Android activity lifecycle. It is called when the activity is starting.onCreate
, the layout for the activity is set using setContentView
, which defines the UI of the activity based on the XML layout file.onCreate
method initializes various UI elements and sets up click event listeners for the buttons.ViewStub
becomes visible, displaying the dynamically inflated layout.ViewStub
to View.GONE
, effectively hiding it from the user interface.In summary, the MainActivity
class showcases the utilization of ViewStub
for dynamic layout inflation and demonstrates the implementation of button click events to control the visibility of UI elements. Understanding these concepts is fundamental when developing interactive and responsive Android applications.
When the project is executed, a similar output to the one demonstrated in the video becomes visible. ViewStub in Android serves as a beneficial feature, enabling significant reduction in memory usage. This feature can be activated by inflation.
Video Player00:0002:23Use Up/Down Arrow keys to adjust volume.If you find yourself perplexed in the expansive domain of Backend Development, it's perhaps time for a transition! Enroll in our Java Backend Development - Live Course and set forth on a captivating expedition to efficiently master backend development within the scheduled timeline.
What Our Course Offers: