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