I started a TabLayout Activity, which includes the following code to create the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_find, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
I've read the official documentation and still unsure how it works. If somebody could explain in detail how each part here is working that would be great.
Edit: Mainly referring to View rootView = inflater.inflate(R.layout.fragment_find, container, false); what each of these 3 parameters are doing and how inflater.inflate() is working here.
OK, here we go.
The process of inflating is simply creating your view explicitly instead of doing this implicitly, this is by using this:
public void onCreate(){
setContentView(R.layout.your_layout);
}
Compare with this question.
Now with the arguments. Compare this with this section.
R.layout.fragment_find returns ID of a fragment used somewhere in the code. R is a dynamic android class used for manipulating some of your app's resources such as views, strings etc. Compare.
container is a root from some ViewGroup. So you hgave a group of, say, buttons doing common things (for example choosing some colour in your application), and they all have same parent, in your case called a container.
attachToRoot is the last argument. According to docs:
If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
So it is not attached to the parent we talked about in last point. Compare here.
I hope this helped.
onCreateView():
After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).
Here, it's a method of the lifecycle for Fragment.
Related
I am using ViewStubs to load show data in my layout. Since I'm using ButterKnife to bind the layout components, I have custom classes that hold the individual viewstub layout's components, e.g one such viewstub is as follows.
<ViewStub
android:id="#+id/featuredContentStub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="#+id/featuredContent"
android:layout="#layout/featured_content" />
The class to handle the #layout/featured_content components is as follows:
public class FeaturedContentView {
#BindView(R.id.art)
ImageView art;
#BindView(R.id.shade)
View shade;
#BindView(R.id.title)
TextView featuredTitle;
#BindView(R.id.performer)
TextView performer;
#BindView(R.id.featured_title)
KerningTextView featuredText;
#BindView(R.id.play_button)
Button play;
#BindView(R.id.shareText)
TextView shareText;
#BindView(R.id.addToFavorites)
ImageView addToFavs;
FeaturedContentView(View view) {
ButterKnife.bind(this, view);
}
}
I inflate the layout like this:
if (viewstub != null) {
View view = viewstub.inflate();
featuredContentView = new FeaturedContentView(view);
}
The above method is called in two different places in my fragment. It inflates correctly the first time but fails the second time citing java.lang.IllegalStateException: ViewStub must have a non-null ViewGroup viewParent.
How can i handle this situation.
Android inflates ViewStub like this:
Initially add a ViewStub to View hierarchy same way as any other View
Replace that View with specified layout when you call inflate.
This means, that when your code is called second time, the original ViewStub object is long detached from View hierarchy and already replaced by the full View.
Personally, I think that ViewStub in it's current form is highly inconvenient, especially when working with ButerKnife. Fortunately the class itself is very simple, you can always create a custom class, which does the same and add any required methods to it (such as isInflated, addInflateCallback etc.). Android support library developers have dones the same, btw.
If you look at source code of the viewstub.inflate() function, once the view referenced by the viewstub is inflated it removes the layout reference from the viewstub.
Hence you'll always get this error when viewstub.inflate gets called the second time. Here's how to prevent it:
if (mViewStub.getParent() != null) {
mViewStub.inflate();
} else {
mViewStub.setVisibility(View.VISIBLE);
}
I'm trying make an app that displays a big amount of text and images in a specific layout when the user clicks on a corresponding listview item. Since I want specific .xml layouts for separate 'chapters', I want only the layout of a fragment to change to the corresponding chapter.
Googling tells me I can do this with fragments, but as far as I understand, I need separate fragment classes and .xml layouts for every chapter I want to implement. With 2 or 3 chapters, that can be done, but with more chapters that will become I thought, isn't it simpler to just keep two fragments (one with a listview and one with the chapter text), but dynamically change the layout of the second fragment if the user clicks on an item in the listview.
Can this be done with some code like this (just thinking out loud)?
Int[] layouts = {
{R.layout.chapter1, R.layout.chapter2, R.layout.chapter3}
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
index = intent.getIntent("index", 0);
return inflater.inflate(layouts[index], container, false);
}
Or is there another way to achieve this?
Fragments at their core are View wrappers that contains states of Views. You can use them for other purposes like resource handling, but mostly they're just segments of an Activity state. It most likely would not be a good idea to have a Fragment for every single chapter unless each chapter has their own unique state that needs to be kept. However, if the Views are static, then a single Fragment is all you really need.
In this case, you simply have to have a method like this:
public void setChapter(int chapter)
{
Context ctx = getActivity();
if(ctx == null) {
// detached from Activity, so bail.
return;
}
if(chapter > layouts.length) {
return;
}
ViewGroup container = (ViewGroup) getView().findViewById(R.id.chapter_container);
container.removeAllViews();
View chapterInflater = LayoutInflater.from(ctx).inflate(layouts[chapter], container);
}
So this will wipe out all views currently in your container, inflate a new one, and put it in the container (most likely a simple FrameLayout).
The code in the original question can be used to initialize a Fragment if you want to open it at a certain point. onCreate_() methods are called only when the items is being "built" or "created". onCreateView() won't be called again though, so you need a method to change the layout once it's set.
Context: I'm using the Wizard with in Eclipse and ADT to get a master detail view framework.
I have a good understanding of the fragments need to sit with in an activity and that with in a view etc how ever looking at the example I'm trying to work out how best to change it so I can start developing a app my self.
Is the 'dummy' content view actually needed ? All it seems to contain is the menu options ?
The questions I have is does any one know how the detail view is created ? From what I am reading the app is just taking the item ID and placing it in the detail view as text ?
Extract from Itemdetailfragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
if (mItem != null) {
((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
}
return rootView;
}
Is the best way to re do a detail fragment for each new screen I need and place some logic behind that or code the existing one to look at the menu option selected and display the right code ?
Finally is this the best example to start working with ?
The questions I have is does any one know how the detail view is
created ? From what I am reading the app is just taking the item ID
and placing it in the detail view as text ?
Yes, that is exactly what it is doing. This is done in the detail fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_team_detail,
container, false);
// Show the dummy content as text in a TextView.
if (mItem != null) {
((TextView) rootView.findViewById(R.id.team_detail))
.setText(mItem.content);
}
return rootView;
}
You need to change it to get whatever data you need from whatever source you want to use (website, database or change the hard coded values
If you look at the DummyContent.java class you will see the comment
/** TODO: Replace all
uses of this class before publishing your app. */
Basically you should re-write this class to get the data that you want
I've actually added action bar sherlock to make this compatible with api 8 with a little help from this question Two questions about Master/Detail Flow Template
There is a hint that you could use ContentLoader in conjunction with a ContentProvider to get your data from another source such as a database or a web service
I'm writing a calculator application in which I would like to be able to switch between 4 modes of calculation: Decimal, Binary, Octal, and Hex. In order to manage the different UIs for the different modes, I have 4 Fragment subclasses in my Activity. Each Fragment has its own XML layout file, in addition to the main XML file for the Activity. I found a guide on the Android Developer site for inflating layouts for Fragments, and I've followed that guide. However, I would like to add listeners and so on to the various components of the layouts, preferably within the onCreateLayout method of the Fragment, or somewhere else where I could do it easily and minimize code duplication.
It appears, however, that when I try to call findViewByID to access one of the inflated Views (after I've called LayoutInflater.inflate, obviously), I get a null return value. This issue occurs whether I call findViewByID from within onCreateLayout or from elsewhere in the Activity (after the Views have, theoretically, been created). What's going wrong here?
One issue I think might be a problem is that I've overloaded the names of the Views between the various Fragment layouts. For example, the "1" button in the Binary layout has the same ID as the "1" button in the Hex layout. Is this allowed, assuming the Binary and Hex layouts are never both part of the Activity at the same time?
Thanks.
I think same id in different layout is not problem in Fragement. First you have to catch the inflated view then find whatever inside this. For example --
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.frg1, container, false);
android.util.Log.v("", "!!!!!!!!!! Frg1 !!!!!!!!!");
Button b = (Button) view.findViewById(R.id.b1);
b.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Toast.makeText(getActivity(), "here", Toast.LENGTH_SHORT).show();
}
});
return view;
}
I know how to get the root view with View.getRootView(). I am also able to get the view from a button's onClick event where the argument is a View. But how can I get the view in an activity?
If you need root view of your activity (so you can add your contents there) use
findViewById(android.R.id.content).getRootView()
Also it was reported that on some devices you have to use
getWindow().getDecorView().findViewById(android.R.id.content)
instead.
Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most devices it is not).
If you need to get view that you added to your activity using setContentView() method then as pottedmeat wrote you can use
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
But better just set id to this view in your xml layout and use this id instead.
This is what I use to get the root view as found in the XML file assigned with setContentView:
final ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
I tested this in android 4.0.3, only:
getWindow().getDecorView().getRootView()
give the same view what we get from
anyview.getRootView();
com.android.internal.policy.impl.PhoneWindow$DecorView##########
and
getWindow().getDecorView().findViewById(android.R.id.content)
giving child of its
android.widget.FrameLayout########
Please confirm.
Get root view from current activity.
Inside our activity we can get the root view with:
ViewGroup rootView = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
or
View rootView = getWindow().getDecorView().getRootView();
In Kotlin we can do it a little shorter:
val rootView = window.decorView.rootView
Just incase Someone needs an easier way:
The following code gives a view of the whole activity:
View v1 = getWindow().getDecorView().getRootView();
To get a certian view in the activity,for example an imageView inside the activity, simply add the id of that view you want to get:
View v1 = getWindow().getDecorView().getRootView().findViewById(R.id.imageView1);
Hope this helps somebody
Kotlin Extension Solution
Use this to simplify access in an Activity. Then you can directly refer to rootView from the Activity, or activity.rootView outside of it:
val Activity.rootView get() = window.decorView.rootView
If you'd like to add the same for Fragments for consistency, add:
val Fragment.rootView get() = view?.rootView
For those of you who are using the Data Binding Library, to get the root of the current activity, simply use:
View rootView = dataBinding.getRoot();
And for Kotlin users, it's even simpler:
val rootView = dataBinding.root
anyview.getRootView(); will be the easiest way.
to get View of the current Activity
in any onClick we will be getting "View view", by using 'view' get the rootView.
View view = view.getRootView();
and to get View in fragment
View view = FragmentClass.getView();
Another Kotlin Extension solution
If your activity's view is declared in xml (ex activity_root.xml), open the xml and assign an id to the root view:
android:id="#+id/root_activity"
Now in your class, import the view using:
import kotlinx.android.synthetic.main.activity_root.root_activity
You can now use root_activity as the view.
if you are in a activity, assume there is only one root view,you can get it like this.
ViewGroup viewGroup = (ViewGroup) ((ViewGroup) this
.findViewById(android.R.id.content)).getChildAt(0);
you can then cast it to your real class
or you could using
getWindow().getDecorView();
notice this will include the actionbar view, your view is below the actionbar view