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?
Asked
Active
Viewed 271 times
0
-
have you included `content_main.xml` in `activity_main.xml`? – V-rund Puro-hit May 23 '17 at 10:29
-
Why are trying to show two different layouts in one activity ? And if you really want to show two layouts in one Activity then you have to include the second layout in your first one. – Passiondroid May 23 '17 at 10:30
-
If MainActivity uses activity_main.xml, where are you using content_main.xml ? – Mayura Devani May 23 '17 at 10:35
-
This is clean starter project, I added a TextView to content_main.xml because I read that's the way to do it. I'm trying to accomplish the simplest possible thing - reference the TextView and set some text. A lot of things changed since I last programmed Android and I'm a bit confused with two layouts. I'm not trying to do anything fancy here. – jarosik May 23 '17 at 10:47
2 Answers
0
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"/>

Antonio Ganci
- 515
- 5
- 16
-
According to follwoing answer, all insludes should already be there: https://stackoverflow.com/questions/32880722/what-is-the-role-of-content-main-xml-in-android-studio-1-4 – jarosik May 23 '17 at 10:51
-
If you include like I wrote the views inside the content_main are all in the same tree view. Have you assigned a unique id to the textview? – Antonio Ganci May 23 '17 at 12:00
-
0
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)

Tushar Saha
- 1,978
- 24
- 29