title: Custom View Grid Object
body: <p>I'm trying to create a view that allows me to add pictures from the phone photos. In the Layout file I currently have a grid layout with nested LinearLayouts separated into 2 rows x 3 columns. The user will Long Click when they want to delete a photo. The expectation is that after the the image is deleted then the views will reorganize so that there  aren't space between the LinearLayout views. Instead I'm getting errors. I think the main issue is that the ImageViews nested withing the LinearLayouts don't have IDs. I've tried using GridViews, swapping ImageView positions, adding copying The ImageView uri to another Imageview. It seems like I'm trying to extend the functionalities to places that they aren't meant to go. Can someone help me ?</p>
  <pre><code>&lt;GridLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      android:id=&quot;@+id/pictureGrid&quot;
      android:layout_width=&quot;wrap_content&quot;
      android:layout_height=&quot;wrap_content&quot;
      android:columnCount=&quot;3&quot;
      android:orientation=&quot;horizontal&quot;
      android:rowCount=&quot;2&quot;
      android:layout_marginTop=&quot;330dp&quot;
      android:layout_marginBottom=&quot;20dp&quot;
      android:layout_marginRight=&quot;10dp&quot;
      android:layout_marginLeft=&quot;10dp&quot;
      android:columnOrderPreserved=&quot;true&quot;&gt;
  
       &lt;LinearLayout
          android:layout_width=&quot;90dp&quot;
          android:layout_height=&quot;90dp&quot;
          android:layout_weight=&quot;1&quot;
          android:layout_marginTop=&quot;20dp&quot;
          android:layout_marginBottom=&quot;20dp&quot;
          android:layout_marginRight=&quot;20dp&quot;
          android:layout_marginLeft=&quot;20dp&quot;
          android:layout_row=&quot;0&quot;
          android:layout_column=&quot;0&quot;
          android:background=&quot;@drawable/custom_border&quot;
          &gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;90dp&quot;
          android:layout_height=&quot;90dp&quot;
          android:layout_weight=&quot;1&quot;
          android:layout_marginTop=&quot;20dp&quot;
          android:layout_marginBottom=&quot;20dp&quot;
          android:layout_marginRight=&quot;20dp&quot;
          android:layout_marginLeft=&quot;20dp&quot;
          android:layout_row=&quot;0&quot;
          android:layout_column=&quot;1&quot;
          android:background=&quot;@drawable/custom_border&quot;
          &gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;90dp&quot;
          android:layout_height=&quot;90dp&quot;
          android:layout_weight=&quot;1&quot;
          android:layout_marginTop=&quot;20dp&quot;
          android:layout_marginBottom=&quot;20dp&quot;
          android:layout_marginRight=&quot;20dp&quot;
          android:layout_marginLeft=&quot;20dp&quot;
          android:layout_row=&quot;0&quot;
          android:background=&quot;@drawable/custom_border&quot;
          &gt;
  
      &lt;/LinearLayout&gt;
  
  
      &lt;LinearLayout
          android:layout_width=&quot;90dp&quot;
          android:layout_height=&quot;90dp&quot;
          android:layout_weight=&quot;1&quot;
          android:layout_marginTop=&quot;0dp&quot;
          android:layout_marginBottom=&quot;20dp&quot;
          android:layout_marginRight=&quot;20dp&quot;
          android:layout_marginLeft=&quot;20dp&quot;
          android:layout_column=&quot;0&quot;
          android:background=&quot;@drawable/custom_border&quot;
          &gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;90dp&quot;
          android:layout_height=&quot;90dp&quot;
          android:layout_weight=&quot;1&quot;
          android:layout_marginTop=&quot;0dp&quot;
          android:layout_marginBottom=&quot;20dp&quot;
          android:layout_marginRight=&quot;20dp&quot;
          android:layout_marginLeft=&quot;20dp&quot;
          android:background=&quot;@drawable/custom_border&quot;
          android:layout_column=&quot;1&quot;&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_width=&quot;90dp&quot;
          android:layout_height=&quot;90dp&quot;
          android:layout_weight=&quot;1&quot;
          android:layout_marginTop=&quot;0dp&quot;
          android:layout_marginBottom=&quot;20dp&quot;
          android:layout_marginRight=&quot;20dp&quot;
          android:layout_marginLeft=&quot;20dp&quot;
          android:background=&quot;@drawable/custom_border&quot;
          &gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/GridLayout&gt;
  
  </code></pre>
  <pre><code>
  import android.content.ActivityNotFoundException;
  import android.content.ClipData;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.drawable.BitmapDrawable;
  import android.graphics.drawable.Drawable;
  import android.media.Image;
  import android.net.Uri;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.GridLayout;
  import android.widget.ImageView;
  import android.widget.LinearLayout;
  
  
  
  
  import com.google.firebase.storage.FirebaseStorage;
  
  import java.net.URI;
  import java.util.ArrayList;
  
  public class RegisterPictures extends android.app.Activity {
      FirebaseStorage storage = FirebaseStorage.getInstance(&quot;gs://foodz-310506.appspot.com&quot;);
      private static final int SELECT_IMAGE_GALLERY = 3;
      GridLayout table;
       View img;
       View longClickView;
       int j;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.register_pictures);
          table =  findViewById(R.id.pictureGrid);
          for(int i = 0; i &lt; table.getChildCount(); i++) {
              img = table.getChildAt(i);
              img.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
                      //Open Up Gallery
                      try {
                          Intent toPhonePhotos = new Intent(Intent.ACTION_GET_CONTENT);
                          toPhonePhotos.setType(&quot;image/*&quot;);
                          toPhonePhotos.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                          toPhonePhotos.putExtra(Intent.CATEGORY_OPENABLE, true);
                          startActivityForResult(Intent.createChooser(toPhonePhotos, &quot;select Picture&quot;), SELECT_IMAGE_GALLERY);
                      } catch (ActivityNotFoundException e) {
                          Log.w(&quot;TAG&quot;, &quot;Unable to start phone gallery&quot;);
                      }
                  }
              });
  
              img.setOnLongClickListener(new View.OnLongClickListener(){
                 @Override
                 public boolean onLongClick(View view){
  
                          try {
                              Log.wtf(&quot;TAG&quot;, &quot;long clicking&quot;);
                              LinearLayout v = (LinearLayout) view;
                              removeSelectedView(v);
                              sortImages();
                          } catch (ActivityNotFoundException e) {
                              Log.wtf(&quot;TAG&quot;, &quot;wrong&quot;);
                          }
  
                         return true;
                 }
                });
              j++;
          }
      }
  
      protected  void onStart(){
          super.onStart();
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          //Was a photo picked?
          if(resultCode != RESULT_CANCELED &amp;&amp; data.getData()!=null/*requestCode == SELECT_IMAGE_GALLERY*/) {
              ClipData clip = data.getClipData();
              //one photo picked
              if(clip == null){
                  Log.e(&quot;TAG&quot;, &quot;only one image was selected&quot;);
                  Uri uri = data.getData();
                  photoCounter();
                  addSinglePhoto(uri);
               //Mult photos picked
              }else{
                  photoCounter();
                  addMultPhoto(clip);
                  }
          }else{
              Log.e(&quot;TAG&quot;, &quot;SHIT IS NOOOT WORKING&quot;);
              }
          }
  
  
      //Consecutively organizes images without whitespace
      private void sortImages(){
          for(int i=0; i &lt; table.getChildCount(); i++){
              LinearLayout imgHolder = (LinearLayout) table.getChildAt(i);
              LinearLayout imgHolder2 = (LinearLayout)table.getChildAt(i+1);
              ImageView image = (ImageView)imgHolder.getChildAt(0);
              ImageView image2 = (ImageView)imgHolder2.getChildAt(0);
              if( image2 != null &amp;&amp; image == null){
              //Get Drawable from ImageView and set to first ImageView
                  float x = imgHolder2.getX();
                  float y = imgHolder2.getY();
                  Bitmap bitmap = ((BitmapDrawable) image2.getDrawable()).getBitmap();
                  image.setImageBitmap(bitmap);
                  imgHolder2.removeAllViews();
                 /* imgHolder2.setX(imgHolder.getX());
                  imgHolder2.setY(imgHolder.getY());
                  imgHolder.setX(x);
                  imgHolder.setY(y);*/
              }
          }
      }
  
      // Copies ImageView to the previous index
      private Drawable CheckView( ImageView iv2) {
          Drawable iv2Image = iv2.getDrawable();
          return iv2Image;
  
  
  
      }
  
      private void removeSelectedView(LinearLayout v){
           v.removeAllViews();
  
      }
  
      private void photoCounter(){
  
      }
  
      //Adds one photo
      private void addSinglePhoto( Uri uri){
              int numOfCells = table.getChildCount();
              for(int i=0; i &lt; numOfCells; i++){
                  Log.e(&quot;TAG&quot;,&quot;Started for statement&quot;);
                  LinearLayout imageHolder = (LinearLayout) table.getChildAt(i);
                  if(imageHolder.getChildCount() == 0){
                      imageHolder.addView(imageViewCreater(uri));
                      i = numOfCells;
                  }
              }
      }
  
      //adds multiple photos from Clip
      private void addMultPhoto(ClipData clip){
          int numOfCells = table.getChildCount();
          int j = clip.getItemCount()-1;
          for(int i=0; i &lt; numOfCells-1; i++){
              LinearLayout cell = (LinearLayout) table.getChildAt(i);
                 Uri uri = clip.getItemAt(j).getUri();
                   cell.addView(imageViewCreater(uri));
          }
      }
  
      private ImageView imageViewCreater(Uri uri){
          ImageView  grandChildImage = new ImageView(this);
          grandChildImage.setLayoutParams(new LinearLayout.LayoutParams(
                  LinearLayout.LayoutParams.MATCH_PARENT,
                  LinearLayout.LayoutParams.MATCH_PARENT));
  
          grandChildImage.setImageURI(uri);
          return grandChildImage;
      }
  
  
  
  
  }
  </code></pre>