Lesson 20 – Layout (FrameLayout) Example in Kotlin

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

The FrameLayout type control has all the visual controls aligned to the upper left vertex, centered, right lower vertex, and so on inside the container. (it has nine possible positions).
If we have two or more controls, they are stacked.

For example, if we have an ImageView and a Button inside the FrameLayout then the button is superimposed on the image.

One possible activity of the FrameLayout control is to arrange a series of visual controls that are not visible and go alternating which one becomes visible.

Problem:

Arrange an ImageView and a Button within a FrameLayout layout. When the application starts displaying only the button and when pressed hide the button and make visible the image displayed by the ImageView.

Create a project in Android Studio and define it as name: Project022

We have a FrameLayout:

Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin -

Then inside it we add an ImageView and select an image that already has the Android system:

Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin -

We also have a button inside the FrameLayout and start the ID and Text properties:

Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin -

We select the ImageView control and set the visibility property to the invisible value (this causes the image not to be displayed), to take into account that to access this property of the ImageView class we must change the property view to “View all properties “:

Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin - Lesson 20 - Layout (FrameLayout) Example in Kotlin -

The source code for the class is:

package com.coding180.project022


import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.view.View

class MainActivity: AppCompatActivity () {
// coding180.com
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
        var image = findViewById (R.id.imageView) as ImageView
        var button = findViewById (R.id.button) as Button
        button.setOnClickListener {
            button.visibility = View.INVISIBLE
            image.visibility = View.VISIBLE
        }
    }
}

….

About The Author

Related posts

Leave a Reply