Wednesday, 29 October 2014

How to dynamically change the text of an android widget

This works for any android widget that support text labels, like TextViews and buttons


  // this code is assumed to run inside an activity class or view
  // in short, this must have the findViewById method
  // also, you need the text to be in your string resource file (usually strings.xml)

  TextView theLabelOnTheScreen = (TextView)this.findViewById(R.id.myLabel);
  theLabelOnTheScreen.setText(R.string.the_id_of_the_string_to_be_used);