I have an activity with the associated activity.xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pattern_background"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:clickable="false" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
...
<LinearLayout
android:id="#+id/llComments"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
<include
android:id="#+id/layoutComments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/widget_comentarios_fragment" >
</include>
In the LinearLayout with id llComments I introduce a fragment called CommentsFragment dynamically from the activity with the following code:
private Fragment commentsFragment;
private FragmentTransaction ft;
...
llComentarios = (LinearLayout) findViewById(R.id.llComments);
...
Bundle args = new Bundle();
args.putString(Constants.IDMODEL, getId());
commentsFragment = CommentsFragment.newInstance(args);
ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.llComments, commentsFragment).commit();
The Fragment associated xml file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="#+id/comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#color/transparent"
android:dividerHeight="10dp"
android:paddingLeft="#dimen/indicator_internal_padding"
android:paddingRight="#dimen/indicator_internal_padding" />
</LinearLayout>
The problem I cannot solve is to show in the activity more than one item (in fact I am showing one and a third items) in the list if I do not fix a height for the listview.It should adapt to as many items I add to the list in the fragment through the adapter. I have tried different combinations of heights in the layouts and views, but still does not work. Maybe I am annulating one with other.
Any help would be appreciated.
Thanks in advance
After some days I find out the exact problem. I was including a ListView into a ScrollView, and that's clearly not recomended by Android developers.
You can look for further info here: Why ListView cannot be used in a ScrollView?
Anyway, if you want to do it, you can do that with a hack. Take a look at these proposals:
How can I put a ListView into a ScrollView without it collapsing?
For me that worked. Problem solved.
Related
I've using the Fragment inside the Navigation Drawer.
In Drawer there are 7 more option menus are presents and all other working fine.
Only this fragment tasking 3sec time to show the views.
I've find using debug mode, below this only line taking that much time to execute.
final View view=inflater.inflate(R.layout.fragment_vehicle_inspection,container,false);
Moreover I've using 4 include layout in XML part.
<?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">
<LinearLayout
android:id="#+id/lat_inspection_vendor_name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_05dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_inspection_vendor_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableEnd="#drawable/ic_action_edit"
android:gravity="center"
android:maxLines="1"
android:textColor="#color/colorAccent"
android:textSize="#dimen/margin_15sp"
android:textStyle="bold" />
</LinearLayout>
<include
android:id="#+id/include_inspection_function_layout"
layout="#layout/fragment_inspection_function_layout_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lat_inspection_vendor_name_layout"
android:layout_marginTop="#dimen/margin_05dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/scroll_inspection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include_inspection_function_layout"
android:layout_marginTop="#dimen/margin_10dp"
android:focusableInTouchMode="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/fragment_inspection_vendor_details_card" />
<LinearLayout
android:id="#+id/lat_inspection_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/fragment_inspection_vehicle_form_row_card" />
<include layout="#layout/fragment_inspection_vehicle_features_row_card" />
<include layout="#layout/fragment_inspection_vehicle_form_penalty_row_card" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I hope include part is not affect inflate view.
I've using 26- EditText, 26-checkbox and 30-TextViews inside the include layout.
My concern is Inflating too much number of views only taking time.
Please assist me to encounter this problem.
Thanks in advance.
It's because too much view's in your layout file and yes include part affect your inflation process. Try to reduce number of view.
Share you layout design image so i can give you a suggestion.
It's because you are showing some high resolution image or any other background resource so it may happen to take some time to draw view in your fragment
I have been trying to add Button's below a GraphView, and all these elements are part of a Fragment. Tried many approaches but none of them worked properly.
This is the layout file for the Fragment (fragment_graph.xml).
<FrameLayout 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.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical"
/>
</FrameLayout>
And this is the Java code dynamically adding a graph and button, placed in the Fragment's onViewCreated (View view, Bundle savedInstanceState).
storageGraph = new LineGraphView(getActivity(), graphLabel);
storageGraph.addSeries(graphSeries); // More config calls follow
...
LinearLayout view = (LinearLayout)getView().findViewById(R.id.graph_fragment_layout);
Button button = new Button(getActivity());
button.setText("Test");
view.addView(storageGraph);
view.addView(button);
The Button is not visible though I have set orientation to vertical for the LinearLayout containing it.
EDIT - solved!
I found that nesting the graph under its own LinearLayout and the buttons under another LinearLayout, and both of these wrapped in a LinearLayout fixed the problem! The LinearLayout containing the graph must be weighted (I chose a weight of 0.8).
Layout file looks like:
<FrameLayout 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.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_wrap"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/graph_buttons"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_navigation_previous_item"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_navigation_next_item"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I've just tried it and it works. Perhaps your graph is taking all the available space, so added button is below the screen? Try to wrap your LinearLayout into a ScrollView and see if there is a button in the bottom.
I'm running into some problems trying to figure out how to make fragments that I have programmatically added into a LinearLayout clickable. I'm using fragments because they will be in multiple activities and it was a good way for me to create a layout like this:
http://i.stack.imgur.com/P4lOG.png
But, if there's a better way to do this that would make the process of making it clickable, I'm certainly open to changing things up.
Anyway, I'm adding the fragments to the LinearLayout, jobsList, like so:
FragmentManager fragmentManager = getFragmentManager();
int count = 2;
for (int i = 0; i < count; i++) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("jobID", i + 1);
jobFragment job = new jobFragment();
job.setArguments(bundle);
fragmentTransaction.add(R.id.jobsList, job, Integer.toString(i));
fragmentTransaction.commit();
}
The count of 2 is just a placeholder for now, later there will be an arbitrary number of jobs.
Here is the layout, it's a bit of a mess but I got all the specific weights the way I wanted this way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/basicPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="Port" />
<TextView
android:id="#+id/basicLoadOrEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="Load/Empty" />
<TextView
android:id="#+id/basicInOrOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="In/Out" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicContainerNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Container #" />
<TextView
android:id="#+id/basicChassisNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Chassis #" />
<TextView
android:id="#+id/basicContainerType"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#drawable/background"
android:text="Cont. Type" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicDirectionArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:background="#drawable/background"
android:text="Direction" />
<TextView
android:id="#+id/basicCustomer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".8"
android:background="#drawable/background"
android:text="Customer Location" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicSteamshipLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Steamship Line" />
<TextView
android:id="#+id/basicBKG_BOL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:background="#drawable/background"
android:text="BKG-BOL#" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/basicStatus1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/background"
android:text="Status 1" />
<TextView
android:id="#+id/basicStatus2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/background"
android:text="Status 2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
My question to you is, is it possible to make these fragments clickable and uniquely identifiable, and if so, what is the best way to go about doing that?
Thank you for your help!
What you could do is:
Modify the layout of your fragment, so it uses the RelativeLayout (now its probably LinearLayout right?). By modify I mean either use RelativeLayout INSTEAD of LinearLayout (more complicated but i think a bit better), or put your LinearLayout INSIDE RelativeLayout.
Place a view that matches in size the size of the fragment, on top of your textViews. Add an onClickListener to it that performes the code on clicks.
First - fragments are not clickable, layouts and their children are. A Fragment can handle the click action for things that occur in its layout but you do not "click" the fragment.
Second - sounds like you're confused about how to use fragments and what a Fragment actually does. A Fragment is like an Activity with some parts missing. A Fragment has its own lifecycle and can be used to perform many of the same tasks as an Activity the difference being that (1) a Fragment is "hosted" by an activity that provides the missing functions and does not run on its own (2) a Fragment can run in the background with no UI (I do not know of a background activity yet).
You do not need to/should not be using Fragments for what you are trying to do.
Your layout is really just a series of TextViews. You should be able to make that layout in XML reuse that layout file - by simply getting a LayoutInflater anywhere else in your app that you want to. If things got really crazy you could create a custom TextView class to provide detailed functionality but you should not be using 10 fragments, each with one TextView to make that layout work.
I just wanted to follow up on things in case anybody stumbles upon this answer in the future. It turns out using fragments was a bad idea. While I was able to make them clickable and identifiable through a bit of a hack job, I ran into problems later on when I was trying to fix layout issues. So, thanks to #Rarw I looked into using a LayoutInflater instead.
And that was much easier! I just made the layout xml file, used the LayoutInflater to add it to a LinearLayout and added that to the list. Makes things much easier down the road:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = new LinearLayout(this);
inflater.inflate(R.layout.basic, container, true);
container.setId(i);
list.addView(container, params);
container.setClickable(true);
container.setOnClickListener(this);
A hacky solution is to place a transparent button on top of the entire fragment, have the fragment extend onClickListener, and set the button's onClickListener to this.
One way to create such a button:
Use a constraintLayout as the root layout for the xml.
As the last of child of the constraintLayout, create a button, constrain it to occupy entire fragment by setting layout_constrainXXX to corner elements.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<!-- The remaining properties of the layout -->
<!-- All the children of the layout -->
<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#00000000"
android:id="#+id/fragment_button" />
</android.support.constraint.ConstraintLayout>
Note that you can do this other ways too, such as in a RelativeLayout
Then in the java/kotlin file, extend onClickListener.
override fun onClick(p0: View?) {
// Whatever you want to do here
}
In method onActivityCreated, set the transparent button's listener as:
var button : Button = fragment_button
button.setOnClickListener(this)
I have a simple layout, which contains two fragments. One of the two fragments periodically will replace itself, the other one will remain consistent for the whole time I am using it. For some reason, removing/adding it works just fine, but replacing it causes the other fragment to disappear. I know from debug code that the Fragment simply gets detached, for no apparent reason. Why might this be? If I use the latter, I have to be extremely careful to ensure that the QuestionArea is gone first...
These lines of code come from the in the FragmentActivity class.
fragManager.beginTransaction()
.replace(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
.commit();
fragManager.beginTransaction()
.remove(fragManager.findFragmentById(R.id.QuestionArea))
.add(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
.commit();
Here is what I hope is the other relevant code:
XML of the main display:
<?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:layout_gravity="center"
android:orientation="vertical" >
<com.pearsonartphoto.FontFitTextView
android:id="#+id/Name"
style="#style/bigText"
android:background="#color/blue"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/Instructions"
style="#style/medText"
android:layout_width="fill_parent"
android:layout_below="#id/PowerName"
/>
<fragment
android:name="com.pearsonartphoto.NumberDisplay"
android:gravity="center"
android:id="#+id/QuestionArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Instructions"/>
<fragment
android:name="com.pearsonartphoto.AbstractAnswerFragment"
android:id="#+id/AnswerArea"
style="#style/bigText"
android:gravity="center"
android:layout_below="#+id/QuestionArea"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="530sp"/>
</RelativeLayout>
Part of FragmentActivity which sets up the AnswerArea
protected void setAnswerPad(AbstractAnswerFragment pad)
{
fragManager.beginTransaction()
.replace(R.id.AnswerArea, pad, "AnswerArea")
.commit();
fragManager.executePendingTransactions();
answerPadToAnswer=pad.getAnswerKey();
}
First time (From FragmentActivity) that the QuestionArea is set.
fragManager.beginTransaction()
.replace(R.id.QuestionArea, getQuestionPreView(), "QuestionArea")
.commit();
The functions getPostView() and getPreView()
#Override
Fragment getQuestionPreView() {
NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
display.setNumber(-1);
return display;
}
#Override
Fragment getQuestionPostView() {
NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
display.setNumber(answer);
return display;
}
Your problems, most likely, come from trying to change static fragments(fragments declared in the xml layout) at runtime, which you shouldn't do. If you plan to replace, remove etc those fragments in your FragmentActivity you should put instead a wrapper layout(like a FrameLayout) in their places in the xml layout and use that container for future fragment transactions. Check this answer from one of the Android engineers.
There were a few things that needed to be done to make this work:
The key thing was to change my main XML code to FrameLayout.
I had to create some of the smaller fragments, when I wasn't having to create them before.
New main XML code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<com.pearsonartphoto.FontFitTextView
android:id="#+id/PowerName"
style="#style/bigText"
android:background="#color/blue"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<TextView android:id="#+id/Instructions"
style="#style/medText"
android:layout_width="fill_parent"
android:layout_below="#id/PowerName"
/>
<FrameLayout
android:gravity="center"
android:id="#+id/QuestionArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Instructions"/>
<FrameLayout
android:id="#+id/AnswerArea"
style="#style/bigText"
android:gravity="center"
android:layout_below="#+id/QuestionArea"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="530sp"/>
</RelativeLayout>
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!
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.
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">
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>
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.
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.