Search code examples
javaandroidtextviewscrollview

How to add Strings to a TextView / ScrollView?


I am quiet new to android developing sorry!

I want to add doctor values as Strings (first name, last name, and some others later) as one entry (I was trying to do so with Objects) to my Scroll View via a button. I know that I need a Layout and a Text View to do so but my Virtual Machine crushes. As far as I understand I have to put my Text View (with the Strings) in the Layout and the Layout in the Scroll View (am I wrong?).

I was trying several things from different websites which all provide similar solutions but nothing worked so far. I am using Android 6.0 with android studio and designed my UI with the Design View and adapted some code in the XML files.

public void addDoctor(View view) {

    setContentView(R.layout.activity_main);
    // standard java class Doctor
    Doctor doc = new Doctor("Foo", "Boo");

    // this is the ScrollView I generated with the design
    ScrollView sv = (ScrollView) this.findViewById(R.id.ScrollViewDoctor);
    TextView tv = new TextView(this);
    // trying to fix the parent problem here
    if(tv.getParent() != null) {
        ((ViewGroup)tv.getParent()).removeView(tv); // <- fix
    }
    //this should be "Foo"
    tv.setText(doc.getDocName());

    // this is the layout I generated with the design "linlay"
    LinearLayout ll = (LinearLayout) this.findViewById(R.id.linlay);
    sv.addView(ll);

    //only one child object! -> in the error log
    ll.addView(tv);
    setContentView(view);
}

I expected the Strings of the object to appear in the Scroll View but the error log says that "ScrollView can host only one direct child" which I was trying to fix with the if statement but it does not seem to affect my code.

Can you please help me with this. Do I miss something?

Thank you!


Solution

  • As far as I understand I have to put my Text View (with the Strings) in the Layout and the Layout in the Scroll View (am I wrong?).

    You've right.

    <ScrollView>
       <LinearLayout>
         <TextView></TextView>
       </LinearLayout>
    </ScrollView>
    

    How to Add a doctor in the right way

    error log says that "ScrollView can host only one direct child"

    You're getting the error because you added already the linear layout inside the <ScrollView>, so you don't have to call, sv.addView(ll); because it's already inside (And you cannot add multiple layout in standard ScrollView)

    So you're reference to ScrollView it's useless.

    if you're xml is like this:

    <ScrollView>
        <LinearLayout>
    
        </LinearLayout>
    </ScrollView>
    

    You can achieve your result with this:

    public class MyActivityDoctors extends Activity {
    
        ScrollView sv;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            sv =  (ScrollView) this.findViewById(R.id.ScrollViewDoctor);
        }
    
        public void addDoctor(Doctor doctor)
        {
            //Linear Layout inside your ScrollView
            LinearLayout ll = (LinearLayout) this.findViewById(R.id.linlay);
    
            //Create a new TextView with doctor data
            TextView tv = new TextView(this);
            tv.setText(doctor.getDocName());
    
            //Adding textView to LinearLayout
            ll.addView(tv);
        }
    
    }