TextView is not programmatically added to my Layout












1















I am trying to add TextViews to LinearLayout programmatically. The amount of TextViews is based on UserInput which he can type in using AlertDialog Builder
But the TextViews are not added to the Layout. I don't know why. Here is my whole code. What is wrong in my code?



public class HandleTableClick extends AppCompatActivity {
public int personsnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handle_table_click);
final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Type number of persons");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
String persons = input.getText().toString();
try {
personsnumber = Integer.parseInt(persons);
}
catch (NumberFormatException nfe){

}
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
TextView pairs=new TextView[personsnumber];
for(int l=0; l<personsnumber; l++)
{
pairs[l] = new TextView(HandleTableClick.this);
pairs[l].setTextSize(15);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText((l + 1) + ": something");
myLayout.addView(pairs[l]);
}
}
});

alert.show();


}
}









share|improve this question




















  • 1





    Did you debug at all? Specifically, did you check the value of personsnumber in onClick()?

    – Mike M.
    Nov 20 '18 at 15:34






  • 1





    I think a possible reason could be because your EditText has the final keyword. Try taking that out. Also try doing EditText input = findViewById(R.id.editText);.

    – Ishaan Javali
    Nov 20 '18 at 15:36








  • 2





    Look at when you're getting that value from the EditText – before the AlertDialog is even shown. You need to get that value in onClick(). (Btw, that's the kind of info you want to include in your question.)

    – Mike M.
    Nov 20 '18 at 15:37








  • 1





    You don't need findViewById(). You already have a reference to the EditText. Move the String persons = input.getText().toString(); and parsing section to inside onClick().

    – Mike M.
    Nov 20 '18 at 15:40








  • 1





    Thank you very much that worked!!

    – Blnpwr
    Nov 20 '18 at 15:41
















1















I am trying to add TextViews to LinearLayout programmatically. The amount of TextViews is based on UserInput which he can type in using AlertDialog Builder
But the TextViews are not added to the Layout. I don't know why. Here is my whole code. What is wrong in my code?



public class HandleTableClick extends AppCompatActivity {
public int personsnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handle_table_click);
final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Type number of persons");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
String persons = input.getText().toString();
try {
personsnumber = Integer.parseInt(persons);
}
catch (NumberFormatException nfe){

}
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
TextView pairs=new TextView[personsnumber];
for(int l=0; l<personsnumber; l++)
{
pairs[l] = new TextView(HandleTableClick.this);
pairs[l].setTextSize(15);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText((l + 1) + ": something");
myLayout.addView(pairs[l]);
}
}
});

alert.show();


}
}









share|improve this question




















  • 1





    Did you debug at all? Specifically, did you check the value of personsnumber in onClick()?

    – Mike M.
    Nov 20 '18 at 15:34






  • 1





    I think a possible reason could be because your EditText has the final keyword. Try taking that out. Also try doing EditText input = findViewById(R.id.editText);.

    – Ishaan Javali
    Nov 20 '18 at 15:36








  • 2





    Look at when you're getting that value from the EditText – before the AlertDialog is even shown. You need to get that value in onClick(). (Btw, that's the kind of info you want to include in your question.)

    – Mike M.
    Nov 20 '18 at 15:37








  • 1





    You don't need findViewById(). You already have a reference to the EditText. Move the String persons = input.getText().toString(); and parsing section to inside onClick().

    – Mike M.
    Nov 20 '18 at 15:40








  • 1





    Thank you very much that worked!!

    – Blnpwr
    Nov 20 '18 at 15:41














1












1








1


1






I am trying to add TextViews to LinearLayout programmatically. The amount of TextViews is based on UserInput which he can type in using AlertDialog Builder
But the TextViews are not added to the Layout. I don't know why. Here is my whole code. What is wrong in my code?



public class HandleTableClick extends AppCompatActivity {
public int personsnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handle_table_click);
final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Type number of persons");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
String persons = input.getText().toString();
try {
personsnumber = Integer.parseInt(persons);
}
catch (NumberFormatException nfe){

}
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
TextView pairs=new TextView[personsnumber];
for(int l=0; l<personsnumber; l++)
{
pairs[l] = new TextView(HandleTableClick.this);
pairs[l].setTextSize(15);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText((l + 1) + ": something");
myLayout.addView(pairs[l]);
}
}
});

alert.show();


}
}









share|improve this question
















I am trying to add TextViews to LinearLayout programmatically. The amount of TextViews is based on UserInput which he can type in using AlertDialog Builder
But the TextViews are not added to the Layout. I don't know why. Here is my whole code. What is wrong in my code?



public class HandleTableClick extends AppCompatActivity {
public int personsnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handle_table_click);
final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Type number of persons");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
String persons = input.getText().toString();
try {
personsnumber = Integer.parseInt(persons);
}
catch (NumberFormatException nfe){

}
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
TextView pairs=new TextView[personsnumber];
for(int l=0; l<personsnumber; l++)
{
pairs[l] = new TextView(HandleTableClick.this);
pairs[l].setTextSize(15);
pairs[l].setLayoutParams(lp);
pairs[l].setId(l);
pairs[l].setText((l + 1) + ": something");
myLayout.addView(pairs[l]);
}
}
});

alert.show();


}
}






android layout






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 16:32









Fantômas

32.5k156388




32.5k156388










asked Nov 20 '18 at 15:29









BlnpwrBlnpwr

5341726




5341726








  • 1





    Did you debug at all? Specifically, did you check the value of personsnumber in onClick()?

    – Mike M.
    Nov 20 '18 at 15:34






  • 1





    I think a possible reason could be because your EditText has the final keyword. Try taking that out. Also try doing EditText input = findViewById(R.id.editText);.

    – Ishaan Javali
    Nov 20 '18 at 15:36








  • 2





    Look at when you're getting that value from the EditText – before the AlertDialog is even shown. You need to get that value in onClick(). (Btw, that's the kind of info you want to include in your question.)

    – Mike M.
    Nov 20 '18 at 15:37








  • 1





    You don't need findViewById(). You already have a reference to the EditText. Move the String persons = input.getText().toString(); and parsing section to inside onClick().

    – Mike M.
    Nov 20 '18 at 15:40








  • 1





    Thank you very much that worked!!

    – Blnpwr
    Nov 20 '18 at 15:41














  • 1





    Did you debug at all? Specifically, did you check the value of personsnumber in onClick()?

    – Mike M.
    Nov 20 '18 at 15:34






  • 1





    I think a possible reason could be because your EditText has the final keyword. Try taking that out. Also try doing EditText input = findViewById(R.id.editText);.

    – Ishaan Javali
    Nov 20 '18 at 15:36








  • 2





    Look at when you're getting that value from the EditText – before the AlertDialog is even shown. You need to get that value in onClick(). (Btw, that's the kind of info you want to include in your question.)

    – Mike M.
    Nov 20 '18 at 15:37








  • 1





    You don't need findViewById(). You already have a reference to the EditText. Move the String persons = input.getText().toString(); and parsing section to inside onClick().

    – Mike M.
    Nov 20 '18 at 15:40








  • 1





    Thank you very much that worked!!

    – Blnpwr
    Nov 20 '18 at 15:41








1




1





Did you debug at all? Specifically, did you check the value of personsnumber in onClick()?

– Mike M.
Nov 20 '18 at 15:34





Did you debug at all? Specifically, did you check the value of personsnumber in onClick()?

– Mike M.
Nov 20 '18 at 15:34




1




1





I think a possible reason could be because your EditText has the final keyword. Try taking that out. Also try doing EditText input = findViewById(R.id.editText);.

– Ishaan Javali
Nov 20 '18 at 15:36







I think a possible reason could be because your EditText has the final keyword. Try taking that out. Also try doing EditText input = findViewById(R.id.editText);.

– Ishaan Javali
Nov 20 '18 at 15:36






2




2





Look at when you're getting that value from the EditText – before the AlertDialog is even shown. You need to get that value in onClick(). (Btw, that's the kind of info you want to include in your question.)

– Mike M.
Nov 20 '18 at 15:37







Look at when you're getting that value from the EditText – before the AlertDialog is even shown. You need to get that value in onClick(). (Btw, that's the kind of info you want to include in your question.)

– Mike M.
Nov 20 '18 at 15:37






1




1





You don't need findViewById(). You already have a reference to the EditText. Move the String persons = input.getText().toString(); and parsing section to inside onClick().

– Mike M.
Nov 20 '18 at 15:40







You don't need findViewById(). You already have a reference to the EditText. Move the String persons = input.getText().toString(); and parsing section to inside onClick().

– Mike M.
Nov 20 '18 at 15:40






1




1





Thank you very much that worked!!

– Blnpwr
Nov 20 '18 at 15:41





Thank you very much that worked!!

– Blnpwr
Nov 20 '18 at 15:41












1 Answer
1






active

oldest

votes


















0














I already tested it! and function perfect! :)
And I added the setWeightSum to distribute the textView



public class MainActivity extends AppCompatActivity {

TextView textView;
Integer personsnumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personsnumber=0;
final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Type number of persons");
final EditText input = new EditText(this);
input.setInputType(InputType.TYPE_CLASS_NUMBER);
input.setRawInputType(Configuration.KEYBOARD_12KEY);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String persons = input.getText().toString();
try {
personsnumber = Integer.parseInt(persons);
}
catch (NumberFormatException nfe){
}
myLayout.setWeightSum(personsnumber);
for(int l=0; l<personsnumber; l++)
{
textView= new TextView(MainActivity.this);
textView.setTextSize(15);
textView.setId(l);
textView.setText((l + 1) + ": something");
textView.setLayoutParams(lp);
myLayout.addView(textView);

}
dialog.dismiss();
}
});
alert.show();
}


}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396328%2ftextview-is-not-programmatically-added-to-my-layout%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I already tested it! and function perfect! :)
    And I added the setWeightSum to distribute the textView



    public class MainActivity extends AppCompatActivity {

    TextView textView;
    Integer personsnumber;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    personsnumber=0;
    final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
    final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Type number of persons");
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    input.setRawInputType(Configuration.KEYBOARD_12KEY);
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
    String persons = input.getText().toString();
    try {
    personsnumber = Integer.parseInt(persons);
    }
    catch (NumberFormatException nfe){
    }
    myLayout.setWeightSum(personsnumber);
    for(int l=0; l<personsnumber; l++)
    {
    textView= new TextView(MainActivity.this);
    textView.setTextSize(15);
    textView.setId(l);
    textView.setText((l + 1) + ": something");
    textView.setLayoutParams(lp);
    myLayout.addView(textView);

    }
    dialog.dismiss();
    }
    });
    alert.show();
    }


    }





    share|improve this answer






























      0














      I already tested it! and function perfect! :)
      And I added the setWeightSum to distribute the textView



      public class MainActivity extends AppCompatActivity {

      TextView textView;
      Integer personsnumber;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      personsnumber=0;
      final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
      final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

      AlertDialog.Builder alert = new AlertDialog.Builder(this);
      alert.setTitle("Type number of persons");
      final EditText input = new EditText(this);
      input.setInputType(InputType.TYPE_CLASS_NUMBER);
      input.setRawInputType(Configuration.KEYBOARD_12KEY);
      alert.setView(input);
      alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int whichButton) {
      String persons = input.getText().toString();
      try {
      personsnumber = Integer.parseInt(persons);
      }
      catch (NumberFormatException nfe){
      }
      myLayout.setWeightSum(personsnumber);
      for(int l=0; l<personsnumber; l++)
      {
      textView= new TextView(MainActivity.this);
      textView.setTextSize(15);
      textView.setId(l);
      textView.setText((l + 1) + ": something");
      textView.setLayoutParams(lp);
      myLayout.addView(textView);

      }
      dialog.dismiss();
      }
      });
      alert.show();
      }


      }





      share|improve this answer




























        0












        0








        0







        I already tested it! and function perfect! :)
        And I added the setWeightSum to distribute the textView



        public class MainActivity extends AppCompatActivity {

        TextView textView;
        Integer personsnumber;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        personsnumber=0;
        final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Type number of persons");
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        input.setRawInputType(Configuration.KEYBOARD_12KEY);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        String persons = input.getText().toString();
        try {
        personsnumber = Integer.parseInt(persons);
        }
        catch (NumberFormatException nfe){
        }
        myLayout.setWeightSum(personsnumber);
        for(int l=0; l<personsnumber; l++)
        {
        textView= new TextView(MainActivity.this);
        textView.setTextSize(15);
        textView.setId(l);
        textView.setText((l + 1) + ": something");
        textView.setLayoutParams(lp);
        myLayout.addView(textView);

        }
        dialog.dismiss();
        }
        });
        alert.show();
        }


        }





        share|improve this answer















        I already tested it! and function perfect! :)
        And I added the setWeightSum to distribute the textView



        public class MainActivity extends AppCompatActivity {

        TextView textView;
        Integer personsnumber;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        personsnumber=0;
        final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

        AlertDialog.Builder alert = new AlertDialog.Builder(this);
        alert.setTitle("Type number of persons");
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_NUMBER);
        input.setRawInputType(Configuration.KEYBOARD_12KEY);
        alert.setView(input);
        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
        String persons = input.getText().toString();
        try {
        personsnumber = Integer.parseInt(persons);
        }
        catch (NumberFormatException nfe){
        }
        myLayout.setWeightSum(personsnumber);
        for(int l=0; l<personsnumber; l++)
        {
        textView= new TextView(MainActivity.this);
        textView.setTextSize(15);
        textView.setId(l);
        textView.setText((l + 1) + ": something");
        textView.setLayoutParams(lp);
        myLayout.addView(textView);

        }
        dialog.dismiss();
        }
        });
        alert.show();
        }


        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 20 '18 at 17:07

























        answered Nov 20 '18 at 16:12









        Vero GorenaVero Gorena

        214




        214






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396328%2ftextview-is-not-programmatically-added-to-my-layout%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

            Npm cannot find a required file even through it is in the searched directory