title: Adding views dynamically in AsyncTask
body: <p>I am creating some text and image views in a loop, with some data received from another class, how can i create those text and image views and use the addview method in a AsyncTask ?</p>
  
  <p>I am using addview in the doInBackground method, which doesn't work..
  Also, i don't know if this is the best way do dynamically add views, suggestions are welcome !</p>
  
  <pre><code>class syncTask extends AsyncTask&lt;Void, Void, Void&gt; {
       private ProgressDialog pd;
       int increment=0;
       RelativeLayout layout;
       public syncTask(RelativeLayout layout) {
          // TODO Auto-generated constructor stub
           this.layout = layout;
      }
  
      @Override
       protected void onPreExecute() {
          pd = new ProgressDialog(main.this);
          pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
          pd.setProgress(0);
          pd.setMax(0);
          pd.setMessage("Loading friends");
          pd.show();
       }
  
      @Override
       protected Void doInBackground(Void... v) {
          try
          {
              Set&lt;User&gt; users = LFMRestConnection.getFriendsNowListening(user, 1440);
              for (User u : users) {
                  pd.setMax(max);
                  //album picture
                      album_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                      resized_a_p = android.graphics.Bitmap.createScaledBitmap(album_pic, (width/3), (width/3), true);
                      iv_album = new ImageView(main.this);
                      iv_album.setImageBitmap(resized_a_p);
                      iv_album.layout(0, 0, 0, 0);
                      iv_album.setPadding(width-resized_a_p.getWidth()-5, t_img, 5, 0);
                  //profile picture
                      user_pic = loadBitmap(u.getImageURL().getImageURL(ImageURL.Size.LARGE));
                      resized_u_p = android.graphics.Bitmap.createScaledBitmap(user_pic, ((width/4)-10)/2, ((width/4)-10)/2, true);
                      iv_user = new ImageView(main.this);
                      iv_user.setImageBitmap(resized_u_p);
                      iv_user.setLayoutParams(params_2);
                      iv_user.layout(0, 0, 0, 0);
                      iv_user.setPadding(0, (t_img+resized_a_p.getHeight())-resized_u_p.getHeight(), resized_a_p.getWidth()+10, 0);
                  //shapes
                      //shapes = new shapes(this, (t_img+resized_a_p.getHeight()), resized_a_p.getWidth()+10);
                  //username text position
                      tv_user = new TextView(main.this);
                      tv_user.layout(0, 0, 0, 0);
                      tv_user.setTextSize(12);
                      tv_user.setPadding(0, (t_img+resized_a_p.getHeight())-resized_u_p.getHeight()+14, (resized_a_p.getWidth()+resized_u_p.getWidth())+15, 0);
                      tv_user.setGravity(Gravity.RIGHT);
                      tv_user.setLayoutParams(params_1);
                  //artist text position
                      tv_artist = new TextView(main.this);
                      tv_artist.layout(0, 0, 0, 0);
                      tv_artist.setTextSize(20);
                      tv_artist.setPadding(5, t_img-10, 0, 0);
                      tv_artist.setGravity(Gravity.LEFT);
                  //track text position
                      tv_info = new TextView(main.this);
                      tv_info.setTextSize(12);
                      tv_info.layout(0, 0, 0, 0);
                      tv_info.setPadding(5, t_img+12, 0, 0);
                      tv_info.setGravity(Gravity.LEFT);
  
                  //artist
                      tv_artist.setTypeface(null, Typeface.BOLD);
                      tv_artist.setText(u.getLastTrack().getArtistName());
                  //username 
                      tv_user.setTypeface(null, Typeface.NORMAL);
                      tv_user.setText(u.getUserName()+"\n");
                      tv_user.append(u.getRealName());
                  //track
                      tv_info.setTypeface(null, Typeface.NORMAL);
                      tv_info.append("\n" + u.getLastTrack().getTrackName());
                      tv_info.append("\n" + u.getLastTrack().getAlbumName());
  
                  //move position
                      t_img+=(width/3)+20;
  
                  increment++;
                  pd.incrementProgressBy(increment);
                  //views
                  layout.addView(iv_album);
                  layout.addView(iv_user);
                  layout.addView(tv_info);
                  layout.addView(tv_user);
                  layout.addView(tv_artist);
              }
          }
          catch(Exception e)
          {
              //startActivity(i);
              System.out.println("Error");
              //finish();     
          }
  
          return null;
  
       }
       @Override
          protected void onPostExecute(Void result) {
  
           pd.dismiss();
       }
  </code></pre>