19

I have a tab that contains a linear layout with a searchview and a listview. When the user clicks one of the search results, I want to replace that layout with another layout containing the details of the selection.

What happens when I replace the search fragment only the listview portion of the layout gets replaced; the searchview is still visible and active on the screen.

Here is my search layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/search_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SearchView
        android:id="@+id/public_calendar_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:iconifiedByDefault="false"
    </SearchView>

    <ListView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1.0"
        android:choiceMode="singleChoice" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:gravity="center"/>

</LinearLayout>

Here is the detail layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/detail_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

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

        <Space
            android:layout_width="0dip"
            android:layout_height="20dip"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal" >

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".8"
            android:text="@string/label_Name" />

        <Space
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight=".1" />
    </LinearLayout>

    .........

</LinearLayout>

And my code to replace the search fragment:

FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack.
DetailFragment fragment = DetailFragment.newInstance(cal);
transaction.replace(R.id.search_layout, fragment);

transaction.addToBackStack(null);
transaction.commit();

Detail fragment creation:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.detail_layout, container, false);
    return v;
}

static DetailFragment newInstance(String cal) {
    DetailFragment d = new DetailFragment();
    return d;
}

What am I doing wrong? How can I get the entire search layout to be replaced.

Thanks!

sitecrawl
  • 385
  • 1
  • 2
  • 9
  • You are not showing how the DetailFragment sets up its layout – Heiko Rupp Oct 18 '12 at 15:52
  • Heiko, I added that information... – sitecrawl Oct 18 '12 at 16:09
  • 1
    so are you trying to remove your original search tab or are you trying to place a layout over it temporarily to show your search results?? – Marco RS Oct 18 '12 at 18:09
  • The latter. I want to replace the search fragment (which contains both the searchview and the listview) with the details fragment. The user would then return to the search fragment after viewing and/or acting on the details fragment. This is working - almost. When the details fragment is displayed, the searchview from the search fragment is still visible (and functional). In debugging, I added a textview both before and after the searchview to see what happens. In this case, both textviews and the searchview remain visible with the detail fragment is displayed. – sitecrawl Oct 18 '12 at 18:24
  • 1
    Have you considered instead adding the detailsfragment on top of everything and using a different tag for it. For example, DetailFragment fragment = DetailFragment.newInstance(cal); transaction.add(android.R.id.content, fragment,"MyDetailsTaG"); transaction.addToBackStack(null); transaction.commit(); – Marco RS Oct 18 '12 at 22:05
  • I did post an answer for the problem same like this here: https://stackoverflow.com/a/63683045/8874958 – Kishan Solanki Sep 01 '20 at 07:18

5 Answers5

18

I figured out my problem. Marco wasn't exactly right but he pointed me in the right direction.

The problem was the the container ID I was passing to the replace method was the ID of the fragment I was replacing, not the ID of the fragment container. This seems to explain why some of the original fragment controls were remaining after the replace - that entire fragment wasn't being replaced.

I changed it to get the fragment container view ID, and that worked! Here's the code:

transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment); 

I found the answer for getting the container view ID of a fragment here, Get fragment's container view id.

Community
  • 1
  • 1
sitecrawl
  • 385
  • 1
  • 2
  • 9
12

I was using the correct ID, then also it wasnt going away. Turns out that you need to set the Background color to your needed color in new Fragment layout, like this.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="@color/white_color">
lionelmessi
  • 1,116
  • 2
  • 10
  • 17
6

To solve the problem, set the android:background property to “?android:windowBackground” in the fragment layout files.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".MainFragment">

    ...

</LinearLayout>

In Fragment_2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?android:windowBackground"
    tools:context=".SomeFragment">

    ...

</LinearLayout>
Shubham Chugh
  • 117
  • 2
  • 7
1

I was having the same problem, and the solution didn't work for me. Maybe this will help someone...

I had added a fragment inside the activity layout xml file. This caused a lot of issues.

Instead, add a frame layout in your xml file, and then programatically add and replace fragments. This causes the whole fragment to be replaced.

John Mansell
  • 624
  • 5
  • 16
0

I had the same problem once, and this is my solution. Try to do this, use FrameLayout instead of fragment in your layout file. Just like this:

    <fragment android:id="@+id/your_fragment" />

Then, add your default fragment in activity method onCreate().

getFragmentManager().beginTransaction()
.add(R.id.your_fragment, YourFragment)
.commit();

Now, the old fragment can be replaced correctly, it looks great.

This worked for me, and I hope it helps you too.

Man Long
  • 1
  • 1