Add XML content to a View - android

im developing an application and I need to add elements dynamically. I wonder if I can append elements (stored in a XML file) in my current Activity, like innerHTML in JavaScript.
I tried LayoutInflater but that replaces all the content and I need to append.
Thanks!

The easiest way to do this is to use the LayoutInflater as you said. I'm not sure how you were doing it (hence why I asked to see your inflating code), but the simplest way to understand is to do the following:
LayoutInflater inflater = getLayoutInflater();
View viewToAppend = inflater.inflate(R.layout.some_layout, null);
// Optional, create LayoutParams and apply to view with
// viewToAppend.setLayoutParams(params);
mainView.addView(viewToAppend);

Related

What is the LayoutInflater in Android? How does it differs from findViewById?

An example LayoutInflater code:
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.,parent,false);
Can't understand the work of LayoutInflater in Android. Tried to read the official documentation of the android but, couldn't get the concept.
The main difference between findViewById and LayoutInflater is that findViewById is used to access existing views in a layout, while LayoutInflater is used to create a new view hierarchy from an XML layout file.
FindViewById is a method of the Activity class, while LayoutInflater is a separate class on it's own.
FindViewById is used to access a view in the current layout, this means lets us find Views from layouts written in XML and returns a reference to their Java objects, while LayoutInflater is used to create a new view hierarchy from an XML layout file, so this class is the responsible for “inflating” the layouts.
Hope it helps.

Dynamic layout design according to data received from web-service call

Hi ,in my app i need to build layout dynamically.All the dimensions of views in layout i will get from web-service.In the web-service we may have different tile sizes,According on tile size we have to place the tile in appropriate position.
Can any one help me to build layout dynamically.
You can inflate different layout programmatically.
So you should have something like this:
LayoutInflater inflater = getLayoutInflater();
ViewGroup layoutLoaded = (ViewGroup)inflater.inflate(R.layout.yourlayout, null);
mainLayout.addView( layoudLoaded );
In this way you can load all layout you want when you need.

Is there a way to specify a Layout to use for a ListView when the adapter is empty?

I have a complex empty view in a layout, with an icon, text, button, etc.
It is easy to select a view within my layout.xml to use when the listview is empty, similar to
getListView().setEmptyView(findViewById(R.id.empty));
This code sets the empty view works just fine when it resides in the layout.xml file.
Now I want to refactor this view into its own empty.xml layout file, and have coded it similar to the following:
// Setup the empty layout.xml
LayoutInflater vi = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vlEmpty = vi.inflate(R.layout.empty, null);
// Find the empty layout view
vEmpty = vlEmpty.findViewById(R.id.llEmpty);
vEmpty.setOnClickListener(ocl);
// Find the ListView
vListView = (ListView) findViewById(R.id.lvWords);
vListView.setEmptyView(vEmpty);
The problem is that the details within llEmpty never show up; The exact same layout and view works withing the main layout, just not refactored into its own xml file.
Has anyone got something like this to work?
You might need to pass the proper context to the inflater:
vListView = (ListView) findViewById(R.id.lvWords);
View vlEmpty = vi.inflate(R.layout.empty, (ViewGroup)vListView.getParent());
which (should) make them both live in the same root view. It may be sufficient to just pass the root view of the parent activity.
Let me know if that works.
I doubt that setEmptyView() automatically makes the supplied View a child of any container in your activity.
Personally, I'd just use the <include> element rather than inflating it separately. But, if you really want to inflate it separately, the answer that Femi posted while I was writing this may work, depending on what the ListView's parent is.

What does it mean to inflate a view from an xml file?

I am new to android development and keep coming across references to Inflating views from a layout xml file. I googled and searched the development guide but still wasn't able to pick up a sense for what it means. If someone could provide a very simple example, it'd be much appreciated.
When you write an XML layout, it will be inflated by the Android OS which basically means that it will be rendered by creating view object in memory. Let's call that implicit inflation (the OS will inflate the view for you). For instance:
class Name extends Activity{
public void onCreate(){
// the OS will inflate the your_layout.xml
// file and use it for this activity
setContentView(R.layout.your_layout);
}
}
You can also inflate views explicitly by using the LayoutInflater. In that case you have to:
Get an instance of the LayoutInflater
Specify the XML to inflate
Use the returned View
Set the content view with returned view (above)
For instance:
LayoutInflater inflater = LayoutInflater.from(YourActivity.this); // 1
View theInflatedView = inflater.inflate(R.layout.your_layout, null); // 2 and 3
setContentView(theInflatedView) // 4
"Inflating" a view means taking the layout XML and parsing it to create the view and viewgroup objects from the elements and their attributes specified within, and then adding the hierarchy of those views and viewgroups to the parent ViewGroup. When you call setContentView(), it attaches the views it creates from reading the XML to the activity. You can also use LayoutInflater to add views to another ViewGroup, which can be a useful tool in a lot of circumstances.
Inflating is the process of adding a view (.xml) to activity on runtime. When we create a listView we inflate each of its items dynamically. If we want to create a ViewGroup with multiple views like buttons and textview, we can create it like so:
Button but = new Button();
but.setText ="button text";
but.background ...
but.leftDrawable.. and so on...
TextView txt = new TextView();
txt.setText ="button text";
txt.background ... and so on...
Then we have to create a layout where we can add above views:
RelativeLayout rel = new RelativeLayout();
rel.addView(but);
And now if we want to add a button in the right-corner and a textview on the bottom, we have to do a lot of work. First by instantiating the view properties and then applying multiple constraints. This is time consuming.
Android makes it easy for us to create a simple .xml and design its style and attributes in xml and then simply inflate it wherever we need it without the pain of setting constraints programatically.
LayoutInflater inflater =
(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true);
//now add menuLayout to wherever you want to add like
(RelativeLayout)findViewById(R.id.relative).addView(menuLayout);
A layman definition for inflation might be to convert the XML code to Java code. Just a way to understand, e.g., if we have a tag in XML, OS has to create a corresponding Java object in memory, so inflatter reads the XMLtags, and creates the corresponding objects in Java.
I think here "inflating a view" means fetching the layout.xml file drawing a view specified in that xml file and POPULATING ( = inflating ) the parent viewGroup with the created View.
Because we make UI into XML but view objects is what we display so we somehow need to convert xml into view objects so inflating means we are converting xml into view objects so that it can be displayed, for this we need a service called layout inflator service and give it an xml and it will be convert for you.
In the iOS UIKit universe, this means getting the reference to the .Xib (which is XML, just like android) file and adding it to the current ViewController's view hierarchy.

Inflator in Android Application Development

Can anybody please tell What Inflator is and how it is being used in an Android application?
I don't know the exact use of it and Why it is being used.
My preferred way to handle inflation:
//First get our inflater ready, you'll need the application/activity context for this
LayoutInflater mInflater;
mInflater = LayoutInflater.from(mContext);
//Inflate the view from xml
View newView = mInflater.inflate(R.layout.my_new_layout, null);
//Then you'll want to add it to an existing layout object
mMainLayout.add(newView);
//Or perhaps just set it as the main view (though this method can also
// inflate the XML for you if you give it the resource id directly)
setContentView(newView);
Basically, you use it to inflate existing xml layouts at runtime. Usually you go ahead and insert those new views into previously defined ViewGroups or List objects.
Not quite sure what you mean, but if its related with inflating views, its used to load layout xml files into your application. e.g by
View myWelcome = View.inflate(this, R.layout.welcome, null);
Its easier and consider best practice to have you view definition inside layout xml files, instead of creating your views fully by code.
layout inflator is used to return a java object of your complete layout
suppose you have a layout xml file in which the root element is relative layout and it contains a imageview and textview then using layout inflator you can return a view object that refers to entire layout.
this basically is used in list view and grid view to plug into them a layout object of single row or element which is to be repeated.
you were asking for use of Inflator..
basically when you want to use two xml files in one java class ,inflator is used and its code is simple which is given below..
TextView text;
View layout;
LayoutInflater inflator=getLayoutInflater();
layout =inflator.inflate(R.layout.new_xml_that you want to use in that java class,null);
text=(TextView)layout.findViewById(R.id.text);
text.setText("progressing");
here i use textview,this is present in next xml with id=text
thats it..
if you find this worthy then please like this..
thanks

Categories

Resources