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

Apps Simple Text output

Discussion in 'Android Development' started by GIR, Aug 29, 2010.

  1. GIR

    GIR Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    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 Android Expert
    Rank:
    None
    Points:
    163
    Posts:
    1,930
    Joined:
    Jul 13, 2010

    Jul 13, 2010
    1,930
    457
    163
    Male
    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
    Rank:
    None
    Points:
    38
    Posts:
    142
    Joined:
    Apr 20, 2010

    Apr 20, 2010
    142
    23
    38
    Student.. also software developer
    Netherlands
    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
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    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 Newbie
    Rank:
    None
    Points:
    35
    Posts:
    11
    Joined:
    Aug 31, 2010

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

    IanGClifton Well-Known Member
    Rank:
    None
    Points:
    58
    Posts:
    111
    Joined:
    Feb 7, 2010

    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
    Thread Starter
    Rank:
    None
    Points:
    18
    Posts:
    96
    Joined:
    Feb 14, 2010

    Feb 14, 2010
    96
    10
    18
    Classified.
    UK
    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...