getLayoutInflator() in Android - java

I have read the android documentation about getLayoutInflator and I am still not understanding what it does. Can someone give me a use case for this method or may be during what time would you want to call getLayoutInflator?

XML Layouts in Android need to be Inflated (parsed into View objects) before they are used. getLayoutInflator() gets you an instance of the LayoutInflator that will allow you to manually inflate layouts for specific uses.
One example being in a Custom ArrayAdapter to populate a ListView with a Custom Layout.
You need to manually inflate and populate your desired Layout for each individual list item in the ArrayAdapter's overridden getView() method.

Use setContentView() when you're in an Activity. That method inflates the layout and displays the selected layout as the view for that Activity. But when you're NOT in an Activity and you need to work with a layout file, you have to inflate it to get access to the view objects in the XML.

Related

Make Fragment appear first in LinearLayout

I am building parts of my layout programmatically.
My process looks like this:
I inflate a layout: View view = inflater.inflate(R.layout.view_layout, viewGroup, false);
I add this view to the ViewGroup: viewGroup.addView(view);
Before adding views in this way, I first add a fragment to the root view of viewGroup:
getChildFragmentManager().beginTransaction().add(R.id.viewgroup_root, fragment, "fragment_tag").commit();
My ViewGroup is a LinearLayout and I am adding the Fragment before I add the other views. However, the Fragment is appearing last - after all the views I add using ViewGroup.addView().
How can I get the added Fragment to be displayed first in the LinearLayout, and why is the LinearLayout displaying it last if it was added first?
commit() is asynchronous - it does not run immediately. Therefore you definitely are running your addView methods before your fragment is actually added.
As Fragments automatically are re-added to their respective layout based on ID and you do not control the ordering of when the fragment is added to the layout in those cases, you can't rely on any initial ordering.
Instead, you should always add a Fragment to its own container - if using Fragment 1.2.0 or higher (the latest right now is 1.2.5), you should add a FragmentContainerView to your LinearLayout. If you're using an earlier version of Fragments, you'd want to add a FrameLayout to your LinearLayout. In either case, you'd need to make sure you use setId() with that layout and use the same ID when you use add.
You will have to leave placeholder under your LinearLayout and use placeholder's ID to do beginTransaction/add stuff. Any viewgroup would do. You are not just adding another view to your viewgroup, you are adding a fragment. It has its own lifecycle, bound to entities hosting it (activity, fragment).

Get UI Element ID In a Fragment From Custom adapter

I'm developing an android app and I have a fragment that contains TextView & ListView in it. The list view has custom list items that contains two buttons. I want to make 'onClickListener' for one of those two buttons in my custom adapter class to change the text of the TextView, but I can't access it by findViewById() every time I try I got null exception.
My guess is that you're trying to access the TextView from inside of the Adapter.
If so, then you won't be able to get the TextView and it's normal to get a nullpointer exception.
The findViewById inside your adapter only finds the views that you have inflated in the getView() method of your adapter.
What you can do here is probably use an interface that's implemented by your Fragment to pass the information from your adapter back to the fragment.
This answer might be a good starting point : How to create interface between Fragment and adapter?

Create a composite view in Android

I'd like to create a custom view (I'll call it MyComplexView), for example a RelativeLayout with an Imageview, a TextView, and a Button.
I'd like to declare an xml with the layout and then create the class:
MyComplexView extends RelativeLayout{...}
But I don't know what I should override to indicate which layout should be inflated.
How can I do this? Thanks
Something like this:
add the constructors from the super class. (the one with just context is for creating the views programaticaly, the others are for when you add the view in XML.
create a method called init() for example and call it from each constructor.
inside the init method do:
LayoutInflater.from(context).inflate(R.layout.my_view_layout, this, true);
now in inflate the additional params actually mean:
true -> attach the layout to the root in your case relative layout (pro-tip: so inside the xml you can have just merge tags if your layout root is also relative layout and align them in code so the hierarchy is simpler) or any layout you like.
this -> the layout to attach the inflated view to in your case the relative layout you are extending.
it will automatically be attached to the root -> extends RelativeLayout.
then you can use findViewById like:
this.findViewById(R.id.myView);
I'm not 100% sure what your main goal is, so I try to be thorough:
If you want to include a complex layout in other layouts, then you can simply define my_complext_layout.xml, and in your other layouts put:
<include layout="#layout/my_complext_layout" />
If you need to run your own code, then you could simply make the root of this layout to be MyComplexView, and you can run code when the view is created.
If you have intended to let your code operate on the layout, then simply implement an OnGlobalLayoutListener and add it to your layout in your views constructor.
Implement a Constructor for the MyComplexView:
public MyComplexView(Context context, AttributeSet attrs){
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.header_view, this, true);
mHeaderView = (TextView)findViewById(R.id.header);
if(mHeaderView != null)
mHeaderView.setText("Test");
}
See Custom Components in the developer docs. In particular the Compound Controls section.
Once you've made your java file, in order to refer to it in xml you'll have to use a fully qualified packagename i.e:
<com.yourpackage.YourCustomView
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Creating a custom view usually is aimed to create a widget which doesn't exist yet. What you're trying to do is to have the same layout repeated at multiple places.
You have severall options to do that according to your context.
If the layout is to be placed in a lest, just create your layout in a separate file, and use it in a ListAdapter. Take a look at the ListView Tutorial for this.
If this layout is a generic layout to be embedded in multiple activities, try using a Fragment instead. Fragments are subparts of an activity, with their own views. Alternatively, you can just embed the layout in severall xml using the tag.
If really you want a custom class and single widget, then you need to extend the View class. Extending a layout means you wan't to organize child widgets differently (for example, organize them in circle). Extending a View, you can have exactly what you want (button, image, text) organized always in the same way. But I won't lie to you, this will mean lot of work.

List Adapter and getView function explanation

I'm thoroughly confused about the life cycle of list view. More specifically, what does the list adapter do exactly? Does it just provide data to the given view? And when/where does the getView() function gets called? And what purpose does this getView() function provide? From just looking at the code, it looks like getView() is "assigning" data to the view to be displayed. I'd like to be able to use list views without having to memorize, do this and then this in order for it to work. I'd much rather understand it so I can use it properly. Someone please help me understand all of this.
Also, if someone can explain to me.. what's the difference between BaseAdapter and ArrayAdapter? and any other kind of adapters that comes with Android.
What I have understood is your adapter constructor instantiated by activity and then on activity launch the getView() method is called. the {#param position, view, viewGroup}
position: it refers to the position of the view as given by adapter. Please Note it is different from the position in {OnItemClick(AdapterView adapter, View v, int position,long id)} here position is the list item position. The {position} in {getView()} changes after particular object in the list are displayed again for eg. when you scroll.
view: the view here is the view you want to be presented through getView(). It can be a particular XML layout for each row. So this states clearly that getView is called to plot every row. this view needs to be valid one or another layout (LinearLayout by default) will be selected to maintain uniqueness.
viewgroup: as you might know and as name says will be the container of your #param:view
any other point is appreciated.
getView() fills in the data into the item's view with the given index. The view which is given as a parameter may be a pre-inflated view. If it is not, you have to infalte it yourself.
An ArrayAdapter simply calls setText on the given view with the result of toString() of the object with the respective index from the array. If you override it, you can do more complex stuff, like assigning a picture or filling in more TextViews.
I recommend the following tutorial: http://www.softwarepassion.com/android-series-custom-listview-items-and-adapters/
Hi list adaper provides view for listview.
when user scrolls listview at that time getview is called.
getview is used to populate your view with data hence the name adapter.
The Adapter does all the "rember to do this" for you. If you change a list view's backing data structure through the adapter's methods (e.g. "add()") it will fire all the datachanged and update events you'll need for the list view to show the new state of the data.

Creating xml template for feed/list items in Android?

I'm helping a friend create an android app that will have screens with lists of info similar to a feed. I've been learning xml layout in Android and have some of the basics down, but don't have a lot of familiarity with doing the java stuff. I've successfully created includes to seperate layout files for compontents within a screen, but what I'm wondering is if such a component can be used as a kind of template for feed/list items that get inserted programmatically on the back end. IE, is there a way to have Android create a list and for each list item it uses the external xml as a template? Sorry if this is somewhat vague, I'm new to this and trying to understand what our options are. TIA!
Yes, every list item can be a custom layout. In fact you always have to define a layout for the list entries. You can either choose a prebuilt one from android.R.layout or you can use your own from R.layout. You can specify it when you create the list adapter in code.
Have a look at one of the ArrayAdapter constructors for example:
public ArrayAdapter (Context context, int resource, int
textViewResourceId)
Since: API Level 1 Constructor Parameters
context - the current context.
resource - The resource ID for a layout file containing a
layout to use when instantiating views.
textViewResourceId - The id of the TextView within the layout resource to be populated
The constructor takes a layout that will be used for the ListView childs. Works similar with other adapters.
What you usually do is inflating the layout inside getView() of the adapter though. When you did that, fill all the data you need into the views of the layout, and return the view.
Note that you get an argument called convertView. This is one of the older layouts you already inflated before. In most cases the user just scrolled down and that entry is not visible anymore. If this convertView is not null, you can fill your data in there instead of inflating the whole layout again (thats expensive).
You can find a working example inside the
ANDROID_SDK\samples\android-10\ApiDemos\src\com\example\android\apis\view\List5.java file. Also take a look at the other list examples in that folder.

Categories