Reputation: 43
Many examples with ScrollView, but I don't have any idea with RelativeLayout
In RelativeLayout, some views will cover another view. I can check it in xml, but how can check it with java code? Use isShown(), willNotDraw(), hasWindowFocus(), getLocalVisibleRect(), those are not working.
Case1: Container layout is really on screen
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--container layout is really on screen-->
<fragment
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Case2: Container layout is not really on screen
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
/>
</LinearLayout>
</LinearLayout>
<!--container layout is not really on screen-->
<fragment
android:id="@+id/map_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Upvotes: 1
Views: 1916
Reputation: 1091
Change in Case2: Container layout is not really on screen
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/icon"
android:layout_width="50dp"
android:layout_height="50dp" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
</LinearLayout>
<!--container layout is not really on screen-->
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- fragment -->
</FrameLayout>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private FrameLayout frameLayout;
private LinearLayout container;
private RelativeLayout parent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
container = (LinearLayout) findViewById(R.id.container);
parent = (RelativeLayout) findViewById(R.id.parent);
//container.bringToFront();
Log.e(TAG, "container: " + parent.indexOfChild(container));
Log.e(TAG, "frameLayout: " + parent.indexOfChild(frameLayout));
if (parent.indexOfChild(container) == parent.getChildCount() - 1) {
Log.e(TAG, "container is visible");
} else {
Log.e(TAG, "container is not visible");
}
}
}
If container index is equal to child count in parent then it is front of other view and is visible. otherwise container is overlapped behind other layout.
I hope it is helpful to you.
Upvotes: 0
Reputation: 1965
try using this
if (myView.isShown()) {
// Its visible
} else {
// Either gone or invisible
}
Upvotes: 1