Think I have 4 button on navbar. First button contains some data or items and other buttons also contains some data or items.
Note: All four buttons under a same xml file.
I want if I click first button it will show me first button data or items and same time it will hide others three buttons data or items. And I want to use same method for all buttons.
How can I do that please help me.
You can achieve that by make all other button visibility GONE in onClick fun like that:
if you are in Activity
override fun onCreate(savedInstanceState: Bundle?) {
val b1 = findViewById(R.id.button1)
val b2 = findViewById(R.id.button2)
val b3 = findViewById(R.id.button3)
val b4 = findViewById(R.id.button4)
b1.setOnClickListener {
b1.visibility = View.VISIBLE
b2.visibility = View.GONE
b3.visibility = View.GONE
b4.visibility = View.GONE
}
b2.setOnClickListener {
b1.visibility = View.GONE
b2.visibility = View.VISIBLE
b3.visibility = View.GONE
b4.visibility = View.GONE
}
b3.setOnClickListener {
b1.visibility = View.GONE
b2.visibility = View.GONE
b3.visibility = View.VISIBLE
b4.visibility = View.GONE
}
b4.setOnClickListener {
b1.visibility = View.GONE
b2.visibility = View.GONE
b3.visibility = View.GONE
b4.visibility = View.VISIBLE
}
}
If you are in Fragment
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val b1 = view.findViewById(R.id.button1)
val b2 = view.findViewById(R.id.button2)
val b3 = view.findViewById(R.id.button3)
val b4 = view.findViewById(R.id.button4)
b1.setOnClickListener {
b1.visibility = View.VISIBLE
b2.visibility = View.GONE
b3.visibility = View.GONE
b4.visibility = View.GONE
}
b2.setOnClickListener {
b1.visibility = View.GONE
b2.visibility = View.VISIBLE
b3.visibility = View.GONE
b4.visibility = View.GONE
}
b3.setOnClickListener {
b1.visibility = View.GONE
b2.visibility = View.GONE
b3.visibility = View.VISIBLE
b4.visibility = View.GONE
}
b4.setOnClickListener {
b1.visibility = View.GONE
b2.visibility = View.GONE
b3.visibility = View.GONE
b4.visibility = View.VISIBLE
}
}
In this example, when you press on a button he will be visible and all other will be gone from the UI.
You can you INVISIBLE instead of GONE, the different is that in GONE the view doesn't "catchin" space in your screen, and INVISIBLE it does, just not visible to user (for more information read this)