• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Simple Text output

GIR

Member
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
 
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...
 
  • Like
Reactions: GIR
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.
 
Thanks for your reply.
I found a tutorial at Android, Part III: Dynamic Layouts - Android Development | Dream.In.Code

and made this:
Code:
package com.dic_tut3;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;
import android.widget.LinearLayout;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.CheckBox;

public class dic_tut3 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView sv = new ScrollView(this);
        LinearLayout ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        sv.addView(ll);
        TextView tv = new TextView(this);
        tv.setText("Looping play");
        ll.addView(tv);
        for(int i = 0; i <10; i++){
            EditText et = new EditText(this);
            et.setText("loop is now:"+i);
            ll.addView(et);
        }
        this.setContentView(sv);
    }
}
 
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.
 
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!
 
Back
Top Bottom