Demo Explanations

Demo Explanations

Features

  • Run Java code when user touches a button
  • Output a string in the user interface
  • Convert integer to string

Run Java code when user touches a button

  • Create a Button element with onClick attribute
    <Button android:onClick="doIt" />
    
  • Declare corresponding method in Java class
    public void doIt(View button) {
      // This code executes when user touches button
    }
    
  • Method must have same name, be public void and take a View as argument

Output a string in the user interface

  • Add unique identifier to the xml element
    <TextView android:id="@+id/my_unique_id" />
    
  • Declare a class member of the same type as the xml element
    TextView _output;
    
  • Assign the class member in onCreate
    _output = (TextView)findViewById(R.id.my_unique_id);
    
  • Change the contents of the element with setText
    _output.setText("This is something else I want to show");
    

Convert integer to string

  • Different ways to convert an integer to a string
    int source = 123;
    String example1 = String.format("%d", source);
    String example2 = Integer.toString(source);
    String example3 = String.valueOf(source);
    
  • Convert from a string to an integer
    String source = "123";
    int example1 = Integer.parseInt(source);
    int example2 = Integer.valueOf(source);
    
  • Which way you choose does not matter