- How to inflate XML-Layout-File correctly inside Custom ViewGroup?
- 6 Answers 6
- How to inflate a textview xml in my layout?
- 4 Answers 4
- How to inflate one view with a layout
- 14 Answers 14
- What does it mean to inflate a view from an xml file?
- 7 Answers 7
- What does LayoutInflater in Android do?
- 15 Answers 15
- What does LayoutInflator do?
- Example
How to inflate XML-Layout-File correctly inside Custom ViewGroup?
I want to inflate a XML-Layout-File in a custom ViewGroup Class, my Problem is that it produces just a empty screen. Doing the same in the Activity Class works fine. Here is my simple XML-Layout-File:
Here is the working Version, inflating the shownumberlayout.xml in the Activity ShowNumber :
This shows a White Background with the black Text “Test” centered.
Now the Version inflating the xml in the Custom ViewGroup -Class:
ShowNumber.class public class ShowNumber extends Activity < /** Called when the activity is first created. */
Im doing it basically like in this Answer explained. This just produces a Empty Black Screen. What I am doing wrong?
UPDATE 1
@Konstantin I applied your changes but still just a blank screen, i also made a log-ouput for getting the number of children. It remains always 1, even i add one more Textview to the XML-Layout-File. Before the Changes it remains always 0.
@Sankar This is the Logcat, after the Changes from Konstantin:
UPDATE 2
The Content is finally showing correctly up. The missing thing was to override the Method onLayout (thanks to Franco) in the RelativeLayout-Sublcass:
Note: Later you should also override the Method onMeasurement() , but currently the content is also showing correctly without overriding it.
Now the solution for the Method initView from Franco do not align the TextView in the Center, but puts it in the top left corner. The solution from Konstantin puts it correctly in the Center of the View:
6 Answers 6
I don’t know what’s going on in these answers.. I think calling onLayout manually is a bit crazy.
The LayoutInflater.inflate function is rather straight forward. If you’re using the one that accepts the 3rd boolean argument (attachToRoot) and it’s true, it will add the views from your XML to your parent view (the 2nd argument). If you’re using the one that accepts only 2 arguments, it will pass true to attachToRoot by default if you provide a parent that isn’t null.
In any case, most of the mess you’re experiencing is because the views from your XML are added to your custom view. Since your XML has a RelativeLayout root and your custom view is also a RelativeLayout — you’re getting a relative layout inside a relative layout.
This is probably not what you want.
The answer given by Konstantin makes sense, but you’re wasting a view because you’re placing a RelativeLayout inside a FrameLayout . Since Android can’t hold too many nested views, it’s a good idea to optimize and not add this unnecessary FrameLayout .
I suggest keeping your custom view as RelativeLayout and changing your XML root to . Then use the inflate form which adds the XML views to your parent — like View.inflate(context,int,this)
The purpose of the tag is to skip the root node in the XML.. look it up.
Источник
How to inflate a textview xml in my layout?
I have created a custom textview class and i am trying to inflate it in my main xml. Here is my code :-
and in my main activity xml, i have only one linear layout :-
I know if i extend it to linear layout instead of textview and add a lienarlayout as parent and in it that textview, it works.
But the problem is that i want to inflate an xml with only a textview and inflate it and the above code is not working. Please suggest
How do inflate a xml containing only one textview using layout inflator ?
4 Answers 4
It’s old and 2 years later, I ran into the same issue.
You cannot extend from a View and inflate from a layout. The view needs to be wrapped by a ViewGroup . Read Understanding Android’s LayoutInflater.inflate()
I came up with 2 solutions , either :
1. in your class CustomTextView , you set programmatically all the properties . Don’t inflate anything. Good luck with conversion from resources dimen.xml
2. In your Activity , inflate the Textview
You can find all of your custom created views under heading «Custom & Library Views» in the Graphical interface of Eclipse.
If your CustomTextView is in the package «com.example» than you can define your custom component as :
A bit late but maybe it will help people.
first and like Pratik goyal said you need to declare your CustomTextView and not a TextView
Then in your CustomTextView class you need to had those lines (Constructor):
And last (correct me if i’m wrong people), but you can’t inflate a customView into a TextView.
Источник
How to inflate one view with a layout
I have a layout defined in XML. It contains also:
I would like to inflate this RelativeView with other XML layout file. I may use different layouts depending on a situation. How should I do it? I was trying different variations of
But none of them worked fine.
14 Answers 14
I’m not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
You inflate an XML resource. See the LayoutInflater doc .
If your layout is in a mylayout.xml, you would do something like:
Though late answer, but would like to add that one way to get this
where item is the parent layout where you want to add a child layout.
It’s helpful to add to this, even though it’s an old post, that if the child view that is being inflated from xml is to be added to a viewgroup layout, you need to call inflate with a clue of what type of viewgroup it is going to be added to. Like:
The inflate method is quite overloaded and describes this part of the usage in the docs. I had a problem where a single view inflated from xml wasn’t aligning in the parent properly until I made this type of change.
Even simpler way is to use
- If you just want to inflate your layout :
- If you want to inflate your layout in container(parent layout) :
If you’re not in an activity you can use the static from() method from the LayoutInflater class to get a LayoutInflater , or request the service from the context method getSystemService() too :
(I know it’s almost 4 years ago but still worth mentioning)
AttachToRoot Set to True
Just think we specified a button in an XML layout file with its layout width and layout height set to match_parent.
On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.
Hope this solution works for you.!
If you want to add a single view multiple time then you have to use
then it will throw exception of all ready added view.
I had the hardest time with this error, because of my unique circumstances, but finally found a solution.
My situation: I am using a separate view (XML) which holds a WebView , then opens in an AlertDialog when I click a button in my main activity view. But somehow or another the WebView belonged to the main activity view (probably because I pull the resource from here), so right before I assigned it to my AlertDialog (as a view), I had to get the parent of my WebView , put it into a ViewGroup , then remove all the views on that ViewGroup . This worked, and my error went away.
Источник
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.
7 Answers 7
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:
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)
«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:
Then we have to create a layout where we can add above views:
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.
Источник
What does LayoutInflater in Android do?
What is the use of LayoutInflater in Android?
15 Answers 15
The LayoutInflater class is used to instantiate the contents of layout XML files into their corresponding View objects.
In other words, it takes an XML file as input and builds the View objects from it.
What does LayoutInflator do?
When I first started Android programming, I was really confused by LayoutInflater and findViewById . Sometimes we used one and sometimes the other.
- LayoutInflater is used to create a new View (or Layout ) object from one of your xml layouts.
- findViewById just gives you a reference to a view than has already been created. You might think that you haven’t created any views yet, but whenever you call setContentView in onCreate , the activity’s layout along with its subviews gets inflated (created) behind the scenes.
So if the view already exists, then use findViewById . If not, then create it with a LayoutInflater .
Example
Here is a mini project I made that shows both LayoutInflater and findViewById in action. With no special code, the layout looks like this.
The blue square is a custom layout inserted into the main layout with include (see here for more). It was inflated automatically because it is part of the content view. As you can see, there is nothing special about the code.
Now let’s inflate (create) another copy of our custom layout and add it in.
To inflate the new view layout, all I did was tell the inflater the name of my xml file ( my_layout ), the parent layout that I want to add it to ( mainLayout ), and that I don’t actually want to add it yet ( false ). (I could also set the parent to null , but then the layout parameters of my custom layout’s root view would be ignored.)
Here it is again in context.
Notice how findViewById is used only after a layout has already been inflated.
Источник