add an existing XML layout programatically - android

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.

Related

FindViewById from other xml layout, Android

I created a TextField in content_main.xml, now I can't reference it from MainActivity using findViewById, because MainActivity uses activity_main.xml.
What is the recommended way to fix this issue?
I think you didn't include content_main.xml in activity_main.xml.
The include directive is explained here
A sample on include directive:
<include layout="#layout/content_main"/>
If you want to dynamically add layout.
inflate the layout
LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.id, null);
TextView textView = (TextView)view.findViewById(R.id.text_ciew_id);
and then add the view in parent layout
parent.addView(view)

How to add LinearLayout in other LinearLayout in AndroidStudio

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

Android including an xml layout to activity

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));

How to add child layouts in a Custom built layout in android

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.

How to add layout from xml to Extended FrameLayout Android?

I have a custom class called NoteView that extends FrameLayout. Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire hierarchy (with some other overlay views which is why I needed framelayout) to the NoteView.
The problem is I can't figure out how to inflate the XML and add it to the NoteView from within the NoteView Class. I can add it after initialization is complete, but I want to be able to be able to inflate that hierarchy and add it automatically when my NoteView is either instantiated or inflated from XML.
How can I populate my extended FrameLayout with a layout from XML by adding it from within the NoteView class itself?
Try LayoutInflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myView = (LinearLayout) inflater.inflate(R.layout.my_layout, null);
frameLayout.addChild(myView);
Have a look at the documentation for LayoutInflater here: http://d.android.com/reference/android/view/LayoutInflater.html

Categories

Resources