title: How to reduce Table load time in android
body: <p>My application has a search product option. I need to search few products out of 150 products(minimum). I have already done that, but when I remove last character in product search option, I have to wait for few seconds(30 seconds), because within that time all the products should be loaded into the table(150 products), but I need to reduce the time.</p>
  
  <p>My table layout class</p>
  
  <pre><code>public class TableMainLayout extends RelativeLayout implements OnClickListener{
  
  InvoiceItemClickListner callBack;
  public final String TAG = "TableMainLayout.java";
  ArrayList&lt;Product&gt; New_productList;
  HashMap&lt;String, RetailerSalesSummary&gt; retailerSummry;
  public ArrayList&lt;Expense&gt; expense = new ArrayList&lt;Expense&gt;();
  HashMap&lt;String,String&gt; shelfStockValue;
  private DecimalFormat df = new DecimalFormat("###,###,###,##0.00"); 
  
  
  int texthead = (int) (getResources().getDimension(R.dimen.boldtext) / getResources()
          .getDisplayMetrics().density);
  int textRow = (int) (getResources().getDimension(R.dimen.normalText) / getResources()
          .getDisplayMetrics().density);
  int pcellLpWP = (int) (getResources().getDimension(R.dimen.pcellSecWP) / getResources()
          .getDisplayMetrics().density);
  int thheight = (int) (getResources().getDimension(R.dimen.icellLpH) / getResources()
          .getDisplayMetrics().density);
  int bheight = (int) (getResources().getDimension(R.dimen.icellLpBH) / getResources()
          .getDisplayMetrics().density);
  int pcellLpW = (int) (getResources().getDimension(R.dimen.pcellLpW) / getResources()
          .getDisplayMetrics().density);
  int pcellSmallB = (int) (getResources().getDimension(R.dimen.pcellSmallD) / getResources()
          .getDisplayMetrics().density);
  int longdesPadingH =  (int) (getResources().getDimension(R.dimen.cellLOngHieght) / getResources()
          .getDisplayMetrics().density);
  TableRow.LayoutParams cellSecL = new TableRow.LayoutParams(pcellLpWP,
          thheight, 1.0f);
  TableRow.LayoutParams cellLp = new TableRow.LayoutParams(pcellLpW,
          thheight, 1.0f);
  TableRow.LayoutParams cellSmallB = new TableRow.LayoutParams(pcellSmallB,
          thheight, 1.0f);
  
  
  
  // set the header titles
  String headers[] = {
      "Description",
      "Qty",
      "Value",
      "Shelf",
      "Reason",
      "Dis Qty",
      "DisProduct",
      "Dis Val",
      "LongDescrip",
      "PCode"
  };
  
  TableLayout tableA;
  TableLayout tableB;
  TableLayout tableC;
  TableLayout tableD;
  
  HorizontalScrollView horizontalScrollViewB;
  HorizontalScrollView horizontalScrollViewD;  
  
  ScrollView scrollViewC;
  ScrollView scrollViewD;
  
  Context context;
  
  
  
  int headerCellsWidth[] = new int[headers.length+1];
  
  public TableMainLayout(Context context,
          InvocieProductFragment invocieProductFragment,
          ArrayList&lt;Product&gt; productList,
          HashMap&lt;String, String&gt; shelfStockValue, ArrayList&lt;Expense&gt; expense,boolean StckValidation) {
  
      super(context);
  
      this.context = context;
      this.callBack = (InvoiceItemClickListner) invocieProductFragment;
      // initialize the main components (TableLayouts, HorizontalScrollView,
      // ScrollView)
      this.initComponents(productList, retailerSummry,shelfStockValue,expense);
      this.setComponentsId();
      this.setScrollViewAndHorizontalScrollViewTag();
  
      // no need to assemble component A, since it is just a table
      this.horizontalScrollViewB.addView(this.tableB);
  
      this.scrollViewC.addView(this.tableC);
  
      this.scrollViewD.addView(this.horizontalScrollViewD);
      this.horizontalScrollViewD.addView(this.tableD);
  
      // add the components to be part of the main layout
      this.addComponentToMainLayout();
  
      // add some table rows
      this.addTableRowToTableA(StckValidation);
      this.addTableRowToTableB();
  
      this.resizeHeaderHeight();
  
      this.getTableRowHeaderCellWidth();
  
      this.generateTableC_AndTable_B(StckValidation);
  
      this.resizeBodyTableRowHeight();
  }
  
  
  // initalized components
  private void initComponents(ArrayList&lt;Product&gt; productList,
          HashMap&lt;String, RetailerSalesSummary&gt; retailerSummry,
          HashMap&lt;String, String&gt; shelfStockValue, ArrayList&lt;Expense&gt; expense) {
  
      this.tableA = new TableLayout(this.context);
      this.tableB = new TableLayout(this.context);
      this.tableC = new TableLayout(this.context);
      this.tableD = new TableLayout(this.context);
  
      this.horizontalScrollViewB = new MyHorizontalScrollView(this.context);
      this.horizontalScrollViewD = new MyHorizontalScrollView(this.context);
  
      this.scrollViewC = new MyScrollView(this.context);
      this.scrollViewD = new MyScrollView(this.context);
  
      this.New_productList = productList;
      this.retailerSummry = retailerSummry;
      this.shelfStockValue = shelfStockValue;
      this.expense = expense;
  
  }
  
  // set essential component IDs
  private void setComponentsId(){
      this.tableA.setId(1);
      this.horizontalScrollViewB.setId(2);
      this.scrollViewC.setId(3);
      this.scrollViewD.setId(4);
  }
  
  // set tags for some horizontal and vertical scroll view
  private void setScrollViewAndHorizontalScrollViewTag(){
  
      this.horizontalScrollViewB.setTag("horizontal scroll view b");
      this.horizontalScrollViewD.setTag("horizontal scroll view d");
  
      this.scrollViewC.setTag("scroll view c");
      this.scrollViewD.setTag("scroll view d");
  }
  
  // we add the components here in our TableMainLayout
  private void addComponentToMainLayout(){
  
      // RelativeLayout params were very useful here
      // the addRule method is the key to arrange the components properly
      RelativeLayout.LayoutParams componentB_Params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
      componentB_Params.addRule(RelativeLayout.RIGHT_OF, this.tableA.getId());
  
      RelativeLayout.LayoutParams componentC_Params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
      componentC_Params.addRule(RelativeLayout.BELOW, this.tableA.getId());
  
      RelativeLayout.LayoutParams componentD_Params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
      componentD_Params.addRule(RelativeLayout.RIGHT_OF, this.scrollViewC.getId());
      componentD_Params.addRule(RelativeLayout.BELOW, this.horizontalScrollViewB.getId());
  
      // 'this' is a relative layout, 
      // we extend this table layout as relative layout as seen during the creation of this class
      this.addView(this.tableA);
      this.addView(this.horizontalScrollViewB, componentB_Params);
      this.addView(this.scrollViewC, componentC_Params);
      this.addView(this.scrollViewD, componentD_Params);
  
  }
  
  
  
  private void addTableRowToTableA(boolean StckValidation){
      this.tableA.addView(this.componentATableRow(StckValidation));
  }
  
  private void addTableRowToTableB(){
      this.tableB.addView(this.componentBTableRow());
  
  }
  
  // generate table row of table A
  TableRow componentATableRow(boolean StckValidation){
      TableRow.LayoutParams params = new TableRow.LayoutParams( pcellLpWP,thheight);
      params.setMargins(2, 2, 0, 0);
      //params.setMargins(2, 0, 0, 0);//10, 5, 10, 5
      //TableRow.LayoutParams cellSmallB = new TableRow.LayoutParams(66,thheight);
      TableRow.LayoutParams cellSmallB = new TableRow.LayoutParams(pcellSmallB,thheight);
      cellSmallB.setMargins(2, 2, 0, 0);
      //cellSmallB.setMargins(2, 0, 0, 0);
      TableRow componentATableRow = new TableRow(this.context);
      TextView textView = this.headerTextView(this.headers[0]);
      TextView textView1 = this.headerTextView("");
      componentATableRow.addView(textView1,cellSmallB);
      componentATableRow.addView(textView,params);
      if(!StckValidation)
      {
          textView1.setVisibility(View.GONE);
      }
  
  
      return componentATableRow;
  }
  
  // generate table row of table B
  TableRow componentBTableRow(){
  
      TableRow componentBTableRow = new TableRow(this.context);
      int headerFieldCount = this.headers.length;
  
      TableRow.LayoutParams params = new TableRow.LayoutParams(pcellLpW,thheight);
      params.setMargins(2, 0, 0, 0);
  
      TableRow.LayoutParams paramlong = new TableRow.LayoutParams( pcellLpWP,thheight);
      paramlong.setMargins(2, 0, 0, 0);
  
      for(int x=0; x&lt;(headerFieldCount-1); x++){
          TextView textView = this.headerTextView(this.headers[x+1]);
          textView.setLayoutParams(params);
  
          if(headers[x+1].equals("Shelf") || headers[x+1].equals("Reason"))
          {
              textView.setVisibility(View.GONE);
          }else if(headers[x+1].equals("LongDescrip"))
          {
              textView.setLayoutParams(paramlong);
          }
          componentBTableRow.addView(textView);
      }
  
      return componentBTableRow;
  }
  
  // generate table row of table C and table D
  private void generateTableC_AndTable_B(boolean StckValidation){
  
  
      int position = 0;
      for(Product productObject : this.New_productList){
  
  
          TableRow tableRowForTableC = this.tableRowForTableC(productObject,StckValidation);
          TableRow taleRowForTableD = this.taleRowForTableD(productObject,position);
  
          tableRowForTableC.setBackgroundColor(Color.GRAY);
          taleRowForTableD.setBackgroundColor(Color.GRAY);
  
          this.tableC.addView(tableRowForTableC);
          this.tableD.addView(taleRowForTableD);
          position++;
  
      }
  }
  
  // a TableRow for table C
  TableRow tableRowForTableC(Product product,boolean StckValidation){      
      TableRow.LayoutParams cellSmallB = new TableRow.LayoutParams(pcellSmallB,bheight);
      cellSmallB.setMargins(2, 2, 0, 0);
      //cellSmallB.setMargins(10, 5, 10, 5);
      TableRow.LayoutParams params = new TableRow.LayoutParams( pcellLpWP,thheight);
      params.setMargins(2, 2, 0, 0);
  
  
      TableRow tableRowForTableC = new TableRow(this.context);
  
      ImageButton btnProductDetails = new ImageButton(context);
      btnProductDetails.setImageDrawable(context.getResources()
              .getDrawable(R.drawable.invoice_de_btn));
      btnProductDetails.setId(1);
      btnProductDetails.setTag(product);
      //btnProductDetails.setGravity(Gravity.CENTER);
      tableRowForTableC.addView(btnProductDetails,cellSmallB);
  //  btnProductDetails.setVisibility(View.GONE);
  
      if( (!product.getSerialNumberRequired().equals("1")) &amp;&amp; !(product.getBatchProcessingFlag().equals("1")) )
      {
          btnProductDetails.setEnabled(false); 
      }
  
      if(!StckValidation)
      {
          btnProductDetails.setVisibility(View.GONE);
      }
  
      TextView textView1 = this.bodyTextView(product.getDescription(),Gravity.LEFT,Color.LTGRAY);
      tableRowForTableC.addView(textView1,params);
  
  
  
      btnProductDetails.setOnClickListener(this);
  
  
  
      return tableRowForTableC;
  }
  
  TableRow taleRowForTableD(final Product product,final int position){
  
      TableRow.LayoutParams params = new TableRow.LayoutParams( pcellLpW,thheight);
      params.setMargins( 2, 2, 0, 0);
      TableRow.LayoutParams paramstest = new TableRow.LayoutParams( pcellLpW,thheight);
      paramstest.setMargins(2, 1, 0, 0);
  
      TableRow.LayoutParams paramlong = new TableRow.LayoutParams( pcellLpWP,thheight);
      paramlong.setMargins(2, 2, 0, 0);
  
      final TableRow taleRowForTableD = new TableRow(this.context); 
      taleRowForTableD.setTag(position);
  
  
      EditText txtQty = this.bodyEditTextView("0",Gravity.CENTER,Color.WHITE);
      txtQty.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              callBack.onTableRowChildClicked("QTY",position,taleRowForTableD,v,product);
          }
      });
  
      taleRowForTableD.addView(txtQty,params);
  
  
      TextView txtVal = this.bodyTextView("0.0",Gravity.CENTER,Color.WHITE);
      taleRowForTableD.addView(txtVal,params);
  
      final EditText txtSQty;
      if(shelfStockValue.containsKey(product.getProductCode()))
      {
           txtSQty = this.bodyEditTextView(shelfStockValue.get(product.getProductCode()),Gravity.CENTER,Color.WHITE);
      }else
      {
           txtSQty = this.bodyEditTextView("0",Gravity.CENTER,Color.WHITE);
      }
      txtSQty.setTag("ShelfQTY");
      txtSQty.setVisibility(View.GONE);
      txtSQty.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              callBack.onTableRowChildClicked("ShelfQTY",position,taleRowForTableD,v,product);
          }
      });
  
  
      txtSQty.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              callBack.onTableRowChildClicked("ShelfQTY",position,taleRowForTableD,v,product);
          }
      });
  
      taleRowForTableD.addView(txtSQty,params);
  
  
      final Spinner spinner = new Spinner(this.context);
      spinner.setVisibility(View.VISIBLE);
      spinner.setPadding(5, 1, 5, 1);  
      spinner.setBackgroundColor(Color.WHITE);
      //spinner.setBackgroundResource(R.drawable.spinner_background_ab_whiteaction);
      spinner.setVisibility(View.GONE);
      taleRowForTableD.addView(spinner,params);
  
      spinner.setOnTouchListener(new OnTouchListener() {
  
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              callBack.onTableRowChildTouch("SpinnerTouch",position,taleRowForTableD,v,product);
  
              return false;
          }
      });
      spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
  
          @Override
          public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
                  int position, long arg3) {
  
              callBack.onItemSelected("SpinnerSelected",parent,view,position,product);
          }
  
          @Override
          public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  
  
          }
      });
  
  
  
  
      TextView txtDQty = this.bodyTextView("0",Gravity.CENTER,Color.WHITE);
      taleRowForTableD.addView(txtDQty,params);
  
      TextView txtDPCode = this.bodyTextView("",Gravity.CENTER,Color.WHITE);
      taleRowForTableD.addView(txtDPCode,params);
  
      TextView txtDisVal = this.bodyTextView("0.00",Gravity.CENTER,Color.WHITE);
      taleRowForTableD.addView(txtDisVal,params);
  
  
      //TextView txtDPCodetxtLongDes = this.bodyTextView1(product.getDescription2(),Gravity.LEFT);
      //taleRowForTableD.addView(txtDPCodetxtLongDes,paramstest);
  
      TextView txtDPCodetxtLongDes = this.bodyTextView1(product.getDescription2(),Gravity.LEFT);
      //txtDPCodetxtLongDes.setFocusable(false);
      taleRowForTableD.addView(txtDPCodetxtLongDes,paramlong);
  
      TextView txtpCode = this.bodyTextView1(product.getProductCode(),Gravity.LEFT);
      taleRowForTableD.addView(txtpCode,paramstest);
  
      TextView txtVat = this.bodyTextView("0",Gravity.CENTER,Color.WHITE);
      txtVat.setVisibility(View.GONE);
      taleRowForTableD.addView(txtVat,params);
  
  
      setOrderItemDetails(this.context,product,"",taleRowForTableD);
  
      return taleRowForTableD;
  
  }
  
  // table cell standard TextView
  TextView bodyTextView(String label, int gravity, int color) {
  
      TextView bodyTextView = new TextView(this.context);
      bodyTextView.setBackgroundColor(color);
      bodyTextView.setText(label);
      bodyTextView.setGravity(gravity);
      bodyTextView.setTextSize(textRow);
      bodyTextView.setPadding(5, 5, 5, 5);
  
      return bodyTextView;
  }
  
  // table cell standard TextView
  TextView bodyTextView1(String label, int gravity) {
  
      TextView bodyTextView = new TextView(this.context);
      bodyTextView.setBackgroundColor(Color.WHITE);
      bodyTextView.setText(label);
      bodyTextView.setGravity(gravity);
      bodyTextView.setTextSize(textRow);
      bodyTextView.setPadding(5, longdesPadingH, 5, 5);
  
      return bodyTextView;
  }
  
  // table cell standard EditText
  EditText bodyEditTextView(String label,int gravity,int Color ){
  
  
      EditText edttxt = new EditText(this.context);
      edttxt.setBackgroundColor(Color);
      edttxt.setText(label);
      edttxt.setGravity(gravity); 
      edttxt.setTextSize(textRow);
      edttxt.setLongClickable(false);
      edttxt.setInputType(InputType.TYPE_NULL);
      edttxt.setPadding(5, 5, 5, 5);
      edttxt.setHint("0");
  
  
      return edttxt;
  }
  
  // header standard TextView
  TextView headerTextView(String label){
  
      TextView headerTextView = new TextView(this.context);
      headerTextView.setBackgroundResource(R.drawable.btn_red);
      headerTextView.setText(label);
      headerTextView.setTextSize(texthead);
      headerTextView.setTextColor(Color.WHITE);
      headerTextView.setTypeface(null, Typeface.BOLD);
      headerTextView.setGravity(Gravity.CENTER);
      headerTextView.setPadding(10, 5, 10, 5);
  
      return headerTextView;
  }
  
  // resizing TableRow height starts here
  void resizeHeaderHeight() {
  
      TableRow productNameHeaderTableRow = (TableRow) this.tableA.getChildAt(0);
      TableRow productInfoTableRow = (TableRow)  this.tableB.getChildAt(0);
  
      int rowAHeight = this.viewHeight(productNameHeaderTableRow);
      int rowBHeight = this.viewHeight(productInfoTableRow);
  
      TableRow tableRow = rowAHeight &lt; rowBHeight ? productNameHeaderTableRow : productInfoTableRow;
      int finalHeight = rowAHeight &gt; rowBHeight ? rowAHeight : rowBHeight;
  
      this.matchLayoutHeight(tableRow, finalHeight);
  }
  
  void getTableRowHeaderCellWidth(){
  
      int tableAChildCount = ((TableRow)this.tableA.getChildAt(0)).getChildCount();
      int tableBChildCount = ((TableRow)this.tableB.getChildAt(0)).getChildCount();;
  
      for(int x=0; x&lt;(tableAChildCount+tableBChildCount); x++){
  
          if(x==0){
              this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableA.getChildAt(0)).getChildAt(x));
          }else if(x==1)
          {
              this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableA.getChildAt(0)).getChildAt(x));
          }
          else{
              this.headerCellsWidth[x] = this.viewWidth(((TableRow)this.tableB.getChildAt(0)).getChildAt(x-2));
          }
  
      }  
  }
  
  // resize body table row height
  void resizeBodyTableRowHeight(){
  
      int tableC_ChildCount = this.tableC.getChildCount();
  
      for(int x=0; x&lt;tableC_ChildCount; x++){
  
          TableRow productNameHeaderTableRow = (TableRow) this.tableC.getChildAt(x);
          TableRow productInfoTableRow = (TableRow)  this.tableD.getChildAt(x);
  
          int rowAHeight = this.viewHeight(productNameHeaderTableRow);
          int rowBHeight = this.viewHeight(productInfoTableRow);
  
          TableRow tableRow = rowAHeight &lt; rowBHeight ? productNameHeaderTableRow : productInfoTableRow;
          int finalHeight = rowAHeight &gt; rowBHeight ? rowAHeight : rowBHeight;
  
          this.matchLayoutHeight(tableRow, finalHeight);      
      }
  
  }
  
  // match all height in a table row
  // to make a standard TableRow height
  private void matchLayoutHeight(TableRow tableRow, int height) {
  
      int tableRowChildCount = tableRow.getChildCount();
  
      // if a TableRow has only 1 child
      if(tableRow.getChildCount()==1){
  
          View view = tableRow.getChildAt(0);
          TableRow.LayoutParams params = (TableRow.LayoutParams) view.getLayoutParams();
          params.height = height - (params.bottomMargin + params.topMargin);
  
          return ;
      }
  
      // if a TableRow has more than 1 child
      for (int x = 0; x &lt; tableRowChildCount; x++) {
  
          View view = tableRow.getChildAt(x);
  
          TableRow.LayoutParams params = (TableRow.LayoutParams) view.getLayoutParams();
  
          if (!isTheHeighestLayout(tableRow, x)) {
              params.height = height - (params.bottomMargin + params.topMargin);
              return;
          }
      }
  
  }
  
  // check if the view has the highest height in a TableRow
  private boolean isTheHeighestLayout(TableRow tableRow, int layoutPosition) {
  
      int tableRowChildCount = tableRow.getChildCount();
      int heighestViewPosition = -1;
      int viewHeight = 0;
  
      for (int x = 0; x &lt; tableRowChildCount; x++) {
          View view = tableRow.getChildAt(x);
          int height = this.viewHeight(view);
  
          if (viewHeight &lt; height) {
              heighestViewPosition = x;
              viewHeight = height;
          }
      }
  
      return heighestViewPosition == layoutPosition;
  }
  
  // read a view's height
  private int viewHeight(View view) {
      view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
      return view.getMeasuredHeight();
  }
  
  // read a view's width
  private int viewWidth(View view) {
      view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
      return view.getMeasuredWidth();
  }
  
  // horizontal scroll view custom class
  class MyHorizontalScrollView extends HorizontalScrollView{
  
      public MyHorizontalScrollView(Context context) {
          super(context);
      }
  
      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt) {
          String tag = (String) this.getTag();
  
          if(tag.equalsIgnoreCase("horizontal scroll view b")){
              horizontalScrollViewD.scrollTo(l, 0);
          }else{
              horizontalScrollViewB.scrollTo(l, 0);
          }
      }
  
  }
  
  // scroll view custom class
  class MyScrollView extends ScrollView{
  
      public MyScrollView(Context context) {
          super(context);
      }
  
      @Override
      protected void onScrollChanged(int l, int t, int oldl, int oldt) {
  
          String tag = (String) this.getTag();
  
          if(tag.equalsIgnoreCase("scroll view c")){
              scrollViewD.scrollTo(0, t);
          }else{
              scrollViewC.scrollTo(0,t);
          }
      }
  }
  
  
  public void setOrderItemDetails(Context context,Product product,String rawColor,TableRow tablerow)
  {
      TableRow table_row = tablerow;
      EditText txtQty = (EditText) table_row.getChildAt(0);
      TextView txtVal =  (TextView) table_row.getChildAt(1);
      EditText txtSQty = (EditText) table_row.getChildAt(2);
      Spinner spinner = (Spinner) table_row.getChildAt(3);
      TextView txtDQty = (TextView) table_row.getChildAt(4);
      TextView txtDPCode = (TextView) table_row.getChildAt(5);
      TextView txtDiVal = (TextView) table_row.getChildAt(6);
      TextView txtVat =  (TextView) table_row.getChildAt(9);
      for (Map.Entry&lt;String, ArrayList&lt;MyProduct&gt;&gt; entry : InvocieProductFragment.mapOrderd
              .entrySet()) {
          ArrayList&lt;MyProduct&gt; lstStyle = entry.getValue();
  
          //int r=lstStyle.size();
          //System.out.println("size..."+r);
          for (int k = 0; k &lt; lstStyle.size(); k++) {
  
              if (lstStyle.get(k).getProductCode().equals(product.getProductCode())) {
                  //rawColor = "1";
                  setTRowSectColor(tablerow);
                  spinner.setEnabled(false);
                  txtQty.setText("" + (int) lstStyle.get(k).getQty());
                  txtDiVal.setText(""+ df.format(lstStyle.get(k).getDisValue()));
                  txtDQty.setText(""+ (int) lstStyle.get(k).getDisQty());
                  txtDPCode.setText(lstStyle.get(k).getDisProductCode());
                  txtVal.setText(""+ df.format(lstStyle.get(k).getValue()));
                  //txtPri.setText(""+ dn.format(lstStyle.get(k).getPrice()));
                  txtVat.setText(""+ df.format(lstStyle.get(k).getVATValue()+ lstStyle.get(k).getBTTValue()+ lstStyle.get(k).getNBTValue()));
  
              }
          }
  
      }
  
  
      for (Map.Entry&lt;String,ShelfStock&gt; entry1 : InvocieProductFragment.alshelfStock
              .entrySet()) {
          ShelfStock rshelfstck1 = entry1.getValue();
          if(rshelfstck1.getProductCode().equals(product.getProductCode()))
          {
              txtSQty.setText(""+rshelfstck1.getShelfStckQty());
              int spinerIndex = rshelfstck1.getSpineerIndex();
  
              if(spinerIndex &gt; 0)
              {
                  ArrayAdapter&lt;String&gt; pNameAdapter = new ArrayAdapter&lt;String&gt;(context,R.layout.spinner_layout, createRReasonArrayList(expense));
                  pNameAdapter.setDropDownViewResource(R.layout.spinner_layout);  
                  spinner.setAdapter(pNameAdapter);
                  spinner.setSelection(spinerIndex);
              }
          }
      }
  
  
  }
  
  
  public ArrayList&lt;String&gt; createRReasonArrayList(ArrayList&lt;Expense&gt; searchItem) {
      ArrayList&lt;String&gt; rReasonArrayList = new ArrayList&lt;String&gt;();
      rReasonArrayList.add("----Select-------");
      for (int i = 0; i &lt; searchItem.size(); i++) {
  
          rReasonArrayList.add(searchItem.get(i).getParameterDescription());
  
  
      }
      Collections.sort(rReasonArrayList);
      return rReasonArrayList;
  }
  
  public void setTRowSectColor(TableRow tablerow) {
      TableRow table_row = tablerow;
      //TextView th3MQty = (TextView) table_row.getChildAt(0);
      EditText txtQty = (EditText) table_row.getChildAt(0);
      //TextView txtInvQty = (TextView) table_row.getChildAt(2);
      TextView txtVal = (TextView) table_row.getChildAt(1);
      EditText shelfQty = (EditText) table_row.getChildAt(2);
      Spinner spinner = (Spinner) table_row.getChildAt(3);
      TextView txtDQty = (TextView) table_row.getChildAt(4);
      TextView txtDPCode = (TextView) table_row.getChildAt(5);
      TextView txtDiVal = (TextView) table_row.getChildAt(6);
      TextView txtLongDes = (TextView) table_row.getChildAt(7);
      TextView txtPcode = (TextView) table_row.getChildAt(8);
  
  
  //  th3MQty.setBackgroundColor(Color.CYAN);
      txtQty.setBackgroundColor(Color.CYAN);
      //txtInvQty.setBackgroundColor(Color.CYAN);
      txtVal.setBackgroundColor(Color.CYAN);
      shelfQty.setBackgroundColor(Color.CYAN);
      spinner.setBackgroundColor(Color.CYAN);
      txtDQty.setBackgroundColor(Color.CYAN);
      txtDPCode.setBackgroundColor(Color.CYAN);
      txtDiVal.setBackgroundColor(Color.CYAN);
      txtLongDes.setBackgroundColor(Color.CYAN);
      txtPcode.setBackgroundColor(Color.CYAN);
      //txtVal.setBackgroundDrawable(getResources().getDrawable(R.drawable.table_shape));
  
  
  }
  
  
  @Override
  public void onClick(View view) {
  
      if(view.getId() == 1 )
      {
          double allocationCount = 0;
          Product product = (Product) view.getTag();
          if(product.getSerialNumberRequired() != null &amp;&amp; product.getSerialNumberRequired().equals("1"))
          {
              if(product.getLstCurrentSerial().size() &gt; 0)
              {
                  allocationCount = getCountSerialEditQty(product.getLstCurrentSerial());
              }
  
  
          }else if(product.getBatchProcessingFlag() != null &amp;&amp; product.getBatchProcessingFlag().equals("1"))
          {
              if(product.getLstPcurrentBatch().size() &gt; 0)
              {
                  allocationCount = getCountBatchEditQty(product.getLstPcurrentBatch());
              }
  
          }
  
          if(allocationCount &gt; 0)
          {
              callBack.viewProductSerialBatchProduct((Product) view.getTag());
          }
  
  
      }
  }
  
  
  /*public void detailsBtnOnClick(Product product)
  {
      Bundle bundle = new Bundle();
      bundle.putParcelable("ProductOB", product);
      product.setAvailableQuantity(500);
      Intent intent = new Intent(context,InvoiceProductDetailsActivity.class);
      intent.putExtras(bundle);   
      context.startActivity(intent);
  }
  */
  
  public double getCountSerialEditQty(ArrayList&lt;CurrentSerial&gt; lstPcurrentSerial)
  {
      double amount = 0;
      for(int j = 0 ; j &lt; lstPcurrentSerial.size(); j ++ )
      {
          amount +=  lstPcurrentSerial.get(j).getAlocateQty();
  
      }
  
      return amount;
  
  }
  
  public double getCountBatchEditQty(ArrayList&lt;CurrentSerialNumber&gt; lstPbatchSerial)
  {
      double amount = 0;
      for(int j = 0 ; j &lt; lstPbatchSerial.size(); j ++ )
      {
          amount +=  lstPbatchSerial.get(j).getAlocateQty();
  
      }
  
      return amount;
  
  }
  
  
  
  }
  </code></pre>
  
  <p>When I debugged that code it took a long time to run <strong>generateTableC_AndTable_B(boolean StckValidation)</strong> method.</p>
  
  <p>How can I reduce that time?</p>