I am trying to get a reference to a LinearLayout so I can add an element.
Here is my xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".MainActivity">
Is it acceptable to have the line android:id="#+id/myLayout" in there?
My incorrect attempt to try and get a reference to my layout is with the following:
LayoutInflater myLayoutInflater;
LinearLayout myLayout;
if((myLayoutInflater = m_Context.getLayoutInflater()) != null) {
myLayout = (LinearLayout) myLayoutInflater.inflate(R.id.myLayout, null);
}
It underlines the R.id.myLayout in the inflate() line in red saying:
Expected resource of type Layout. Ensure resource ids passed to APIs are of the right type.
There's a misunderstanding about those methods.
LayoutInflater.inflate
This method expects the id of a layout file (and not of a layout view). So, you should call:
myLayoutInflater.inflate(R.layout.<NAME_OF_THE_XML_LAYOUT_FILE>, null);
That method will return whole view that was inflated. So, now that you have the inflated view, you can search Views inside of it. You can search view by their ID.
findViewById()
This method expects the id of the View. So, here, you should call:
View inflatedView = myLayoutInflater.inflate(R.layout.<NAME_OF_THE_XML_LAYOUT_FILE>, null);
LinearLayout linearLayout = inflatedView.findViewById(R.id.myLayout); // Assuming you added android:id="#+id/myLayout" to the LinearLayout
Note that first, we inflated the xml file and then, we started to seach for views inside of it.
HOWEVER
If your view is part of an Activity, you don't need to inflate that layout. You can instead:
public void onCreate() {
....
// This will inflate and add your layout to the actvity
setContentView(R.layout.<NAME_OF_THE_LAYOUT_FILE);
// After that line, you can call:
LinearLayout linearLayout = inflatedView.findViewById(R.id.myLayout); // Assuming you added android:id="#+id/myLayout" to the LinearLayout
// Since your view was added to the activity, you can search for R.id.myLayout
// If you search for any view before setContentView(), it will return null
// Because no view was added the screen yet
}
It is ok to find a view by id for a layout just like you do for a view:
LinearLayout layout = (LinearLayout) findViewById(R.id.myLayout);
This will do the trick if you are in an Activity context.
To add a view to it you can then do:
layout.addView(...)
You are getting that error because the LayoutInflater expects the name of the layout file not your layout id, so something like R.layout.item_layout. You also don't want to pass null for the parent view group in most cases so I would not recommend inflating it this way unless you know the parent layout.
Try something like this,
LayoutInflater myLayoutInflater = LayoutInflater.fromContext(mContext);
LinearLayout myLayout = myLayoutInflater.inflate(R.layout.layout_file, null);
View view = (LinearLayout)view.findViewById(R.id.myLayout);
This is my use case:
I want to change my inflated layout at run time, say first I inflate layout a, then after some time I want to show layout B, then layout C etc.
I read somewhere that rather than including layouts in main layout and then hiding/unhiding I should use viewstub and inflate.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ViewStub
android:id="#+id/layout_stub"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
My issue now is when I inflate the first layout it works fine but the next time when I try to inflate the second layout I get the stub null.
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
stub.setLayoutResource(layoutId);
View inflated = stub.inflate();
My understanding is the Viewstub is a container in which the layouts are being loaded, if so
why am I not getting the ViewStub when trying to load the second layout?
(So this means when I inflated the first layout (A) the layout in which the ViewStub was placed was removed completely?)
I'm looking for any pointers to implementing my usecase with Viewstub or alternatives.
A ViewStub is a placeholder, which is replaced by an inflated layout as soon as ViewStub.inflate() is called. It doesn't make sense to call inflate a second time, as the ViewStub will no longer be in the hierarchy. Instead, you should obtain a reference to your LinearLayout, remove its views, and add your second layout as a child.
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
stub.setLayoutResource(layoutId);
stub.inflate(); // inflate 1st layout
ll.removeAllViews(); // remove previous view, add 2nd layout
ll.addView(LayoutInflater.from(context).inflate(secondLayoutId, ll, false));
Yes, I think you can easily replace it with another ViewStub and lazily inflate your new layout in this way:
for Java
public static ViewStub deflate(View view) {
ViewParent viewParent = view.getParent();
if (viewParent != null && viewParent instanceof ViewGroup) {
int index = ((ViewGroup) viewParent).indexOfChild(view);
int inflatedId = view.getId();
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
((ViewGroup) viewParent).removeView(view);
Context context = ((ViewGroup) viewParent).getContext();
ViewStub viewStub = new ViewStub(context);
viewStub.setInflatedId(inflatedId);
viewStub.setLayoutParams(layoutParams);
((ViewGroup) viewParent).addView(viewStub, index);
return viewStub;
} else {
throw new IllegalStateException("Inflated View has not a parent");
}
}
or Kotlin with an extension
fun ViewStub.deflate(view: View): ViewStub {
val viewParent = view.parent
if (viewParent != null && viewParent is ViewGroup) {
val index = viewParent.indexOfChild(view)
viewParent.removeView(view)
val viewStub = ViewStub(context).apply {
inflatedId = this#deflate.inflatedId
layoutParams = this#deflate.layoutParams
}
viewParent.addView(viewStub, index)
return viewStub
} else {
throw IllegalStateException("Inflated View has not a parent")
}
}
Check out the gist
I have an Activity and its layout. Now I need to add a LinearLayout from another layout, menu_layout.xml.
LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.menu_layout, null);
After this, findViewById() returns null. Is there is any solution for that?
Note: I can't put both XML in one place, and using <include> also is not working.
Explanation
When you inflate a layout, the layout is not in the UI yet, meaning the user will not be able to see it until it's been added. To do this, you must get a hold of a ViewGroup (LinearLayout,RelativeLayout,etc) and add the inflated View to it. Once they're added, you can work on them as you would with any other views including the findViewById method, adding listeners, changing properties, etc
Code
//Inside onCreate for example
setContentView(R.layout.main); //Sets the content of your activity
View otherLayout = LayoutInflater.from(this).inflate(R.layout.other,null);
//You can access them here, before adding `otherLayout` to your activity
TextView example = (TextView) otherLayout.findViewById(R.id.exampleTextView);
//This container needs to be inside main.xml
LinearLayout container = (LinearLayout)findViewById(R.id.container);
//Add the inflated view to the container
container.addView(otherLayout);
//Or access them once they're added
TextView example2 = (TextView) findViewById(R.id.exampleTextView);
//For example, adding a listener to the new layout
otherLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
}
});
Assuming
main.xml contains a LinearLayout with the id container
other.xml is a layout file in your project
other.xml contains a TextView with the id exampleTextView
Try using
layoutObject.findViewById();
I want to inflate a childView of ExpandableChildView component.
Code:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
LinearLayout linearOpt = themesOptions.get(childPosition);
if(v == null) {
v = mInflater.inflate(R.layout.itemrow, null);
}
LinearLayout ll = (LinearLayout) v.findViewById(R.id.root);
ll.addView(linearOpt);
return v;
}
Where linearOpt is a vector that contains a lot of LinearLayout objects that I have instantiated.
private void prepareThemes(){
View v = mInflater.inflate(R.layout.widget_configure, null);
LinearLayout theme = (LinearLayout) v.findViewById(R.id.themeLayout);
LinearLayout color = (LinearLayout) v.findViewById(R.id.colorLayout);
LinearLayout trans = (LinearLayout) v.findViewById(R.id.transpLayout);
themesOptions.add(theme);
themesOptions.add(color);
themesOptions.add(trans);
}
This is R.layout.itemrow xml:
But I received this error:
07-18 10:48:49.740:
ERROR/AndroidRuntime(2738):
java.lang.IllegalStateException: The
specified child already has a parent.
You must call removeView() on the
child's parent first.
How I can resolve this issue?
I think the problem is caused by the manner you prepare the layouts in prepareThemes().
You didn't mention, but I assume your 'layout/widget_configure.xml' defines a structure like this:?
<LinearLayout android:id="#+id/root">
<LinearLayout android:id="#+id/themeLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="#+id/colorLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="#+id/transpLayout> <!-- ... --> </LinearLayout>
</LinearLayout>
Then you inflate this in prepareThemes() to get the 3 sub layouts. But at this moment they are already registered as children of the surrounding layout (which I called "root"). As the error implies a view instance can be added to just one parent.
You could store each LinearLayout in its own xml file and then inflate 3 times. Those stored layouts then could be add exactly once to a child.
I'm not sure what you want to do, but I would guess, it would be better - instead of preparing - to just have 3 different layouts that include the part from layout.itemrow and then just make a switch case in getChildView() and inflate the needed layout on the fly. Because even when you store a not bind layout in prepareThemes: As soon as you have more then one group you would get the same error when adding a prestored layout to the child of the snd group.
I hope this helps.
I have a layout defined in XML. It contains also:
<RelativeLayout
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
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
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
But none of them worked fine.
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:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
You inflate an XML resource. See the LayoutInflater doc .
If your layout is in a mylayout.xml, you would do something like:
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);
Though late answer,
but would like to add that one way to get this
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
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:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
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
View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
Try this code :
If you just want to inflate your layout :
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);
If you want to inflate your layout in container(parent layout) :
LinearLayout parent = findViewById(R.id.container); //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false);
RelativeLayout item = view.findViewById(R.id.item); //initialize layout & By this you can also perform any event.
parent.addView(view); //adding your inflated layout in parent layout.
layout inflation
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);
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 :
LayoutInflater i;
Context x; //Assuming here that x is a valid context, not null
i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);
(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.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/custom_button">
</Button>
On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);
Hope this solution works for you.!
If you want to add a single view multiple time then you have to use
layoutInflaterForButton = getActivity().getLayoutInflater();
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
btnContainer.addView(btnView);
}
If you do like
layoutInflaterForButton = getActivity().getLayoutInflater();
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
and
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
btnContainer.addView(btnView);
}
then it will throw exception of all ready added view.
If you are you trying to attach a child view to the RelativeLayout? you can do by following
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);
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.
// set up Alert Dialog box
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// inflate other xml where WebView is
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.your_webview_layout, null);
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id);
// more code...
.... later on after I loaded my WebView ....
// first, remove the parent of WebView from it's old parent so can be assigned a new one.
ViewGroup vg = (ViewGroup) webView.getParent();
vg.removeAllViews();
// put WebView in Dialog box
alert.setView(webView);
alert.show();
With Kotlin, you can use:
val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
[your_main_layout].apply {
//..
addView(content)
}
When add a layout to an Activity in Kotlin, see these steps:
Add just in Activity - One layout as a parent
Xml file of new
Layout Give the value of R.
val parent: LinearLayout =findViewById(R.id.stars)
val view =
LayoutInflater.from(applicationContext).inflate(R.layout.another, parent,false)
Where parent is not necessary, can be null But warning message will be appear
view.findViewById<ImageView>(R.id.ivTimer).setImageResource(R.drawable.t2)
Any view must be set value in this way, finally add
parent.apply { addView(view)}
I had used below snippet of code for this and it worked for me.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);