Add custom layout (from file) to another layout - android

Content view is a LinearLayout. We'll call it llOne and we'll say it's in the file llOne.xml.
The view I'm trying to add is also a LinearLayout but they're in separate files. We'll call it llTwo and we'll say it's in the file llTwo.xml.
setContentView(R.layout.llOne);
LinearLayout llOne = (LinearLayout) findViewById(R.id.llOne);
LinearLayout llTwo = (LinearLayout) findViewById(R.id.llTwo);
llOne.addView(llTwo); //NullPointerException

you need to inflate the second layout, as setContentView only inflate your llOne
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View otherView = inflater.inflate(R.layout.yourSecondLayoutFileName, null);
and then
LinearLayout llTwo = (LinearLayout) otherView .findViewById(R.id.llTwo);
llOne.addView(llTwo);

Related

Load layout from another XML

I want to change the content of a "RelativeLayout", the problem is that the content of that layout is in another xml.
setContentView(R.layout.activity_fullscreen);
//from actual xml
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout);
//from another xml
RelativeLayout rL2= (RelativeLayout) findViewById(R.id.cocinaL);
rL1.removeAllViews();
rL1.addView(rL2); //this fail because rL2 is null
Thank
you can't add another layout View without LayoutInflater
setContentView(R.layout.activity_fullscreen);
//your actule view
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout);
LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// your second xml
View view2 = inflater.inflate(R.layout.myview12, null);
// add second View
rL1.addView(view2 );
//from actual xml
RelativeLayout rL1= (RelativeLayout) findViewById(R.id.principalLayout);
//from another xml
View child = getLayoutInflater().inflate(R.layout.child, null);
RelativeLayout rL2 = (RelativeLayout) child.findViewById(R.id.cocinaL);
rL1.removeAllViews();
rL1.addView(rL2);

Android: Load layout from xml to new layout

I want to load layout dafined in xml to newly created LinearLayout object. Is that possible?
LinearLayout new_layout = new LinearLayout(context);
new_layout.load(R.layout.my_layout); // is something like this possible?
What you're trying to achieve is usually done this way
LayoutInflater inflater = LayoutInflater.from(context);
LinearLayout newLayout = (LinearLayout) inflater.inflate(R.layout.my_layout, null);

Inflate a linear layout from R.id and not R.layout

I'm going nuts here. I have a layout for a fragment. Inside I have among other things a LinearLayout with an id, say for example myLinearLayout. Basically, I want to do the following:
LinearLayout newLayout = new LinearLayout(this);
LinearLayout layoutCopy = inflatefrom(R.id.myLinearLayout);
newLayout.addView(layoutCopy);
How can I do this?
you can not inflate a Layout from its R.id, but you can inflate the Layout through R.layout and use the view returned to retrieve the R.id you need.
For instance
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.mylayout, null);
LinearLayout layoutCopy = (LinearLayout)view.findViewById(R.id.myLinearLayout);
check for typo
Try this one:
View.inflate(Context, R.id, ViewGroup)
Actually blackbelt, that's pretty much how I solved it, but I still got error when adding the copied layout to the new layout. The reason was because the layout I wanted to copy still had a parent. Here's a complete sollution (layoutRoot is an xml layout that lives in res/layout and myLinearLayout is a LinearLayout inside the file layoutRoot. layoutToPlaceCopiedLayout is a LinearLayout inside the layout where I want to put the copied layout).
LinearLayout newLayout = (LinearLayout)findViewById(R.id.layoutToPlaceCopiedLayout)
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout layoutRootCopy = (LinearLayout)inflatefrom(R.layout.layoutRoot);
LinearLayout layoutCopy = (LinearLayout)layoutRootCopy.findViewById(R.id.myLinearLayout);
ViewGroup parent = (ViewGroup)layoutCopy.getParent();
int index = parent.indexOfChild(layoutCopy);
parent.removeView(layoutCopy);
newLayout.addView(layoutCopy, index);

How to get a reference to an XML file from FrameLayout

I need to get reference to separate XML file which is FrameLayout but I can't figure out how to do it, this code doesn't work:
FrameLayout desktopFrameLayout = (FrameLayout) findViewById(R.id.desktopsFramelayout);
desktopFrameLayout.setDrawingCacheEnabled(true);
desktopFrameLayout.buildDrawingCache();
Bitmap bitmap = desktopFrameLayout.getDrawingCache();
For that you have to use inflate view.
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.mylayout, null);
FrameLayout item = (FrameLayout ) view.findViewById(R.id.desktopsFramelayout);

dynamically cloning a LinearLayout in android?

Say I have a LinearLayout with some elements in it as an .xml file.
In Java, I need to somehow "clone" it a few times into an array, edit some of its children, and then loop through the array, adding each LinearLayout to my main view.
What do you think would be the correct way to "clone" this layout from an xml file into an array element in java?
Thanks!
LayoutInflater vi = (LayoutInflater) myContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.yourLayoutId, null);
you can do some thing like this to inflate the view, and then modify the element iside the view using the findViewById method. Hope this will help
Something like this:
....
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = null;
for(....) {
layout = (LinearLayout) inflater.inflate(R.layout.YOUR_LAYOUT_ID, null);
someList.add(layout);
}
.....
Try getting the layout in a variable:
for (int c=0; c < count; c++)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.yourmainlayout);
// do something with layout
// assign layout to a variable or add it on another layout
}

Categories

Resources