1

For example, say I have a simple coin flip program that display a single line of text. After running the program as a Java Project, the console would display "There are 473 heads and 527 tails." I don't mean displaying this in LogCat, but actually displaying it when using the application.

//Basic coin flip statistics program.
package com.company.practice;

import java.util.Random;

public class CoinFlip {
public static void main(String[] args){

    System.out.println("Toss a coin 1000 times");

    int heads = 0;
    int tails = 0;
    Random r = new Random();

    for(int flips = 0; flips < 1000; flips++){ 
        //Change the # after < to set amount of flips.
        int side = r.nextInt(2);
        if(side == 0) heads++;
        else tails++;
    }

    System.out.println("There are " + heads + " heads and " + tails + " tails.");
    }
}

How would I display the line of code at the bottom that says, "System.out.println(...)" as text in an Android Application's activity?

  • 1
    See this if you have never built an android app - http://developer.android.com/training/basics/firstapp/index.html – Varun Aug 23 '13 at 21:37
  • 1
    Do you have a activity design in xml with a text view element. – bobthemac Aug 23 '13 at 21:39
  • http://stackoverflow.com/questions/2220547/why-doesnt-system-out-println-work-in-android – Sotirios Delimanolis Aug 23 '13 at 21:39
  • 1
    You need to make a design with a text view and assign the string you want to display to that element in your design files if you are unsure look at the first few tutorials in the android developers – bobthemac Aug 23 '13 at 21:42
  • 1
    @Varun: I have built some basic android applications before, but I have never built one by starting in Java like I am currently. I looked through the android developer site before and couldn't find this answer there. – WoodburyRaider Aug 23 '13 at 21:42

1 Answers1

2

You'd display it in the UI attached to the Activity.

An Android app is not constructed the same as a Java program. There's no "main" class. In fact, you usually don't declare an Application subclass. Instead, you construct a loose web of Activities that interact with each other using Intents. Each Activity has an associated layout that contains Views. When the Activity starts up, you load its main layout, then you store the View objects in variables in the Activity. When you want to modify the contents of a View, you call a method (often setText()) to change the contents.

Layouts and Views can be defined in an XML file, or set up programmatically.

Those of you who have written Java programs before will have to step back some, and approach app construction from a somewhat different point of view. I strongly encourage you to browse through the documentation, particularly the training class, and look at the samples.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
  • 1
    Thank you all for responding so quickly. I'm fairly certain I understand what I have to do now, and I'll be sure to check out the documentation if I need further assistance. Thanks again. – WoodburyRaider Aug 23 '13 at 21:49