Skip to content
Advertisement

Java create a TextView without open new window

This is my code:

        LinearLayout linearLayout = new LinearLayout(this);
        setContentView(linearLayout);
        linearLayout.setOrientation(LinearLayout.VERTICAL); // Open new window

        TextView textView = new TextView(this);
        textView.setId(R.id.textView_note + i);
        textView.setWidth(50);
        textView.setWidth(70);
        textView.setText(title + "n" + map_from_file.get(title));
        textView.setTextSize(20);
        linearLayout.addView(textView);

What i made in the xml file deleted because’ it’s open new window. How can i add TextView without open new window, and actually save the work that i made?

Advertisement

Answer

This is happening because you are creating a new linear layout. Instead, add attribute to the old layout. Check out this code it may help:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout);
    linearLayout.setOrientation(LinearLayout.VERTICAL);

    TextView textView = new TextView(this);
    textView.setId(R.id.textView_note + i);
    textView.setWidth(50);
    textView.setWidth(70);
    textView.setText(title + "n" + map_from_file.get(title));
    textView.setTextSize(20);
    linearLayout.addView(textView);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement