In Activity's onCreate Method we can do the following
QuickReturnHeaderHelper helper = new QuickReturnHeaderHelper(this,
R.layout.activity_product_detail,R.layout.product_details_footer);
wholeView = helper.createView(productDetailDTO.isOwner());
setContentView(wholeView);
In fragment,we can return a layout from onCreateView(), we can inflate it from a layout resource defined in XML. To help us do so, onCreateView() provides a LayoutInflater object.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
Now, I want to use
whole view i.e my custom view
as my fragments rootview.I dont want to inflate if from a layout resource defined in XML.
But, until now I have not been able to do so. I have been searching in google for past few days but searching didnot produce any result. So, here I am posting this question.
I do not get your question completely but in my opinion what you can do is create an empty xml file(with relative layout only) and inflate it then add your custom view to it. Hope this will work, sorry if i was wrong
Related
I wonder what is container parameter in onCreateView(), cause when i inflate view to that container it make me wonder what viewGroup is this,is it a viewGroup from the activity that we will add a fragment to ? if it is true then why we need to attach it in inflate method cause i think we will add this fragment to the viewgroup of activity in activity's xml anyway.
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_top,container,true);
return view;
}
The inflate() method takes three arguments:
The resource ID of the layout you want to inflate.
The ViewGroup to be the parent of the inflated layout. Passing the container is important in order for the system to apply layout
parameters to the root view of the inflated layout, specified by the
parent view in which it's going.
A boolean indicating whether the inflated layout should be attached to the ViewGroup (the second parameter) during inflation.
For more refer here
To be more specific, I think the container is the FragmentContainerView in this example. Basically it's the resource id which you add your fragment to. For example, if we do
fragmentTransaction.add(R.id.container_view, fragment).commitNow();
Then the container is the ViewGroup identified by R.id.container_view.
Quoting the docs
container ViewGroup: If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view. This value may be null.
From what I understand, the LayoutInflater converts XML into Views. But when I use Buttons, TextViews or other widgets in code, I simply have to use findViewById() without having to inflate these Views first. Are these views automatically inflated? If so, when are views automatically inflated and when do you have to inflate them manually?
You just have to inflate an xml layout, and then all the viewgroups and views (buttons,textview,edittext,etc.) will automatically be shown.
So in an Activity class, oncreate method has a line SetContentview(), this inflates the xml layout.
An Activity needs to include a call to setContentView(R.layout.<your_layout_here>) in its onCreate method. That inflates the XML in the specified layout into the view hierarchy for the Activity. For a Fragment, override the onCreateView method like below:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.<your_layout_here>, container, false);
}
Once you've inflated a layout (and all of its children Views) into the view hierarchy, you can then use findViewById(R.id.<your_view_id>) to get a reference to the actual View object that you've inflated into the hierarchy and play with it.
you inflate your views manually if you create your activity and xml manually.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
}
Few things are here:
1. View for your activity(UI screen), that is set by setContentView() method.
2. View for a specific UI component e.g. Button, that is either created in xml of your activity_layout or you can inflate a separate xml layout file for your specific UI component. The best example and use is like inflating an xml layout file for your customised Toast.
So, here is the thing that relates setContentView() and inflating an xml layout file for a separate view:
Both of these provides a layout for view and view components, both of them creates a binary output for layout and use them as described above.
I would like to know if there is a way to access elements from different layouts in a Fragment class, something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view1 = inflater.inflate(R.activity_layout_1, container, false);
View view2 = inflater.inflate(R.activity_layout_2, container, false);
TextView textView1= (TextView) view1.findViewById(R.id.textView1);
TextView textView2= (TextView) view2.findViewById(R.id.textView2);
textView2.setText("text changed");
}
This is what I want to achieve
Am I missing something?
To add menu items to the ActionBar, you should override onCreateOptionsMenu(). You can do this in either an Activity or a Fragment. The "listener" is actually the onOptionsItemSelected() method, also within the Activity or Fragment subclass which created the menu. For details, check out the Menus API Guide.
If you are targetting API 23 (Marshmallow), you should look at Toolbar. Even for older versions, you can use Toolbar from the appcompat library.
Your code should work as written. What it will look like will depend upon the container. And, depending upon how you are setting up the fragment, you might not have control over container
Another options is to use <include> instead, for greater control. You have one layout that uses <include> elements to reference the ones that you want to reuse and, if needed, provide right containers for it all to work.
I have followed this tutorial and its working great. In my fragment's view there are some buttons which I have some onClick assigned to. For example I have a button like:
<ImageButton
android:onClick="doSomething">
</ImageButton>
While creating the rootView I use the following code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
return rootView;
}
I did some googling and found out the last parameter is for attach to parentView and I have set it to true. But the parent view is container. Where does this container point to?
Where should I write the function 'doSomething'?
I did some googling and found out the last parameter is for attach to
parentView and I have set it to true.
Don't set it to true. In the onCreateView() you'll create the view for the fragment and return it and Android will attach that view to the layout on its own(what the documentation says).
Where does this container point to?
If not null it will be the layout where the fragment's view will be added, generally used for generating the proper LayoutParams for the newly inflated view.
Where should I write the function 'doSomething'?
As the buttons using the onClick attribute will be part of the fragment layout you should remove the onClick attribute from the layout and set the OnClickListener in code on the buttons. Android will search for the doSomething() method at the Activity level so you can't receive the on click event directly in the fragment.
The inflate method of the LayoutInflater abstract class has as the second parameter of the inflate method that takes ViewGroup root. From the documentation, it is mentioned as an "Optional view to be the parent of the generated hierarchy."
Can someone give an example on how to use this parameter? And what would you put in there? A ViewGroup can be any type of layout like LinearLayout.
I have not quite understood what to do with this parameter. If the view you are inflating is not part of the layout that is entered here then it would give an error. Don't understand the purpose of it.
More from the documentation:
public View inflate (XmlPullParser parser, ViewGroup root)
Added in API level 1
Inflate a new view hierarchy from the specified xml node. Throws InflateException if there is an error.
Important for performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at run time.
Parameters
parser XML dom node containing the description of the view hierarchy.
root Optional view to be the parent of the generated hierarchy.
Returns
The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.
inflate method returns the parent View of inflated xml
In case you don't pass root ViewGroup, inflated xml root View
will be returned
In case you pass root ViewGroup, root View of root ViewGroup will
be returned
I'm not really sure what part of that confuses you, to be honest. You can pass in any ViewGroup, to be the parent of the views that you're creating dynamically.
For example:
private static View mView = inflater.inflate(R.layout.fragment_featured, container, false);
This will inflate the layout contained in the fragment_featured xml file within the container (as a parent). The type of the container ViewGroup is up to you.