ToggleButton



<RelativeLayout
    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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        android:src="@drawable/ic_launcher_background" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/imageView"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        android:text="TextView" />

    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        android:text="ToggleButton" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="50dp"
        app:srcCompat="@drawable/img" />

</RelativeLayout>


---------------------------------------------------------------------------------------------------------------------



package com.example.test1

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

class MainActivity : AppCompatActivity() {
    lateinit var image1: ImageView
    lateinit var image2: ImageView
    lateinit var result: TextView
    lateinit var toggleButton: ToggleButton
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        toggleButton=findViewById(R.id.toggleButton)
        image1=findViewById(R.id.imageView)
        image2=findViewById(R.id.imageView2)
        result=findViewById(R.id.textView)

        toggleButton.setOnCheckedChangeListener { compoundButton,isChecked->
            if(isChecked){
                image1.visibility = View.VISIBLE
                image2.visibility = View.GONE
                result.text="Image1 is visible"
            }
            else{
                image1.visibility = View.GONE
                image2.visibility = View.VISIBLE
                result.text="Image2 is visible"
            }
        }
    }
}