title: Android programmatically add ImageView with ConstraintLayout
body: <p>I want to programmatically insert several ImageView into a RecyclerView using ConstraintLayout.</p>
  
  <p>To insert images in an equidistant way using Horizontal Bias, the value for each image is calculated as:</p>
  
  <pre><code>float biasedValue = (1f / (flightPlanWeather.weather.size () + 1f)) * (i + 1f);
  </code></pre>
  
  <p>The code is as follows</p>
  
  <pre><code>public class FlightPlanWeatherRecyclerAdapter extends RecyclerView.Adapter&lt;FlightPlanWeatherRecyclerAdapter.MyViewHolder&gt; {
      private static final String TAG = FlightPlanWeatherRecyclerAdapter.class.getName();
      private List&lt;FlightPlanWeather&gt; dataSet;
      private LayoutInflater mInflater;
      Context mContext;
  
  
      // Provide a suitable constructor (depends on the kind of dataset)
      // data is passed into the constructor
      public FlightPlanWeatherRecyclerAdapter(Context context, List&lt;FlightPlanWeather&gt; data) {
          this.mInflater = LayoutInflater.from(context);
          this.dataSet = data;
          this.mContext = context;
      }
  
      // inflates the row layout from xml when needed
      @Override
      public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = mInflater.inflate(R.layout.row_item_flightplan_weather, parent, false);
          return new MyViewHolder(view);
      }
  
  
      @Override
      public void onBindViewHolder(MyViewHolder holder, int position) {
          holder.bind(dataSet.get(position));
          FlightPlanWeather flightPlanWeather = dataSet.get(position);
  
          holder.from_img.setImageResource(flightPlanWeather.from.getImg_resource());
          holder.from_label.setText(flightPlanWeather.from.getLabel());
  
          holder.to_img.setImageResource(flightPlanWeather.to.getImg_resource());
          holder.to_label.setText(flightPlanWeather.to.getLabel());
  
  
          int dimensionInPixel = 256;
  
          ConstraintLayout.LayoutParams lp =
                  new ConstraintLayout.LayoutParams(/*ConstraintLayout.LayoutParams.WRAP_CONTENT,*/ dimensionInPixel,
                          /*ConstraintLayout.LayoutParams.WRAP_CONTENT*/ dimensionInPixel);
  
  
  
          //weather
          for(int i=0; i&lt;flightPlanWeather.weather.size(); i++ ){
              ConstraintSet constraintSet = new ConstraintSet();
  
              holder.mConstraintSetList.add(constraintSet);
              ImageView imageView = new ImageView(mContext);
              imageView.setId(View.generateViewId());
              imageView.setImageResource(flightPlanWeather.weather.get( i ));
              imageView.setLayoutParams(lp);
              imageView.requestLayout();
              holder.weather.add(imageView);
              holder.mConstraintLayout.addView( imageView/*, lp */);
  
              //}
              constraintSet.clone(holder.mConstraintLayout);
  
              float biasedValue = (1f/ (flightPlanWeather.weather.size()+1f)) * (i+1f);
              Log.d(TAG, Float.toString( biasedValue ) +"i:"+ Integer.toString( i )+"size:"+ flightPlanWeather.weather.size());
              constraintSet.setHorizontalBias(imageView.getId(), biasedValue);
              constraintSet.connect(imageView.getId(), ConstraintSet.START, holder.mConstraintLayout.getId(), ConstraintSet.START, 0);
              constraintSet.connect(imageView.getId(), ConstraintSet.END, holder.mConstraintLayout.getId(), ConstraintSet.END, 0);
              constraintSet.connect(imageView.getId(), ConstraintSet.TOP, holder.mConstraintLayout.getId(), ConstraintSet.TOP, 0);
              constraintSet.connect(imageView.getId(), ConstraintSet.BOTTOM, holder.mConstraintLayout.getId(), ConstraintSet.BOTTOM, 0);
              constraintSet.applyTo(holder.mConstraintLayout);
              holder.mConstraintLayout.requestLayout();
          }
  
      }
  
      // Return the size of your dataset (invoked by the layout manager)
      @Override
      public int getItemCount() {
          return dataSet == null ? 0 : dataSet.size();
      }
  
  
      // Provide a reference to the views for each data item
      // Complex data items may need more than one view per item, and
      // you provide access to all the views for a data item in a view holder
      // Static inner class to initialize the views of rows
      static class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
  
          TextView from_label;
          TextView to_label;
          ImageView from_img;
          ImageView to_img;
          ImageView line;
          ConstraintLayout mConstraintLayout;
  
          List&lt;ConstraintSet&gt; mConstraintSetList = new ArrayList&lt;&gt;(  );
          List&lt;ImageView&gt; weather = new ArrayList&lt;&gt;(  );
  
          public MyViewHolder(View itemView) {
              super(itemView);
              itemView.setOnClickListener(this);
  
              mConstraintLayout  = (ConstraintLayout) itemView.findViewById(R.id.mainConstraintLayout);
              from_label = (TextView) itemView.findViewById(R.id.from_label);
              to_label = (TextView) itemView.findViewById(R.id.to_label);
              from_img = (ImageView) itemView.findViewById(R.id.from_img);
              to_img = (ImageView) itemView.findViewById(R.id.to_img);
              line = (ImageView) itemView.findViewById(R.id.line);
  
  
  
          }
  
          public void bind(Object object){
  
  
          }
  
          @Override
          public void onClick(View view) {
              Log.d("onclick", "onClick " + getLayoutPosition() + " " /*+ /*item.getText()*/);
          }
      }
  
  }
  </code></pre>
  
  <p>The problem is that the Horizontal Bias of the weather does not work properly
  <a href="https://i.stack.imgur.com/RDi0K.png" rel="nofollow noreferrer">screenshot</a></p>
  
  <p>The View should come this way:
  <a href="https://i.stack.imgur.com/dglfG.png" rel="nofollow noreferrer">screenshot</a></p>