The app I'm currently developing has an action bar with 5 tabs (fragments). One of these fragment shows an alert dialog, but the layout is blank. I want to put a background image, so I created a layout for that fragment and used inflater.inflate(...) method to set the layout.
Problem is that line of code sets that layout to ALL fragments. How can I limit it just to the fragment I need? Here's my code:
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//...
AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Title")
//...
ad.show();
inflater.inflate(R.layout.fragment_my, container); //this is the layout I want to inflate to my fragment
return super.onCreateView(inflater, container, savedInstanceState);
}
Also tried to replace the last two lines with:
return inflater.inflate(R.layout.fragment_trovami, container);
but i'm getting this error:
07-27 16:19:46.768: E/AndroidRuntime(1998): java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
Answer :
return inflater.inflate(R.layout.fragment_trovami, container,false);
Related
I am inflating a fragment from activity at runtime. So, in order to inflate the view of the fragment in the fragment class I called:
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.confirm_fragment, container);
}
At this moment I get a crash with logs:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
But if I modify my method as:
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable final ViewGroup container, #Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.confirm_fragment, null);
}
where I now specify my container as null, it works. But what I didn't understand is where did I specify a parent for the view in the code which was crashing?
Read from this link that why and when you pass parent container's instance or null in attachToRoot param.
From link:
Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false);
mLinearLayout.addView(button);
By passing in false, we say that we do not want to attach our View to the root
ViewGroup just yet. We are saying that it will happen at some other
point in time. In this example, the other point in time is simply the
addView() method used immediately below inflation.
So if you inflate with:
return inflater.inflate(R.layout.confirm_fragment, container);
First it will immediately attach confirm_fragment to container. After returning the view, fragment will be tried to be added to parent again implicitly, but since it has been added already, exception will be thrown:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Hope you got the point.
where did I specify a parent for the view [...] ?
You add a Fragment dynamically by writing something like this:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(android.R.id.content, myFragment, FRAGMENT_TAG)
.addToBackStack(null)
.commit();
In add(), you specify where the Fragment should be displayed (in this case fullscreen) by passing the id of the desired container.
See also the method description in the reference developers.android.com:
containerViewId int: Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.
I you don't add a Fragment to the UI (e.g. you need it for caching purposes as a retained fragment), onCreateView() will never be called.
So once onCreateView() is called, the parent for this Fragment already exists - the FragmentManager set it for you.
Try to use the following code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.confirm_fragment, container, false);
return view;
}
You need to set attachToParent to false
Example:
inflater.inflate(R.layout.confirm_fragment, container,false);
I have an HomeAcivity with 4 fragments. 3 of the fragments have no Toolbar of their own and I am showing the Activity's Toolbar in them. However, the 4th Fragment have Toolbar of its own.
Currently I am seeing 2 Toolbars in 4th Fragment, one its own and other that of Activity.How can I hide the Activity's Toolbar and just show its own Toolbar?
Please note that I don't want to update the content of Activity's Toolbar instead of showing new Toolbar inside Fragment. I want to show Fragments own Toolbar and hide that of Activity.
Please help me out.
You can hide activity's toolbar from your 4th fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.GONE);
return rootView;
}
and show activity's toolbar from other three fragments
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_view, container, false);
((ToolbarView)getActivity().findViewById(R.id.toolbarView)).setVisibility(View.VISIBLE);
return rootView;
}
I've got a fragment activity that appears as a dialog window. I call to this activity this way:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/** Inflates the main layout to be used by this fragment*/
final View detailView =
inflater.inflate(R.layout.contact_detail_fragment, container, false);
In other part of the code, when I push a button of this layout, I need this activity to be closed, because it remains opened.
So, what will the way to finish this?
I have a FragmentActivity which sets a main layout with 3 tabs. Each tab has it's own fragment which inflates is own layout into main layout.
For example, one of the three Fragments:
public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return inflater.inflate(R.layout.fragment1, container, false);
}
}
When I add all the fragments and connect them with tabs everything works. When I click on tab1, layout of the Fragment1 shows, when I click on Tab2, layout of the Fragment2 shows.
The problem is that when I want to change something in that inflated layout (for example execute setText to textView in fragment1 layout), I receive a NullPointerException in that line and application stops.
This doesn't work:
TextView test = (TextView) findViewById(R.id.fragmentOneText1);
test.setText("Test text!");
How to set data to inflated layout?
You have to inflate your fragment specific layout first by using LayoutInflater and map the control in your Fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.R.layout.fragment1, container, false);
TextView text = (TextView) fragmentView.findViewById(R.id.fragmentOneText1);
mTextNoResult=(TextView)fragmentView.findViewById(R.id.text_noresult);
return fragmentView;
}
I made a program that uses FragmentTabs for implementing ActionBar Tabs.
I was wondering how it is possible to add a widget to a inflated Fragments in Tabs. It's more complicated than normal because the fragment is returning only an onCreateView to the TabListener, so how would I add in functioning widgets? I don't need specific code, just the logic please!
Here is a sample Fragment being inflated when a Tab is clicked (inside of which I need widgets):
public class IdFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.id_layout, container, false);
}
}