I have two layout.xml like
viewOne.xml and viewTwo.xml
then
How to add viewOne.xml inside the viewTwo.xml programmatically like
viewone.addView(viewTwo);
but it's not working anyway to do this.
programmatically:
you can use LayoutInflater
View viewTwo = LayoutInflater.from(context).inflate(R.layout.viewTwo);
viewOne.addView(viewTwo);
View viewTwo = LayoutInflater.from(context).inflate(R.layout.view_two, viewOne);
inflate method need 2 or more parameters
http://developer.android.com/reference/android/view/LayoutInflater.html
Related
I've an activity.java file in which my setContentView(R.layout.x); Now,I've an y.xml in which I've an Linear Layout,I've to attach an onclick() method to my view.
Attaching onclick() has to be in my activity.java file, How do I include y.xml.
I tried this,
1. layout = (LinearLayout) findViewById(R.layout.y);
eView = (EditText)layout. findViewById(R.id.editview);
2. eView = (EditText)findViewById(R.id.editview);
but both gives my null pointer exception, How do I include my editText
Update
final LayoutInflater lyInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
showLinearLayout = (LinearLayout) lyInflater.inflate(R.layout.y, null);
showView = (EditView) showLinearLayout.findViewById(R.id.edittext);
You can use inflation as shown below:
final LayoutInflater lyInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout yLayout = (LinearLayout) lyInflater .inflate(
R.layout.y, null);
eView = (EditText)yLayout.findViewById(R.id.editview);
So you won't get exception anymore. Hope it helps.
LayoutInflater is used to instantiate layout XML file into its corresponding View objects.in other words, it takes as input a XML file and builds the View objects from it.
in your scenario, you have to use LayoutInflater. read this article.
Ram.
If you want to include y xml file into your x xml file then follw this steps.
I am assuming that you want to include Linear Layout into your Activity on click of onclick() method of the button or whatever, then add the Linear Layout into your x xml file and add the android:visibility="gone" so at begin you can not show the linearlayout.
<LinearLayout
android:id="#+id/history_value_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" > <<<<<<<<<<< HERE
----------------------------
-----------------------------
</LinearLayout>
Now, From the java class make it visible when needed, in your case into onclick method.
Like...
linear.setVisibility(View.VISIBLE); // linear is the object of your Linearlayout
If any prob then ask me.
Good Luck.
If I understand the question correctly, your Activity uses x.xml, and you also want to include another layout that is defined in y.xml.
You can do so using the <merge> or <include> tags, as described in the documentation.
Alternately, you can use a ViewStub to conditionally inflate another layout in a given place in a layout. For example, you can include a ViewStub tag in x.xml, and inflate y.xml in the same spot in the view hierarchy. Then, you may attach any click listeners you need (by using findViewById()).
You can use addView method of ViewGroup.
addView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
I'm trying to create something like editable UITableView on iPhone.
I added a button to add new items at the end of the list which would be the equivalent of the "+" button on iphone, this takes user to a list of items to add. My problem is that when they select one I don't know how to add it to the list.
<LinearLayout
android:key="application_preferences"
android:title="Applications">
</LinearLayout>
How can I append a view inside the LinearLayout programatically?
You can use addView to add a child view to a ViewGroup, but also consider just using ListView instead of a LinearLayout.
Your question is not very clear but if you try to add a view to the linear layout programmatically just use Layout Inflater
LayoutInflater localLayoutInflater =(LayoutInflater)getBaseContext().getSystemService("layout_inflater");
View myView = localLayoutInflater.inflate(R.layout.custom_layout, null);
final LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayoutID);
ll.addView(myView);
I'm going to create a new RelativeLayout with some Text and Image Views programatically and then add it to a parent layout. The question: is it possible to define that layout in XML and then just add it to the parent programatically? Would be a faster way..
Use inflation:
final LayoutInflater lyInflaterForPanel = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout lLayoutPanel = (LinearLayout) lyInflaterForPanel.inflate(
R.layout.my_row, null);
This has worked for me.
Yes, using LayoutInflater you will inflate your XML then add it to the parent..
check, What does LayoutInflater in Android do?
Use LayoutInflater class for that. It will covert your layout into a view and add it to parent.
I have created some xml layouts. Now I am creating a custom layout (in my java file) with some elements and want to add the previously created layout(xml layouts). How can I do that?
You can use LayoutInflater to inlate layouts. And add inflated view to your custom view by addView() method
For ex:
LayoutInflater mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewTobeLoaded = mInflater.inflate(R.layout.ilan_list_item, null);
yourView.addView(viewTobeLoaded);
Hope helps
Use <include> tag in your new layout. See docs here.
I have a pretty complex layout defined in xml file , now I want to add this layout as a view using addView or something else.
As layout is pretty much complex , its pretty cumbersome process to write the code for layout and then add it as a view. So is there anyway to load layout resource and add it as a view.
I want to add the layout into WebView
Use
LayoutInflater factory = LayoutInflater.from(this);
View myView = factory.inflate(R.layout.my_layout_id, null);
then use addView(myView)
You can also reduce this to one line of code;
View view = View.inflate(getActivity, R.layout.my_layout,null);
then add to your view.
If your view is a ViewGroup (layout)
You can use InflaterService.inflate(id, ViewGroup) and set the ViewGroup, it will set the current child(s) with the content of your xml.