Reputation:
I am new in Kotlin. I have a view that I need to show or hide in conditional ways.
How can I do this in Kotlin?
In Java:
public void showHide(View view){
if (view.getVisibility() == View.VISIBLE) {
view.setVisibility(View.INVISIBLE);
} else {
view.setVisibility(View.VISIBLE);
}
}
Upvotes: 64
Views: 127350
Reputation: 441
You can do this very easily with just one line of code.
idTextview.isVisible = true
idTextview.isVisible = false
Upvotes: 19
Reputation: 3322
You can do it in 2 different ways.
I.way
fun showHide(view: View) {
if (view.visibility == View.VISIBLE) view.visibility = View.INVISIBLE else view.visibility = View.VISIBLE
}
II.way
fun showHide(view: View) {
view.isVisible = view.visibility != View.VISIBLE
}
Upvotes: 1
Reputation: 321
Upvotes: 2
Reputation: 3711
If the View is visible initially, one can use the xor operator to toggle the visibility.
view.visibility = view.visibility.xor(View.GONE)
The correct - and more readable - way however is to use the inline var View.isVisible
:
view.isVisible = !isVisible
inline var View.isVisible: Boolean
get() = visibility == View.VISIBLE
set(value) {
visibility = if (value) View.VISIBLE else View.GONE
}
Android developers have added an extension androidx.core.view.ViewKt#isVisible to toggle visibility between View.VISIBLE and View.GONE. So use that rather.
Upvotes: 5
Reputation: 796
You can get the visibility state of a view with the command .isVisible
I´ll show you how.
val menu: ConstraintLayout = findViewById(R.id.layMenu)
if (menu.isVisible==false){
//view is not visible
} else {
//view is visible
}
Upvotes: 1
Reputation: 741
if you want to visible icon
ic_back.visibility = View.VISIBLE
and if you want to visibility GONE so please try it :
ic_back.visibility = View.GONE
Upvotes: 43
Reputation: 11926
A simple way in Kotlin:
fun toggleView(view: View) {
view.isVisible = !view.isVisible
}
Upvotes: 6
Reputation: 1838
You can use from bellow code:
fun View.isVisible(): Boolean {
return visibility == View.VISIBLE
}
And:
fun View.setVisible(visible: Boolean) {
visibility = if (visible) {
View.VISIBLE
} else {
View.GONE
}
}
And you can use:
if (text_view.isVisible()) {
text_view.setVisible(false)
}
Upvotes: 5
Reputation: 7669
In response to this answer, I believe a Kotlin-styled way to accomplish this can also be written as:
fun showHide(view:View) {
view.visibility = if (view.visibility == View.VISIBLE){
View.INVISIBLE
} else{
View.VISIBLE
}
}
Upvotes: 67
Reputation: 1754
This is how I handle view's visibility in Kotlin. These methods can be called on any subclass of View
class. E.g. LinearLayout
, TextView
etc.
VISIBLE / GONE:
// @BindingAdapter("visibleOrGone")
fun View.visibleOrGone(visible: Boolean) {
visibility = if(visible) View.VISIBLE else View.GONE
}
VISIBLE / INVISIBLE:
// @BindingAdapter("visibleOrInvisible")
fun View.visibleOrInvisible(visible: Boolean) {
visibility = if(visible) View.VISIBLE else View.INVISIBLE
}
Databinding:
Uncomment @BindingAdapter
if you also want to use above methods with databinding.
<FrameLayout
app:visibleOrGone="@{viewModel.visibleView}"
...
/>
or
<EditText
app:visibleOrInvisible="@{viewModel.visibleView}"
...
/>
My ViewModel
class looks like this:
class LoginViewModel {
val visibleView = ObservableBoolean()
}
Upvotes: 3
Reputation: 1950
You can convert using Android Studio: Click on the Java file you want to convert, choose Code -> Convert Java File To Kotlin File and see the magic. The result is:
fun showHide(view: View) {
if (view.visibility == View.VISIBLE) {
view.visibility = View.INVISIBLE
} else {
view.visibility = View.VISIBLE
}
}
Upvotes: 6
Reputation: 89568
You could do this in an extension function:
fun View.toggleVisibility() {
if (visibility == View.VISIBLE) {
visibility = View.INVISIBLE
} else {
visibility = View.VISIBLE
}
}
Can be used like this:
someView.toggleVisibility()
Upvotes: 10