1. Are you ready for the Galaxy S20? Here is everything we know so far!

Apps Modifying hello world in order to learn

Discussion in 'Android Development' started by ketanco, Sep 22, 2011.

  1. ketanco

    ketanco Newbie
    Thread Starter

    I did the famous hello world tutorial

    Code (Text):
    1. package com.mysite.helloworld;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.widget.TextView;
    6.  
    7. public class MysitehelloworldActivity extends Activity {
    8.     /** Called when the activity is first created. */
    9.     @Override
    10.     public void onCreate(Bundle savedInstanceState) {
    11.         super.onCreate(savedInstanceState);
    12.         TextView tv = new TextView(this);
    13.         tv.setText("Hello, Android");
    14.         setContentView(tv);
    15.     }
    16. }

    and then it printed "Hello Android" with no problems on the virtual device.

    Now I want to print "Good Bye Android" also, in addition to, and below "Hello Android". Ho can I do it? I tried adding another line of tv.setText("Good Bye Android"); below the Hello Andorid line but this time it only displays Good bye Android. How can I make both displayed?
     

    Advertisement

  2. AccelArlene

    AccelArlene Newbie

    Katanco, in this case, the easiest thing to do would be to modify your text string to include the newline escape sequence like this:

    tv.setText("Hello, Android\nGood Bye Android");
     
  3. jonbonazza

    jonbonazza Android Expert

    You need to call the setContentView() method before performing any operations on its Views (that includes using the findViewById() method).
     
  4. miXer

    miXer Android Enthusiast

    This is true only if you are getting an id handler to a view defined in the xml, which ketanco is not doing.

    He is doing it right, and if you want to add Good Bye Android, AccelArlene has the right answer.
     
  5. jonbonazza

    jonbonazza Android Expert

    Woops, you are correct. I apologize, I didn't read through the code, I just glanced at it.
     
Loading...
Similar Threads - Modifying hello world
  1. Turtlegamin
    Replies:
    0
    Views:
    217
  2. cutedroid
    Replies:
    4
    Views:
    279
  3. FouinyBaby22
    Replies:
    1
    Views:
    209
  4. Sands207
    Replies:
    0
    Views:
    493
  5. Finn Le Moenic
    Replies:
    6
    Views:
    2,333
  6. pandorasky21
    Replies:
    4
    Views:
    7,182
  7. Werajen
    Replies:
    0
    Views:
    4,994
  8. Werajen
    Replies:
    1
    Views:
    2,189
  9. armoredknight2015
    Replies:
    5
    Views:
    477
  10. jestyr8
    Replies:
    5
    Views:
    975

Share This Page

Loading...