Adding TextView object dynamically to a Fragment
android, android-fragments, dynamic, textview
Solution
I'm new to android development, but i think you should return `l` instead of `v` in your onCreateView or you should add TextViews to `v`.
Problem
I have been able to work with TextView elements in a Fragment when they are statically defined in my XML. But when I try to create them dynamically/programatically, I don't see anything displayed. I've created a simple example of the problem I'm having. This one works, I see the screen updated. ``` @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.hello_world, container, false); TextView tv = (TextView)v.findViewById(R.id.text); tv.setText("Fragment #" + mNum); tv.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb)); return v; } ``` This one doesn't work. The XML file diagtextvertical.xml is simply this: ``` <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> ``` The code is as follows. ``` @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //good View v = inflater.inflate(R.layout.diaglist, container, false); View v = inflater.inflate(R.layout.diagverticaltext, container, false); mycontainer = container; LinearLayout l = (LinearLayout)inflater.inflate(R.layout.diagverticaltext, container, false); TextView tv = new TextView(container.getContext()); tv.setText("Testing..."); l.addView(tv); tv = new TextView(container.getContext()); tv.setText("Testing1..."); l.addView(tv); tv = new TextView(container.getContext()); tv.setText("Testing2..."); l.addView(tv); return v; } ``` I have tried using getActivity() in place of container.getContext(), but no luck. There is something I'm not clear on on how to map the layout to the actual view instance - something different than doing this in an Activity. There is a second part to my question. My actual goal is to update the view asyncronously based on polled data, so one question I have is how to get my hands on the ViewGroup and the inflater when I'm calling in on my own callback.