activity_main.xml (Button + ListView Ek Hi Page Pe)
<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical”
android:padding=”20dp”
android:gravity=”center”>
<Button
android:id=”@+id/btnShowList”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Show List” />
<ListView
android:id=”@+id/listView”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:visibility=”gone”/>
</LinearLayout>
MainActivity.kt (Button Click Par ListView Show Karna)
package com.rohit.frame_layout
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnShowList: Button = findViewById(R.id.btnShowList)
val listView: ListView = findViewById(R.id.listView)
// List of items
val names = arrayOf(
“One”, “Two”, “Three”, “Four”, “Five”,
“Six”, “Seven”, “Eight”, “Nine”, “Ten”
)
// Adapter for ListView
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, names)
listView.adapter = adapter
// Button click par ListView dikhaye
btnShowList.setOnClickListener {
listView.visibility = ListView.VISIBLE
}
}
}