0

I have a view. The view might become visible at some time in the future. When this view is visible I want to call a method. How to do this?

val editText = findViewById<EditText>(R.id.editText)
 //  editText might become invisible in some time in future
 // and in some in future it might visible 
 if(editText.isVisible(){
   // code to be executed
 }

Code for View.isVisible() :

 fun View.isVisible() = this.visibility == View.VISIBLE  // check if view is visible

Is there anything like View.setOnClickListener which could be applied and triggered when the view is visible-

 editText.setOnClickListener { view ->

 }
Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22
mad_lad
  • 654
  • 3
  • 8
  • 20

2 Answers2

-1

click listener is callback when the view is being clicked. it has no concern with its visibility. There is no method like isVisible(). to check Visibility

if(yourView.getVisiblity()==View.VISIBLE){ //your task}

for kotlin:

if(youView.visibility==View.VISIBLE){//your task}
-1

I might initialize a variable int status of visibility and set it to 0 with the view invisible. Now I would create a function instead directly setting the visibility of the view. For example a function named onVisibilityChanged();

In that function add the set visibility code followed by setting the int to 0 or 1 as per the visibility an if-else block. If you just set the view to visible, set the int to 1.

The reason for adding if-else block is to configure your actions based on the visibility status. So that gives you the freedom to do whatever you want bases on the visibility.

Make sure you add this code in such a way that it is executed anytime you want. Use the performClick() function to click any button or any other view.

I hope you understand. Or comment any query. I would have posted the code for the same but it looks like you are using Kotlin. So I'll try to post it in Kotlin if possible.

The main intention of doing such a thing is when the value of int changes, the app knows what to do and also knows that visibility has changed.

Just call the function wherever you want. It will be easy.

So this is what I am trying to do:

int visibilityStatus;

textview = findViewById(R.id.textview);
getInitialVisibility();

funcOnVisibilityChange();
}

private void getCurrentVisibility() {
    if (textview.getVisibility() == View.VISIBLE) {
        visibilityStatus = 1;
    } else {
        visibilityStatus = 0;
    }
}

private void funcOnVisibilityChange() {
        //Now change the visibility in thia function
        textview.setVisibility(View.VISIBLE);

        int currentVisibilityStatus;
        if (textview.getVisibility() == View.VISIBLE) {
            currentVisibilityStatus = 1;
        } else {
            currentVisibilityStatus = 0;
        }

        if (visibilityStatus != currentVisibilityStatus) {
            //Visibility status has changed. Do your task
            else {
                //visibility status not changed
            }
        }
}

So all that we are doing is getting visibility of the view when the app is started and when you somehow change its visibility. Here in the example, I've directly changed the visibility. So wherever you know that visibility is going to change just put the funcOnVisibilityChange() and it will do your job... hope it helps. Let me know if you need more clarification.

Dharmaraj
  • 47,845
  • 8
  • 52
  • 84