Simple Text output


Last Updated:

  1. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    Hiyer folks!

    I'm just dipping my toes and to get started i would like to re-create a program from my C=64 days: a program that goes through a loop printing out the loop counter.
    e.g.
    for n =1 to 10
    print "n is now ",n
    next n

    n is now 1
    n is now 2
    ...

    How can i do this?
    I keep getting errors about strings, but im not sure where/how to define them.

    I'd appreciate any help,
    Gir
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    Your best bet is to dynamically create a series of TextView objects and add them to a LinearLayout, each containing the text for that iteration of the loop.

    I am not sure how to give you an example without giving you the entire program... lol If you want me to I will (It is actually VERY simple and straightforward), but I would rather not ruin your learning experience unless I am asked to...
     
    GIR likes this.
  3. id0001

    id0001 Well-Known Member

    Joined:
    Apr 20, 2010
    Messages:
    142
    Likes Received:
    23
    could you please post the relevant parts of your code so we can have a look?
    Also if you have trouble defining strings, make sure that 'String' starts with a capital 'S' because String is a class.
     
  4. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    Thanks for your reply.
    I found a tutorial at Android, Part III: Dynamic Layouts - Android Development | Dream.In.Code

    and made this:
    Code (Text):
    1.  
    2. package com.dic_tut3;
    3.  
    4. import android.app.Activity;
    5. import android.os.Bundle;
    6. import android.widget.ScrollView;
    7. import android.widget.LinearLayout;
    8. import android.widget.Button;
    9. import android.widget.TextView;
    10. import android.widget.EditText;
    11. import android.widget.CheckBox;
    12.  
    13. public class dic_tut3 extends Activity {
    14.     /** Called when the activity is first created. */
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         ScrollView sv = new ScrollView(this);
    19.         LinearLayout ll = new LinearLayout(this);
    20.         ll.setOrientation(LinearLayout.VERTICAL);
    21.         sv.addView(ll);
    22.         TextView tv = new TextView(this);
    23.         tv.setText("Looping play");
    24.         ll.addView(tv);
    25.         for(int i = 0; i <10; i++){
    26.             EditText et = new EditText(this);
    27.             et.setText("loop is now:"+i);
    28.             ll.addView(et);
    29.         }
    30.         this.setContentView(sv);
    31.     }
    32. }
     
    Waltzy likes this.
  5. Waltzy

    Waltzy Member

    Joined:
    Aug 31, 2010
    Messages:
    11
    Likes Received:
    0
    an so begins The tedium of Java Layout managers, I had enough f this with Swing!
     
  6. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    It's a good idea to start with best practices, including defining your layouts in XML, even if they're as simple as a ScrollView wrapping a LinearLayout. Really, you could already have a single TextView defined and update its contents as the loop progresses. You can use the getText() method to return what is already there and concatenate the next portion of your loop. In theory, this allows you to see the looping as it happens, but it will happen too fast to see.
     
  7. GIR

    GIR Well-Known Member This Topic's Starter

    Joined:
    Feb 14, 2010
    Messages:
    96
    Likes Received:
    10
    IanGClifton,
    I agree with your advice concerning best practices.
    This was merely an advanced 'hello world' for me.
    My programming background is Python, and assembly for PIC chips.
    To be honest I've always avoided Java in most forms like the plague previously, so I'll keep your words of guidance; monolithic code can be a pain: Android is not dimdoze!
     

Share This Page

Loading...