user3289688
user3289688

Reputation: 11

How to add a new LinearLayout into an existing one?

Hello im a beginner in Android development. I have made an LinearLayout to show the different Informations in a goodlooking way. I want to have a Screen with a ScrollView where I can add as many LinearLayouts I want. But the Problem is that th Button event does not add any LinearLayouts...

This is my activity_layouttest.xml:


RelativeLayout

-----Button @+id/spawnbutton

-----LinearLayout

-----------ScrollView @+id/scrollView1

--------------------LinearLayout: spawnLayout


This is my Code:


public class LayoutTest extends Activity {


    // declaring Widgets
LinearLayout SpawnLayout;
Button Spawnbtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_layouttest);

    Spawnbtn = (Button) findViewById(R.id.spawnbutton);
    SpawnLayout = new LinearLayout(readtest.this);
             // set On Click Listener to a Button "Spawnbtn which schould add the new
             // Layout to the existion one

    Spawnbtn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            dospawn();
        }
    });
}


     // method to create/add new linearLayout


public void dospawn() {



    // create custom Layout components
    String LEHRER = "Lehrer";
    String STUNDEN = "Stunden";
    String KLASSE = "Klasse";
    String ORT = "Ort";
    String INFO = "Info";
    String FACH = "Fach";


            //create new objects
    LinearLayout linlay = new LinearLayout(CONTEXT);
    TextView tvLehrer = new TextView(CONTEXT);
    TextView tvStunden = new TextView(CONTEXT);
    TextView tvKlasse = new TextView(CONTEXT);
    TextView tvOrt = new TextView(CONTEXT);
    TextView tvInfo = new TextView(CONTEXT);
    TextView tvFach = new TextView(CONTEXT);


            //defining each one including the child - parent Layout "linlay"


    linlay.setOrientation(LinearLayout.VERTICAL);
    linlay.setPadding(20, 10, 20, 10);
    linlay.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    Resources res = CONTEXT.getResources();
    tvLehrer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));



    tvLehrer.setBackgroundColor(res.getColor(R.color.transparent_light));
    tvLehrer.setGravity(Gravity.RIGHT);
    tvLehrer.setPadding(0, 0, 20, 0);
    tvLehrer.setText(LEHRER);
    tvLehrer.setTextColor(res.getColor(R.color.weiss));
    tvLehrer.setTextSize(20);



    tvStunden.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    tvStunden.setBackgroundColor(res.getColor(R.color.transparent_light));
    tvStunden.setGravity(Gravity.LEFT);
    tvStunden.setPadding(20, 0, 0, 0);
    tvStunden.setText(STUNDEN);
    tvStunden.setTextColor(res.getColor(R.color.weiss));



    tvKlasse.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    tvKlasse.setBackgroundColor(res.getColor(R.color.transparent_light));
    tvKlasse.setGravity(Gravity.LEFT);
    tvKlasse.setPadding(20, 0, 0, 0);
    tvKlasse.setText(KLASSE);
    tvKlasse.setTextColor(res.getColor(R.color.weiss));


    tvOrt.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    tvOrt.setBackgroundColor(res.getColor(R.color.transparent_light));
    tvOrt.setGravity(Gravity.LEFT);
    tvOrt.setPadding(20, 10, 0, 10);
    tvOrt.setText(ORT);
    tvOrt.setTextColor(res.getColor(R.color.weiss));



    tvInfo.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    tvInfo.setBackgroundColor(res.getColor(R.color.transparent_light));
    tvInfo.setGravity(Gravity.LEFT);
    tvInfo.setPadding(20, 0, 0, 10);
    tvInfo.setText(INFO);
    tvInfo.setTextColor(res.getColor(R.color.weiss));



    tvFach.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
            LayoutParams.WRAP_CONTENT));
    tvFach.setBackgroundColor(res.getColor(R.color.orange));
    tvFach.setGravity(Gravity.CENTER_HORIZONTAL);
    tvFach.setPadding(0, 10, 0, 10);
    tvFach.setText(FACH);
    tvFach.setTextColor(res.getColor(R.color.schwarz));



            //add the Components to LinearLayout linlay



    linlay.addView(tvLehrer);
    linlay.addView(tvStunden);
    linlay.addView(tvKlasse);
    linlay.addView(tvOrt);
    linlay.addView(tvInfo);
    linlay.addView(tvFach);

            // add the linlay to the LinearLayout which is defined in the activity.

            SpawnLayout.addView(linlay);
}

I hope you can find the missing Command or sth else. Thanks

Upvotes: 1

Views: 168

Answers (1)

SunGa
SunGa

Reputation: 231

Your activity code is without any scrollview. Please paste the contents of activity layout (i.e activity_layoutTest.xml). Looking at the code, I think, it may be better for you to use scrollbar with a list adapter. A look at this question may help you : Using a ListAdapter to fill a LinearLayout inside a ScrollView layout

Upvotes: 1

Related Questions