Change TextView text in code

by kaloer » Mon, 13 Apr 2009 22:16:04 GMT


Sponsored Links
 Hi,

I have made a layout in Xml, which includes a TextView. The TextView
should show some text that is found in a database. Therefore, i cannot
find the text by using the @string/something. How can i define the
text from my code, without creating the whole layout in code?

My TextView has the id  "currentRankLabel".

I think i shall do something like this:

View currentRankText = this.findViewById(R.id.currentRankLabel);
currentRankText.setText("some text");

Best Regards,
Kaloer
--~--~---------~--~----~------------~-------~--~----~



Change TextView text in code

by Arnaud Weber » Mon, 13 Apr 2009 22:22:38 GMT


 That's almost it. but you need to cast your View as a TextView :
TextView currentRankText =
(TextView)  this.findViewById(R.id.currentRankLabel);
currentRankText.setText("some text");

2009/4/13 kaloer <mkal...@gmail.com>


--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Change TextView text in code

by Mark Murphy » Mon, 13 Apr 2009 22:22:46 GMT


 > I have made a layout in Xml, which includes a TextView. The TextView

That should work fine, so long as you call findViewById() sometime after
setContentView() or otherwise inflating your layout.

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 2.0 Available!



--~--~---------~--~----~------------~-------~--~----~



Change TextView text in code

by kaloer » Mon, 13 Apr 2009 22:34:43 GMT


 Thank you! It works now :-)

//Kaloer
--~--~---------~--~----~------------~-------~--~----~