Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a textview in different layout xml from the main activity

Tags:

android

I've created a new .xml file in my layout folder called log.xml. It only contains one TextView.

Is it possible to set the text on the textview located in the log.xml from my main activity? Or can it only be set when in an activity which uses the log.xml as view? Hope you get what i mean here, otherwise ill elaborate.

Thanks

like image 846
Johan Avatar asked Jun 23 '12 23:06

Johan


1 Answers

If you don't set the xml you are talking about on "setContentView()" you can always get it with layout inflater. You'll have to add the tv to the current layout using addView() though.

LayoutInflater inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View vi = inflater.inflate(R.layout.log, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.tv); //get a reference to the textview on the log.xml file. 
like image 91
Nuno Gonçalves Avatar answered Nov 14 '22 23:11

Nuno Gonçalves