title: How to get the order of texts in grid layout after drag and drop operation is done in android
body: <p>I have a grid layout with texts where I can perform drag and drop. Everything is working fine but I want to record the order of the grid layout or text after some <strong>DRAG AND DROP OPERATION</strong> is done. I tried getting the texts after the drag is performed but its not giving me updated position. </p>
  
  <p>Here is my code where I generate 'n' number of text views dynamically and add it to GRID LAYOUT</p>
  
  <pre><code>    text = new TextView[sizeofgrid];
      for (int i = 0; i &lt; sizeofgrid; i++) {
          text[i] = new TextView(MainActivity.this); // create the txt view
          first = new GridLayout.LayoutParams();
          first.width = pergrid;
          text[i].setLayoutParams(first);
          text[i].setGravity(Gravity.CENTER);
          first.height = LinearLayout.LayoutParams.MATCH_PARENT;
          text[i].setText(String.valueOf(finallist.get(i)));
  
          text[i].setTextSize(20);
          text[i].setBackgroundResource(R.drawable.back);
          text[i].setTextColor((Color.parseColor("#0B0B0B")));
          //  text[i].setPadding(5, 25, 10, 25);
          text[i].setPadding(5, 10, 0, 25);
           first.setMargins(0, 10, 5, 20);// margin can be set nly to layout and cant be set directly to textview
          text[i].setOnTouchListener(new View.OnTouchListener() {
              @Override
              public boolean onTouch(View view, MotionEvent motionEvent) {
                  final ClipData data = ClipData.newPlainText("", ""); 
                  View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
                  view.startDrag(data, shadowBuilder, view, 0);
                  view.setVisibility(View.VISIBLE); 
                  Log.d(tag, "calleddd");
                  return true;
              }
          });
  </code></pre>
  
  <p>Here is the code which performs drag </p>
  
  <pre><code>   class DragListener implements View.OnDragListener {
      @Override
      public boolean onDrag(View v, DragEvent event) { // the view which we are dragging and the events whcih gets triggred
  
         final View view = (View) event.getLocalState();
          switch (event.getAction()) {
              case DragEvent.ACTION_DRAG_LOCATION: // if the shadow of the view is within the bounds for a long amt of time , it will get triggred
                  // do nothing if hovering above own position
                  dragflag = true;
                  if (view == v) return true; // can or cant be thr..makes no diff
                  // get the new list index
                  final int index = calculateNewIndex(event.getX(), event.getY()); // as soon as we start moving from current pos, it will start
                  //calculating new value and will get relocated
                  // remove the view from the old position
                  gl.removeView(view); //remove the view from original pos
                  // and push to the new
                  gl.addView(view, index); // add the view to newly cal pos
                  break;
              case DragEvent.ACTION_DROP:
                  view.setVisibility(View.VISIBLE); 
                  //tested by retrieving the text of textview after dropping but it hasn't got updated.
  
                  for(int i = 0; i &lt; sizeofgrid; i++)
                  {
                      Log.d(tag,"thetexssis"+text[i].getText().toString());
                  }
                  break;
              case DragEvent.ACTION_DRAG_ENDED:
                  if (!event.getResult()) {
                      view.setVisibility(View.VISIBLE); 
  
                  }
                  break;
          }
          return true;
      }
  }
  </code></pre>
  
  <p>I tested by retrieving the text of the text view but it gives me the same order of text before drag and drop is performed. I don't know how to get the order of the current text view in grid layout.</p>
  
  <p>Any help would be great !! Thanks</p>