Huseyn Huseyn - 4 months ago 17
Android Question

How to hide activity view inside fragment

I am newbie in android development and trying to develop simple news application. But I've got a problem when using fragments.

This my activity xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/app_container_view"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/drawer_view"
app:headerLayout="@layout/nav_header">

</android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>


app_container_ view xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">

<include
layout="@layout/toolbar" />

<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabTextColor="@android:color/white"
app:tabSelectedTextColor="@android:color/white"
app:tabIndicatorHeight="6dp"
app:tabMaxWidth="0dp"
app:tabMode="scrollable"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id = "@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</android.support.design.widget.CoordinatorLayout>


toolbar xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways">

<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EastNews"
style="@style/Toolbar.TitleText"
android:layout_gravity="center"
/>

</android.support.v7.widget.Toolbar>


When i replace fragments with transaction it only changes toolbar in fragment and rest inherits from activity view. I don't want to activity tabs and other views become visible on fragment UI.

fragment xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
layout="@layout/toolbar" />

<!-- <android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerIraq"
android:layout_margin="16dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> -->




I've replaced fragment in this way

boolean fragmentPopped = fragmentManager.popBackStackImmediate(fragment.getClass().getName(),0);

if(!fragmentPopped && fragmentManager.findFragmentByTag(fragment.getClass().getName()) == null) {
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_frame, fragment, fragment.getClass().getName());
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.addToBackStack(fragment.getClass().getName());
transaction.commit();
}


Thanks to Rahul problem has been solved. Before posting question i've tried to solve this by adding setVisibility(View.GONE) to attach() and setVisibility(View.VISIBLE) to detach() method. It worked too but activity view loaded slowly.

Answer Source

in your parent RelativeLayout of fragment.xml add below 2 lines as:

android:background="@color/white"
android:clickable="true"

background color is whatever your fragment background color. This way you will be not able to see activity views when you have fragment opened. Hope it will help you out.