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)
Related
I have to set a layout inside other layout's child (linearlayout). To do this, I write this code on the layout's activity that I want to set into the root layout:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
**setContentView(R.layout.main);**
/**Define root layout's child where I want to set the layout*/
LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);
/**Inflate this layout and add it to the root layout*/
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View this_layout = inflater.inflate(R.layout.main, null);
inside_menu_view.addView(this_layout);
But I'm getting a NULLPOINTEREXCEPTION in this last line inside_menu_view.addView(this_layout);
UPDATE
-Added the setContentView() after super.onCreate
This line will return null because you have not yet called setContentView()
LinearLayout inside_menu_view = (LinearLayout)findViewById(R.id.activitycontent);
You need to call setContentView(R.layout.layout_with_activitycontent); first
From the Docs
Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).
Returns The view if found or null otherwise.
You have not yet processed an xml layout file with setContentView() or by using a LayoutInflater so it will return null causing a NPE when you try to call a method on it like addView().
Edit
I'm not sure why you are doing it that way but you can't. As in my link above, you can only use findViewById() to find a View inside the inflated layout. You can't use it to find a View inside some other layout which hasn't been inflated.
Put that layout file in setContentView() if that is the one you want to use.
Different approach
You probably want to use Fragments then to achieve this.
See the docs on how to use these
Or you can use <include> to include the "menu" layout that you want to include in all of the Activities then you can switch Activities when you need to show the inner part of your layout.
See here about re-using layouts
Example of <include>. You have some layout that you want to re-use, say main.xml, then in an Activity in which you want to re-use it you simply do something like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="30dp"
android:background="#drawable/blue">
<include layout="#layout/main"
android:id="#+id/main_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- other layouts -->
/<RelativeLayout>
yours will obviously be different but this is an example of one I have. Hope this helps.
Let's say your R.layout.main is a LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout" >
</LinearLayout>
Now in onCreate():
LinearLayout layout = (LinearLayout) findViewById(R.id.main_layout);
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View this_layout = inflater.inflate(R.layout.main_layout, layout, true);
And this_layout is automatically added to the activity's layout.
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 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 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