jobin
jobin

Reputation: 1517

Android-Views in Relativelayout visible even after replacing it with another fragment

The Relativelayout(R.id.relativeLayout1) in activity_main.xml is replaced with the fragment layout hotel_search.But after replacing the relativelayout with fragment the original views of the relativelayout are still visible. Screenshot included.

activity_main.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"
    android:background="@drawable/bg"
    tools:context="com.example.jobinsabu.georgetravels.MainActivity">
<android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/toolbar"
        android:background="@android:color/transparent">

    </android.support.v7.widget.Toolbar>
<RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/relativeLayout1">
<android.support.v4.view.ViewPager android:layout_width="wrap_content"
    android:layout_height="100dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:visibility="invisible"
    android:id="@+id/viewpager">

</android.support.v4.view.ViewPager>

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_marginLeft="20dp"
                android:layout_marginTop="40dp"
                android:id="@+id/homeflight"
                android:background="@drawable/homeflightcircle"
                android:layout_gravity="left|center_vertical"
                android:layout_below="@+id/viewpager"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

            </Button>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="flights"
                android:textColor="#ffffff"
                android:layout_below="@+id/homeflight"
                android:layout_alignRight="@+id/homeflight"
                android:layout_alignEnd="@+id/homeflight"
                android:layout_marginRight="4dp"
                android:layout_marginEnd="4dp"
                android:layout_marginTop="3dp"
                android:id="@+id/textView" />
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/homehotelcircle"
                android:layout_alignTop="@+id/homeflight"
                android:layout_centerHorizontal="true"
                android:id="@+id/homehotel" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="hotels"
                android:textColor="#ffffff"
                android:layout_alignTop="@+id/textView"
                android:layout_centerHorizontal="true"
                android:id="@+id/textView2" />
            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:background="@drawable/homeholidaycircle"
                android:layout_alignTop="@+id/homehotel"
                android:layout_alignRight="@+id/viewpager"
                android:layout_alignEnd="@+id/viewpager"
                android:id="@+id/button" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="holidays"
                android:id="@+id/holidaytext"
                android:textColor="#ffffff"
                android:layout_alignTop="@+id/textView2"
                android:layout_alignLeft="@+id/button"
                android:layout_alignStart="@+id/button" />
 </RelativeLayout>
</RelativeLayout>

hotel_search.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginBottom="60dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        android:background="#60212121">

        <TextView
            android:id="@+id/locationtext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="30dp"
            android:text="Location"
            android:textColor="#BDBDBD"
            android:textSize="15sp" />

        <AutoCompleteTextView
            android:id="@+id/autohotellocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/locationtext"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="20dp"
            android:background="@android:color/transparent"
            android:text="Hotel Name/City/Code"
            android:textColor="#ffffff" />

        <View
            android:id="@+id/view1"
            android:layout_width="100dp"
            android:layout_height="0.5dp"
            android:layout_below="@+id/autohotellocation"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="22dp"
            android:background="#00838F"
            android:foregroundGravity="center"></View>

        <TextView
            android:id="@+id/checkin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/view1"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="10dp"
            android:text="CheckIn"
            android:textColor="#BDBDBD" />

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_alignLeft="@+id/locationtext"
            android:layout_alignStart="@+id/locationtext"
            android:layout_below="@+id/checkin"
            android:layout_marginLeft="12dp"
            android:layout_marginStart="12dp">

            <TextView
                android:id="@+id/checkindate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="18"
                android:textColor="#fff"
                android:textSize="23sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/checkindate"
                android:text="Jun 2015"
                android:textColor="#fff"
                android:textSize="15sp" />
        </RelativeLayout>

        <View
            android:id="@+id/view2"
            android:layout_width="100dp"
            android:layout_height="0.5dp"
            android:layout_below="@+id/relativeLayout"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="22dp"
            android:background="#00838F"
            android:foregroundGravity="center"></View>

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/view2">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/view2"
                android:layout_marginTop="30dp">

                <RelativeLayout
                    android:id="@+id/roomlayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent">


                    <TextView
                        android:id="@+id/rooms"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" />

                    <TextView
                        android:id="@+id/adults"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@+id/rooms"

                        android:layout_toEndOf="@+id/button3"
                        android:layout_toRightOf="@+id/button3"
                        android:text="Adults"
                        android:textColor="#BDBDBD" />

                    <Button
                        android:id="@+id/button3"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_below="@+id/adults"
                        android:layout_marginLeft="18dp"
                        android:layout_marginStart="18dp"
                        android:layout_marginTop="10dp"
                        android:layout_toEndOf="@+id/rooms"
                        android:layout_toRightOf="@+id/rooms"
                        android:background="#90006064"
                        android:text="-"
                        android:textColor="#fff"
                        android:textSize="22sp" />

                    <Button
                        android:id="@+id/button4"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_alignLeft="@+id/adults"
                        android:layout_alignStart="@+id/adults"
                        android:layout_alignTop="@+id/button3"
                        android:layout_marginLeft="37dp"

                        android:layout_marginStart="37dp"
                        android:background="#90006064"
                        android:text="+"
                        android:textColor="#fff"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/children"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_above="@+id/button3"
                        android:layout_alignParentEnd="true"
                        android:layout_alignParentRight="true"
                        android:layout_marginEnd="38dp"
                        android:layout_marginRight="38dp"
                        android:text="Children"
                        android:textColor="#BDBDBD" />

                    <Button
                        android:id="@+id/button6"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:layout_marginEnd="13dp"

                        android:layout_marginRight="13dp"
                        android:background="#90006064"
                        android:text="+"
                        android:textColor="#fff"
                        android:textSize="18sp"
                        android:layout_alignParentBottom="true"
                        android:layout_alignParentRight="true"
                        android:layout_alignParentEnd="true" />

                    <Button
                        android:id="@+id/button5"
                        android:layout_width="30dp"
                        android:layout_height="30dp"
                        android:background="#90006064"
                        android:text="-"
                        android:textColor="#fff"
                        android:textSize="22sp"
                        android:layout_alignParentBottom="true"
                        android:layout_toLeftOf="@+id/button6"
                        android:layout_toStartOf="@+id/button6"
                        android:layout_marginRight="41dp"
                        android:layout_marginEnd="41dp" />
                </RelativeLayout>
            </RelativeLayout>
        </ScrollView>

        <TextView
            android:id="@+id/checkout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/checkin"
            android:layout_marginEnd="21dp"
            android:layout_marginRight="21dp"
            android:text="CheckOut"
            android:textColor="#BDBDBD" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/checkout"
            android:layout_alignStart="@+id/checkout"
            android:layout_alignTop="@+id/relativeLayout"
            android:id="@+id/relativeLayout2">

            <TextView
                android:id="@+id/checkoutdate"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="18"

                android:textColor="#fff"
                android:textSize="23sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/checkoutdate"
                android:text="Jun 2015"
                android:textColor="#fff"
                android:textSize="15sp" />
        </RelativeLayout>

    </RelativeLayout>
</RelativeLayout>

MainActivity.java:

package com.example.jobinsabu.georgetravels;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
        Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=(Button) findViewById(R.id.homehotel);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HotelSearch hotelSearch=new HotelSearch();
                FragmentTransaction transaction=getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.relativeLayout1,hotelSearch);
                transaction.commit();
            }
        });
    }
}

HotelSearch.java:

package com.example.jobinsabu.georgetravels;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Jobin Sabu on 4/5/2016.
 */
public class HotelSearch extends Fragment {
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.hotel_search,null,false);
        return v;
    }
}

enter image description here

Upvotes: 0

Views: 155

Answers (3)

nuuneoi
nuuneoi

Reputation: 1798

Fragment doesn't work that way. FragmentTransaction.replace will remove an attached fragment and add the new one to the layout.

But those ViewPager and Button are not inside a Fragment but it's hardcoded to be laid in Activity's layout so FragmentTransaction cannot remove those views out.

What to do?

Leave that relativeLayout1 empty and group those child views inside as a Fragment. And then you will be able to remove them as you want.

Upvotes: 1

bryan c
bryan c

Reputation: 1366

You must usereplce() when you want replace fragment with another fragment in your case you just added fragment to your activity so define all your first RelativeLayout inside a new Fragment then use a FrameLayout as main container

Upvotes: 0

Prasanna Anbazhagan
Prasanna Anbazhagan

Reputation: 1725

Give background color white to the fragment layout.

replace
android:background="@android:color/transparent"

with
android:background="color/white"

Upvotes: 0

Related Questions