Reputation: 333
I have a cardview and a button in my content_main.xml , if the user press the button , it should show the the fragment (named radio fragment), when i do that cardview is still showing in the radio fragment(fragment name)
i want to hide the cardview and button and show the fragment, when the user press the button
here is my content main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.CardView
android:layout_width="350dp"
android:layout_height="100dp"
android:layout_marginTop="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
app:cardElevation="10dp" >
</android.support.v7.widget.CardView>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
here is the fragment_radio.xml
<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"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.Radio">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/webView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:animationResolution="@integer/abc_max_action_buttons"
android:clickable="false"
android:theme="@style/Base.Theme.AppCompat"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center" />
</RelativeLayout>
this is the button function in main activity
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Radio radio=new Radio();
android.support.v4.app.FragmentManager manager=getSupportFragmentManager();
manager.beginTransaction().replace(R.id.Relative, radio,radio.getTag()).commit();
}
});
}
any one can help me?
Upvotes: 0
Views: 461
Reputation: 3574
The CardView
and Button
is showing because both are elevated above the RelativeLayout
.
So when you set fragment on RelativeLayout
, it is placed under those views.
One solution will be to cover the cardview
and button
inside a layout and hide the layout when showing fragment, and unhide it on the reverse process.
Upvotes: 2
Reputation: 815
Use FrameLayout as a root layout like this:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.Radio">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:animationResolution="@integer/abc_max_action_buttons"
android:clickable="false"
android:theme="@style/Base.Theme.AppCompat" />
</RelativeLayout>
</FrameLayout>
And then replace your fragment using this id:R.id.container
Upvotes: 4
Reputation: 163
As others have pointed out, you do not replace the views inside the container.
The documentation for replace()
says:
Replace an existing fragment that was added to a container. This is essentially the same as calling
remove(Fragment)
for all currently added fragments that were added with the same containerViewId and thenadd(int, Fragment, String)
with the same arguments given here.
I don't think that there is an existing fragment that you are replacing.
You can work around this by either hiding both the CardView
and the Button
or setting a background on your fragment should also work.
Upvotes: 1
Reputation: 23881
try adding a background to your Relativelayout
:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/Relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.hackerinside.jaisonjoseph.radioplanet.MainActivity"
tools:showIn="@layout/activity_main">
if that's not work; try:
manager.beginTransaction().replace(android.R.id.content, radio,radio.getTag()).commit();
android.R.id.content
gives you the root element of a view,
Upvotes: 1