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);
Related
I'm using addContentView to add a child view to my main view like this:
TableLayout.LayoutParams tlp = new TableLayout.LayoutParams(
TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_NAME);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
addContentView(textView,tlp);
Rather than defining the view programmatically like I do above, can I define the view in an xml file and then add it via addContentView?
Thanks!
can I define the view in an xml file and then add it via
addContentView?
Yes, first inflate xml file using LayoutInflater which return a View. pass View to addContentView after setting layout LayoutParams
LayoutInflater inflater = getLayoutInflater();
View view;
view = inflater.inflate(R.layout.xml_file_name, null);
//.. add LayoutParams to view
// call addContentView
addContentView(view,tlp);
You can actually use something like
LayoutInflater.from(context).inflate(R.layout.your_xml_layout_file, this);
You can load a view from xml using LayoutInflater:
LayoutInflater.from(context).inflate(R.layout.file, rootView, false);
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);
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);
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
}
I want to add xml layout dynamically to scrollview in my application, but it is showing an error.
This is my code:
LinearLayout ll = (LinearLayout)findViewById(R.id.myContent1);
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vv = vi.inflate(R.layout.headerone, null);
ll.addView(vv);//, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().height));
View vv2 = vi.inflate(R.layout.headertwo, null);
ll.addView(vv2);//, new LinearLayout.LayoutParams(ll.getLayoutParams().width, ll.getLayoutParams().height));
View vv3 = vi.inflate(R.layout.headerone, null);
ll.addView(vv3);
"headerone.xml" and "headertwo.xml" are my two xml layout files.
Probably
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects.
But we would need to see the XML to be sure.