TextViews in Android

TextView is a text field, usually user editable. You can read and write TextView values from code. You often have to cast between CharSequence, String, int and other types.

Prequisites: Android Hello World Explained

Read TextView

final TextView q = (TextView) findViewById(R.id.q);
String s = q.getText().toString();

Write TextView

out.setText("Foo bar");

Numbers have to be cast to string.

int x = 2;

Area Calculator with TextView

private void calculate() {
      final TextView q = (TextView) findViewById(R.id.q);
      final TextView out = (TextView) findViewById(R.id.out);
      String s = q.getText().toString();
      int x=0;
      try {
            x = Integer.parseInt(s);
      } catch (NumberFormatException e) { }
Posted in Uncategorized | Tagged , , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *



You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Recent Posts

  • Picks

  • Boxing Clock for AndroidOcton8 Diving T-ShirtsShaking Tower Panda Android GameLearn Chinese with Android
  • Student projects