Android: Using findViewById() with a string / in a loop












55















I'm making an android application, where there is a view composed of hundreds of buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code (for each one of the buttons).



My question is: How can I use findViewById without statically having to type in each button id?
Here is what I would like to do:



    for(int i=0; i<some_value; i++) {
for(int j=0; j<some_other_value; j++) {
String buttonID = "btn" + i + "-" + j;
buttons[i][j] = ((Button) findViewById(R.id.buttonID));
buttons[i][j].setOnClickListener(this);
}
}


Thanks in advance!










share|improve this question





























    55















    I'm making an android application, where there is a view composed of hundreds of buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code (for each one of the buttons).



    My question is: How can I use findViewById without statically having to type in each button id?
    Here is what I would like to do:



        for(int i=0; i<some_value; i++) {
    for(int j=0; j<some_other_value; j++) {
    String buttonID = "btn" + i + "-" + j;
    buttons[i][j] = ((Button) findViewById(R.id.buttonID));
    buttons[i][j].setOnClickListener(this);
    }
    }


    Thanks in advance!










    share|improve this question



























      55












      55








      55


      25






      I'm making an android application, where there is a view composed of hundreds of buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code (for each one of the buttons).



      My question is: How can I use findViewById without statically having to type in each button id?
      Here is what I would like to do:



          for(int i=0; i<some_value; i++) {
      for(int j=0; j<some_other_value; j++) {
      String buttonID = "btn" + i + "-" + j;
      buttons[i][j] = ((Button) findViewById(R.id.buttonID));
      buttons[i][j].setOnClickListener(this);
      }
      }


      Thanks in advance!










      share|improve this question
















      I'm making an android application, where there is a view composed of hundreds of buttons, each with a specific callback. Now, I'd like to set these callbacks using a loop, instead of having to write hundreds of lines of code (for each one of the buttons).



      My question is: How can I use findViewById without statically having to type in each button id?
      Here is what I would like to do:



          for(int i=0; i<some_value; i++) {
      for(int j=0; j<some_other_value; j++) {
      String buttonID = "btn" + i + "-" + j;
      buttons[i][j] = ((Button) findViewById(R.id.buttonID));
      buttons[i][j].setOnClickListener(this);
      }
      }


      Thanks in advance!







      android button clicklistener






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 1 '11 at 16:56









      Heiko Rupp

      22.3k1170113




      22.3k1170113










      asked Feb 1 '11 at 16:35









      user573536user573536

      9553108




      9553108
























          7 Answers
          7






          active

          oldest

          votes


















          94














          You should using getIdentifier()



          for(int i=0; i<some_value; i++) {
          for(int j=0; j<some_other_value; j++) {
          String buttonID = "btn" + i + "-" + j;
          int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
          buttons[i][j] = ((Button) findViewById(resID));
          buttons[i][j].setOnClickListener(this);
          }
          }





          share|improve this answer


























          • Thanks, that was what I was looking for.

            – user573536
            Feb 1 '11 at 17:20






          • 22





            "com.sample.project" can be replaced by getPackageName().

            – jenzz
            Nov 17 '12 at 17:41











          • I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

            – Dave C
            Mar 28 '16 at 3:39






          • 2





            Note that in my experience doing this gives you very poor performance.

            – ThomasW
            Jun 28 '16 at 3:56



















          4














          You can try making an int that holds all of your button IDs, and then iterate over that:



          int buttonIDs = new int {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }

          for(int i=0; i<buttonIDs.length; i++) {
          Button b = (Button) findViewById(buttonIDs[i]);
          b.setOnClickListener(this);
          }





          share|improve this answer































            3














            Take a look at these answers:




            • Android and getting a view with id cast as a string

            • Array of ImageButtons, assign R.view.id from a variable






            share|improve this answer





















            • 1





              @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

              – dave.c
              Feb 1 '11 at 16:47













            • You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

              – Cristian
              Feb 1 '11 at 17:03



















            1














            you can Use tag if you want to access.



            in onClick



            int i=Integer.parseInt(v.getTag);


            But you cant access that button like this.



            simply create button programatically



            by Button b=new Button(this);






            share|improve this answer

































              0














              create Custom Button in java code rather in Xml as i shown below



              Button bs_text= new Button[some_value];

              for(int z=0;z<some_value;z++)
              {
              try
              {

              bs_text[z] = (Button) new Button(this);

              }
              catch(ArrayIndexOutOfBoundsException e)
              {
              Log.d("ArrayIndexOutOfBoundsException",e.toString());
              }
              }





              share|improve this answer































                0














                If your top level view only has those button views as children, you could do



                for (int i = 0 ; i < yourView.getChildCount(); i++) {
                Button b = (Button) yourView.getChildAt(i);
                b.setOnClickListener(xxxx);
                }


                If there are more views present you'd need to check if the selected one is one of your buttons.






                share|improve this answer
























                • do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                  – dave.c
                  Feb 1 '11 at 16:58











                • No sorry no idea. A view knows his children, so I don't think it is too bad.

                  – Heiko Rupp
                  Feb 1 '11 at 21:26



















                0














                If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.



                int id = 0;

                switch(name) {
                case "x":
                id = R.id.x;
                break;
                etc.etc.
                }

                String value = findViewById(id);





                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%2f4865244%2fandroid-using-findviewbyid-with-a-string-in-a-loop%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  94














                  You should using getIdentifier()



                  for(int i=0; i<some_value; i++) {
                  for(int j=0; j<some_other_value; j++) {
                  String buttonID = "btn" + i + "-" + j;
                  int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                  buttons[i][j] = ((Button) findViewById(resID));
                  buttons[i][j].setOnClickListener(this);
                  }
                  }





                  share|improve this answer


























                  • Thanks, that was what I was looking for.

                    – user573536
                    Feb 1 '11 at 17:20






                  • 22





                    "com.sample.project" can be replaced by getPackageName().

                    – jenzz
                    Nov 17 '12 at 17:41











                  • I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

                    – Dave C
                    Mar 28 '16 at 3:39






                  • 2





                    Note that in my experience doing this gives you very poor performance.

                    – ThomasW
                    Jun 28 '16 at 3:56
















                  94














                  You should using getIdentifier()



                  for(int i=0; i<some_value; i++) {
                  for(int j=0; j<some_other_value; j++) {
                  String buttonID = "btn" + i + "-" + j;
                  int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                  buttons[i][j] = ((Button) findViewById(resID));
                  buttons[i][j].setOnClickListener(this);
                  }
                  }





                  share|improve this answer


























                  • Thanks, that was what I was looking for.

                    – user573536
                    Feb 1 '11 at 17:20






                  • 22





                    "com.sample.project" can be replaced by getPackageName().

                    – jenzz
                    Nov 17 '12 at 17:41











                  • I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

                    – Dave C
                    Mar 28 '16 at 3:39






                  • 2





                    Note that in my experience doing this gives you very poor performance.

                    – ThomasW
                    Jun 28 '16 at 3:56














                  94












                  94








                  94







                  You should using getIdentifier()



                  for(int i=0; i<some_value; i++) {
                  for(int j=0; j<some_other_value; j++) {
                  String buttonID = "btn" + i + "-" + j;
                  int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                  buttons[i][j] = ((Button) findViewById(resID));
                  buttons[i][j].setOnClickListener(this);
                  }
                  }





                  share|improve this answer















                  You should using getIdentifier()



                  for(int i=0; i<some_value; i++) {
                  for(int j=0; j<some_other_value; j++) {
                  String buttonID = "btn" + i + "-" + j;
                  int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
                  buttons[i][j] = ((Button) findViewById(resID));
                  buttons[i][j].setOnClickListener(this);
                  }
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 15 '16 at 7:10

























                  answered Feb 1 '11 at 16:44









                  WarrenFaithWarrenFaith

                  50k21124137




                  50k21124137













                  • Thanks, that was what I was looking for.

                    – user573536
                    Feb 1 '11 at 17:20






                  • 22





                    "com.sample.project" can be replaced by getPackageName().

                    – jenzz
                    Nov 17 '12 at 17:41











                  • I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

                    – Dave C
                    Mar 28 '16 at 3:39






                  • 2





                    Note that in my experience doing this gives you very poor performance.

                    – ThomasW
                    Jun 28 '16 at 3:56



















                  • Thanks, that was what I was looking for.

                    – user573536
                    Feb 1 '11 at 17:20






                  • 22





                    "com.sample.project" can be replaced by getPackageName().

                    – jenzz
                    Nov 17 '12 at 17:41











                  • I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

                    – Dave C
                    Mar 28 '16 at 3:39






                  • 2





                    Note that in my experience doing this gives you very poor performance.

                    – ThomasW
                    Jun 28 '16 at 3:56

















                  Thanks, that was what I was looking for.

                  – user573536
                  Feb 1 '11 at 17:20





                  Thanks, that was what I was looking for.

                  – user573536
                  Feb 1 '11 at 17:20




                  22




                  22





                  "com.sample.project" can be replaced by getPackageName().

                  – jenzz
                  Nov 17 '12 at 17:41





                  "com.sample.project" can be replaced by getPackageName().

                  – jenzz
                  Nov 17 '12 at 17:41













                  I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

                  – Dave C
                  Mar 28 '16 at 3:39





                  I'd seen other answers very similar to this, but the second arg being "id" was the last thing I needed for it to work.

                  – Dave C
                  Mar 28 '16 at 3:39




                  2




                  2





                  Note that in my experience doing this gives you very poor performance.

                  – ThomasW
                  Jun 28 '16 at 3:56





                  Note that in my experience doing this gives you very poor performance.

                  – ThomasW
                  Jun 28 '16 at 3:56













                  4














                  You can try making an int that holds all of your button IDs, and then iterate over that:



                  int buttonIDs = new int {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }

                  for(int i=0; i<buttonIDs.length; i++) {
                  Button b = (Button) findViewById(buttonIDs[i]);
                  b.setOnClickListener(this);
                  }





                  share|improve this answer




























                    4














                    You can try making an int that holds all of your button IDs, and then iterate over that:



                    int buttonIDs = new int {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }

                    for(int i=0; i<buttonIDs.length; i++) {
                    Button b = (Button) findViewById(buttonIDs[i]);
                    b.setOnClickListener(this);
                    }





                    share|improve this answer


























                      4












                      4








                      4







                      You can try making an int that holds all of your button IDs, and then iterate over that:



                      int buttonIDs = new int {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }

                      for(int i=0; i<buttonIDs.length; i++) {
                      Button b = (Button) findViewById(buttonIDs[i]);
                      b.setOnClickListener(this);
                      }





                      share|improve this answer













                      You can try making an int that holds all of your button IDs, and then iterate over that:



                      int buttonIDs = new int {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }

                      for(int i=0; i<buttonIDs.length; i++) {
                      Button b = (Button) findViewById(buttonIDs[i]);
                      b.setOnClickListener(this);
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 1 '11 at 16:39









                      Rick BarkhouseRick Barkhouse

                      6771512




                      6771512























                          3














                          Take a look at these answers:




                          • Android and getting a view with id cast as a string

                          • Array of ImageButtons, assign R.view.id from a variable






                          share|improve this answer





















                          • 1





                            @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

                            – dave.c
                            Feb 1 '11 at 16:47













                          • You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

                            – Cristian
                            Feb 1 '11 at 17:03
















                          3














                          Take a look at these answers:




                          • Android and getting a view with id cast as a string

                          • Array of ImageButtons, assign R.view.id from a variable






                          share|improve this answer





















                          • 1





                            @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

                            – dave.c
                            Feb 1 '11 at 16:47













                          • You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

                            – Cristian
                            Feb 1 '11 at 17:03














                          3












                          3








                          3







                          Take a look at these answers:




                          • Android and getting a view with id cast as a string

                          • Array of ImageButtons, assign R.view.id from a variable






                          share|improve this answer















                          Take a look at these answers:




                          • Android and getting a view with id cast as a string

                          • Array of ImageButtons, assign R.view.id from a variable







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 23 '17 at 11:55









                          Community

                          11




                          11










                          answered Feb 1 '11 at 16:40









                          CristianCristian

                          169k53334253




                          169k53334253








                          • 1





                            @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

                            – dave.c
                            Feb 1 '11 at 16:47













                          • You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

                            – Cristian
                            Feb 1 '11 at 17:03














                          • 1





                            @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

                            – dave.c
                            Feb 1 '11 at 16:47













                          • You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

                            – Cristian
                            Feb 1 '11 at 17:03








                          1




                          1





                          @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

                          – dave.c
                          Feb 1 '11 at 16:47







                          @user573536 be aware that using getResources().getIdentifier() in a loop can cause performance to degrade when you have a large number of lookups.

                          – dave.c
                          Feb 1 '11 at 16:47















                          You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

                          – Cristian
                          Feb 1 '11 at 17:03





                          You are right. I recommend that approach only when there's a sequence or order for the resources AND there are a lot of them (creating an array of 50 resources smells).

                          – Cristian
                          Feb 1 '11 at 17:03











                          1














                          you can Use tag if you want to access.



                          in onClick



                          int i=Integer.parseInt(v.getTag);


                          But you cant access that button like this.



                          simply create button programatically



                          by Button b=new Button(this);






                          share|improve this answer






























                            1














                            you can Use tag if you want to access.



                            in onClick



                            int i=Integer.parseInt(v.getTag);


                            But you cant access that button like this.



                            simply create button programatically



                            by Button b=new Button(this);






                            share|improve this answer




























                              1












                              1








                              1







                              you can Use tag if you want to access.



                              in onClick



                              int i=Integer.parseInt(v.getTag);


                              But you cant access that button like this.



                              simply create button programatically



                              by Button b=new Button(this);






                              share|improve this answer















                              you can Use tag if you want to access.



                              in onClick



                              int i=Integer.parseInt(v.getTag);


                              But you cant access that button like this.



                              simply create button programatically



                              by Button b=new Button(this);







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 10 '16 at 18:51









                              cricket_007

                              80k1142110




                              80k1142110










                              answered Feb 1 '11 at 16:42









                              Ganapathy CGanapathy C

                              4,81053668




                              4,81053668























                                  0














                                  create Custom Button in java code rather in Xml as i shown below



                                  Button bs_text= new Button[some_value];

                                  for(int z=0;z<some_value;z++)
                                  {
                                  try
                                  {

                                  bs_text[z] = (Button) new Button(this);

                                  }
                                  catch(ArrayIndexOutOfBoundsException e)
                                  {
                                  Log.d("ArrayIndexOutOfBoundsException",e.toString());
                                  }
                                  }





                                  share|improve this answer




























                                    0














                                    create Custom Button in java code rather in Xml as i shown below



                                    Button bs_text= new Button[some_value];

                                    for(int z=0;z<some_value;z++)
                                    {
                                    try
                                    {

                                    bs_text[z] = (Button) new Button(this);

                                    }
                                    catch(ArrayIndexOutOfBoundsException e)
                                    {
                                    Log.d("ArrayIndexOutOfBoundsException",e.toString());
                                    }
                                    }





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      create Custom Button in java code rather in Xml as i shown below



                                      Button bs_text= new Button[some_value];

                                      for(int z=0;z<some_value;z++)
                                      {
                                      try
                                      {

                                      bs_text[z] = (Button) new Button(this);

                                      }
                                      catch(ArrayIndexOutOfBoundsException e)
                                      {
                                      Log.d("ArrayIndexOutOfBoundsException",e.toString());
                                      }
                                      }





                                      share|improve this answer













                                      create Custom Button in java code rather in Xml as i shown below



                                      Button bs_text= new Button[some_value];

                                      for(int z=0;z<some_value;z++)
                                      {
                                      try
                                      {

                                      bs_text[z] = (Button) new Button(this);

                                      }
                                      catch(ArrayIndexOutOfBoundsException e)
                                      {
                                      Log.d("ArrayIndexOutOfBoundsException",e.toString());
                                      }
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Feb 1 '11 at 16:43









                                      Sankar GaneshSankar Ganesh

                                      10.2k114985




                                      10.2k114985























                                          0














                                          If your top level view only has those button views as children, you could do



                                          for (int i = 0 ; i < yourView.getChildCount(); i++) {
                                          Button b = (Button) yourView.getChildAt(i);
                                          b.setOnClickListener(xxxx);
                                          }


                                          If there are more views present you'd need to check if the selected one is one of your buttons.






                                          share|improve this answer
























                                          • do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                                            – dave.c
                                            Feb 1 '11 at 16:58











                                          • No sorry no idea. A view knows his children, so I don't think it is too bad.

                                            – Heiko Rupp
                                            Feb 1 '11 at 21:26
















                                          0














                                          If your top level view only has those button views as children, you could do



                                          for (int i = 0 ; i < yourView.getChildCount(); i++) {
                                          Button b = (Button) yourView.getChildAt(i);
                                          b.setOnClickListener(xxxx);
                                          }


                                          If there are more views present you'd need to check if the selected one is one of your buttons.






                                          share|improve this answer
























                                          • do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                                            – dave.c
                                            Feb 1 '11 at 16:58











                                          • No sorry no idea. A view knows his children, so I don't think it is too bad.

                                            – Heiko Rupp
                                            Feb 1 '11 at 21:26














                                          0












                                          0








                                          0







                                          If your top level view only has those button views as children, you could do



                                          for (int i = 0 ; i < yourView.getChildCount(); i++) {
                                          Button b = (Button) yourView.getChildAt(i);
                                          b.setOnClickListener(xxxx);
                                          }


                                          If there are more views present you'd need to check if the selected one is one of your buttons.






                                          share|improve this answer













                                          If your top level view only has those button views as children, you could do



                                          for (int i = 0 ; i < yourView.getChildCount(); i++) {
                                          Button b = (Button) yourView.getChildAt(i);
                                          b.setOnClickListener(xxxx);
                                          }


                                          If there are more views present you'd need to check if the selected one is one of your buttons.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Feb 1 '11 at 16:55









                                          Heiko RuppHeiko Rupp

                                          22.3k1170113




                                          22.3k1170113













                                          • do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                                            – dave.c
                                            Feb 1 '11 at 16:58











                                          • No sorry no idea. A view knows his children, so I don't think it is too bad.

                                            – Heiko Rupp
                                            Feb 1 '11 at 21:26



















                                          • do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                                            – dave.c
                                            Feb 1 '11 at 16:58











                                          • No sorry no idea. A view knows his children, so I don't think it is too bad.

                                            – Heiko Rupp
                                            Feb 1 '11 at 21:26

















                                          do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                                          – dave.c
                                          Feb 1 '11 at 16:58





                                          do you know how the performance compares with the other suggested methods? I'd assume that it was slightly slower than an array of ID's, but I'd be interested to know how it compares with getResources().getIdentifier().

                                          – dave.c
                                          Feb 1 '11 at 16:58













                                          No sorry no idea. A view knows his children, so I don't think it is too bad.

                                          – Heiko Rupp
                                          Feb 1 '11 at 21:26





                                          No sorry no idea. A view knows his children, so I don't think it is too bad.

                                          – Heiko Rupp
                                          Feb 1 '11 at 21:26











                                          0














                                          If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.



                                          int id = 0;

                                          switch(name) {
                                          case "x":
                                          id = R.id.x;
                                          break;
                                          etc.etc.
                                          }

                                          String value = findViewById(id);





                                          share|improve this answer




























                                            0














                                            If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.



                                            int id = 0;

                                            switch(name) {
                                            case "x":
                                            id = R.id.x;
                                            break;
                                            etc.etc.
                                            }

                                            String value = findViewById(id);





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.



                                              int id = 0;

                                              switch(name) {
                                              case "x":
                                              id = R.id.x;
                                              break;
                                              etc.etc.
                                              }

                                              String value = findViewById(id);





                                              share|improve this answer













                                              If for some reason you can't use the getIdentifier() function and/or you know the possible id's beforehand, you could use a switch.



                                              int id = 0;

                                              switch(name) {
                                              case "x":
                                              id = R.id.x;
                                              break;
                                              etc.etc.
                                              }

                                              String value = findViewById(id);






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Apr 17 '18 at 23:08









                                              B.CakirB.Cakir

                                              118112




                                              118112






























                                                  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%2f4865244%2fandroid-using-findviewbyid-with-a-string-in-a-loop%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

                                                  How to fix TextFormField cause rebuild widget in Flutter

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