wizyashas wizyashas - 1 month ago 8
Android Question

How to check if TextView is within the Visible Screen Android

I wanted to know if my textview is within the visible screen region or not..

I have looked at links as:
Android - Get the visible area of a view?
Android: how to check if a View inside of ScrollView is visible?

But nothing seems to work for me.

Code that i used to check is

Rect rect = new Rect();
//textview intialized in onCreate as text1
text1.getHitRect(rect);
text1.measure(0, 0);
//layout is the parent layout (linear) in which i am adding the text view
Rect acctualView = new Rect(Math.round(layout.getX()), Math.round(layout.getY()),
Math.round(layout.getX() + layout.getWidth()), Math.round(layout.getY() +layout.getHeight()));

if(Rect.intersects(acctualView, rect))
{
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Not visible ", Toast.LENGTH_LONG).show();
}


Every width and height of textview and layout is giving me 0 "zero",

Please let me know how to fix this and know whether a view is inside the visible region or not

Here is my complete activity code

public class MainActivity extends Activity {

private TextView text1, text2, text3, text4, text5, text6;
private Context context;
private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getLayoutInflater().getContext();
layout = (LinearLayout) findViewById(R.id.parentLayout);
setTextViews();
checkViewAndUpdate();

}

private void checkViewAndUpdate() {

Rect rect = new Rect();
layout.getHitRect(rect);
if (text1.getLocalVisibleRect(rect)) {
Toast.makeText(context, "visible", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "Not visible", Toast.LENGTH_LONG).show();
}

}

private void setTextViews() {

text1 = new TextView(context);
text2 = new TextView(context);
text3 = new TextView(context);
text4 = new TextView(context);
text5 = new TextView(context);
text6 = new TextView(context);

text1.setText("text1");
text2.setText("text2");
text3.setText("text3");
text4.setText("text4");
text5.setText("text5");
text6.setText("text6");

text1.setTextSize(30);
text2.setTextSize(30);
text3.setTextSize(30);
text4.setTextSize(30);
text5.setTextSize(30);
text6.setTextSize(30);

//layout.removeAllViews();

layout.addView(text1);
layout.addView(text2);
layout.addView(text3);
layout.addView(text4);
layout.addView(text5);
layout.addView(text6);

}


}

and my layout only contains LinearLayout (empty)

Answer

as i wrote on comment, checking visible rect at onCreate won't work.
Try this code at end of onCreate (instead of call checkAndUpdate() directly).

 layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                checkViewAndUpdate();
            }
        });

View layout does not finish though if you added view at onCreate. yes, this time user cannot see the actual activity and view's actual size are not set.
onGlobalLayout will call when view layout is ended (and activity is visible).
at that timing, we can now get size and position of them!

FYI, you can remove this listener by calling layout.getViewTreeObserver().removeOnGlobalLayoutListener(this) in onGlobalLayout()

Comments