1. Download our Official Android App: Forums for Android!

Apps Modifying hello world in order to learn

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

  1. ketanco

    ketanco Newbie
    Thread Starter
    Rank:
     #615
    Points:
    16
    Posts:
    30
    Joined:
    Apr 13, 2011

    Apr 13, 2011
    30
    3
    16
    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
    Rank:
    None
    Points:
    16
    Posts:
    11
    Joined:
    Aug 6, 2011

    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
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    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
    Rank:
    None
    Points:
    53
    Posts:
    313
    Joined:
    Aug 12, 2010

    Aug 12, 2010
    313
    51
    53
    Norway
    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
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    Woops, you are correct. I apologize, I didn't read through the code, I just glanced at it.
     

Share This Page

Loading...