title: How can i change the render of a TableLayout depending on the dimension of its contents?
body: <p>I have an <code>ExpandableListView</code> which is populated with some data that i retrieve from a web service. Inside a few of the child of this ExpView there are row populated by <code>TableLayout</code>. The problem is that <strong>i'd like to set <code>setStretchAllColumns()</code> to true when the width of the table is minor than screen's one; at the same time i'd like to put the table in a <code>ScroolView</code> if its width is greater than screen's one</strong> (to let the user scrolls through all the data in the large table).<br>
  I have tried using ViewTreeObserver but with no success. </p>
  
  <p>Can you help me with some code or a tutorial or just pointing me in the right direction plz?</p>
  
  <p>PS:sorry for my poor english, be patient.</p>
  
  <p>I post here the source of my <code>ExpandableListViewAdapter</code>, it's a mess but will help you understand what i'm trying to achieve. The important methods are <code>createTableOnUIThread</code> and <code>init</code>.</p>
  
  <pre><code>public class MyExpListAdapter extends BaseExpandableListAdapter {
  
      private final int TABLE_BORDER = 1;
      private final int TABLE_TEXT_PADDING = 10;
  
      private Context context = null;
  
      private List&lt;String&gt; keyList = null;
  
      private Map&lt;String, List&lt;LabelValueTable&gt;&gt; infoMap = null;
  
      private LayoutInflater inflater = null;
  
      public MyExpListAdapter(Context context, Map&lt;String, List&lt;LabelValueTable&gt;&gt; infoMap) {
          this.context = context;
          this.infoMap = infoMap;
          this.keyList = new ArrayList&lt;String&gt;(infoMap.keySet());
          this.inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      }
  
      @Override
      public Object getChild(int groupPosition, int childPosition) {
          return this.infoMap.get(this.keyList.get(groupPosition)).get(childPosition);
      }
  
      @Override
      public long getChildId(int groupPosition, int childPosition) {
          return childPosition;
      }
  
      @Override
      public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  
          LabelValueTable listElement = (LabelValueTable) this.getChild(groupPosition, childPosition);
          SubTableApparato table = listElement.getTable();
  
          ViewHolder holder = null;
  
          if (convertView == null) {
              convertView = this.inflater.inflate(R.layout.cmp_ac_scheda__expview_child, null);
  
              holder = new ViewHolder();
  
              holder.labelvalueContainer = (LinearLayout) convertView.findViewById(R.id.cmp_expview_child__labelvalue_container);
  
              holder.label = (TextView) convertView.findViewById(R.id.cmp_expview_child__label);
              holder.value = (TextView) convertView.findViewById(R.id.cmp_expview_child__value);
  
              holder.tableHeader = (TextView) convertView.findViewById(R.id.cmp_expview_child__tableheader);
  
              holder.tableScroll = (HorizontalScrollView) convertView.findViewById(R.id.cmp_expview_child__scrollview);
  
              holder.tableLayout = (TableLayout) convertView.findViewById(R.id.cmp_expview_child__tablelayout);
              holder.tableLayoutScrollable = (TableLayout) convertView.findViewById(R.id.cmp_expview_child__tablelayout_scrollable);
  
              convertView.setBackgroundResource(R.color.scheda_asset_oggetto_apparato_layout);
  
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
          // Se table è diverso da null disegno la tabella
          if (table != null) {
              holder.tableLayout.removeAllViewsInLayout();
              holder.tableLayoutScrollable.removeAllViewsInLayout();
  
              // Gruppo di controlli per il reset del layout
              holder.tableHeader.setVisibility(View.VISIBLE);
              holder.tableScroll.setVisibility(View.VISIBLE);
              holder.tableLayout.setVisibility(View.VISIBLE);
              holder.tableLayoutScrollable.setVisibility(View.VISIBLE);
  
              holder.labelvalueContainer.setVisibility(View.GONE);
  
              this.createTableOnUIThread(table, holder.tableHeader, holder.tableLayout, holder.tableLayoutScrollable);
          } else {
              // Altrimenti imposto i due valori label e value nelle rispettive
              // textview
              holder.tableHeader.setVisibility(View.GONE);
              holder.tableScroll.setVisibility(View.GONE);
              holder.tableLayout.setVisibility(View.GONE);
              holder.tableLayoutScrollable.setVisibility(View.GONE);
  
              holder.labelvalueContainer.setVisibility(View.VISIBLE);
  
              holder.label.setText(((LabelValueTable) listElement).getLabelValue().getLabel());
  
              holder.value.setText(((LabelValueTable) listElement).getLabelValue().getValue());
  
          }
          return convertView;
  
      }
  
      @Override
      public int getChildrenCount(int groupPosition) {
          return this.infoMap.get(this.keyList.get(groupPosition)).size();
      }
  
      @Override
      public Object getGroup(int groupPosition) {
          return this.keyList.get(groupPosition);
      }
  
      @Override
      public int getGroupCount() {
          return this.infoMap.size();
      }
  
      @Override
      public long getGroupId(int groupPosition) {
          return groupPosition;
      }
  
      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
          ViewHolder holder = null;
          if (convertView == null) {
              convertView = this.inflater.inflate(R.layout.cmp_ac_scheda__expview_group, null);
  
              holder = new ViewHolder();
              holder.tv = (TextView) convertView.findViewById(R.id.cmp_expview_group__groupname);
  
              holder.tv.setBackgroundResource(R.color.title_bar_bg_color);
              holder.tv.setTextColor(convertView.getResources().getColor(R.color.title_text_color));
              holder.tv.setBackgroundResource((R.drawable.scheda_asset__groupview_bg_style));
  
              convertView.setTag(holder);
          } else {
              holder = (ViewHolder) convertView.getTag();
          }
  
          holder.tv.setTypeface(null, Typeface.BOLD);
          holder.tv.setText((String) getGroup(groupPosition));
  
          return convertView;
      }
  
      @Override
      public boolean hasStableIds() {
          return true;
      }
  
      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
          return false;
      }
  
      private LinearLayout createFormattedCell(boolean isTabHeader, boolean isLast, String str) {
          // Layout che circonda le textView necessario per disegnare il bordo
          // delle celle
          LinearLayout container = new LinearLayout(this.context);
  
          container.setBackgroundColor(Color.BLACK);
  
          if (isLast) {
              container.setPadding(TABLE_BORDER, TABLE_BORDER, TABLE_BORDER, 0);
          } else {
              container.setPadding(TABLE_BORDER, TABLE_BORDER, 0, 0);
          }
  
          TextView textView = new TextView(this.context);
  
          textView.setPadding(TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING, TABLE_TEXT_PADDING);
          textView.setBackgroundColor(Color.WHITE);
          LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
          textView.setLayoutParams(params);
  
          if (isTabHeader) {
              textView.setTypeface(Typeface.DEFAULT_BOLD);
              textView.setGravity(Gravity.CENTER);
              textView.setBackgroundColor(this.context.getResources().getColor(R.color.light_grayish_orange));
          }
          textView.setText(str);
          container.addView(textView);
  
          return container;
      }
  
      private void createTableRows(List&lt;List&lt;String&gt;&gt; table, TableLayout tableLayout) {
          for (int i = 0; i &lt; table.size(); i++) {
              List&lt;String&gt; record = table.get(i);
              TableRow column = new TableRow(this.context);
              // If necessario per l'identificazione delle intestazioni delle
              // colonne
              if (i == 0) {
                  for (int j = 0; j &lt; record.size(); j++) {
                      String cellValue = record.get(j);
                      // If necessario per l'inserimento del bordo destro
                      // sull'ultima cella
                      if (j == record.size() - 1) {
                          column.addView(this.createFormattedCell(true, true, cellValue));
                      } else {
                          column.addView(this.createFormattedCell(true, false, cellValue));
                      }
                  }
              } else {
                  for (int j = 0; j &lt; record.size(); j++) {
                      String cellValue = record.get(j);
  
                      if (j == record.size() - 1) {
                          column.addView(this.createFormattedCell(false, true, cellValue));
                      } else {
                          column.addView(this.createFormattedCell(false, false, cellValue));
                      }
                  }
              }
              tableLayout.addView(column);
          }
      }
  
      private void createTableOnUIThread(SubTableApparato tableEntity, TextView tableHeader, final TableLayout tableLayout, final TableLayout tableLayoutScrollable) {
          // Setto il titolo della table
          tableHeader.setText(tableEntity.getTableName());
  
          List&lt;List&lt;String&gt;&gt; table = tableEntity.getTable();
  
          // if (table.size() != 0 &amp;&amp; table.get(0).size() &lt;= 3) {
          // // Se la tabella è vuota setto le colonne per non superare la
          // // larghezza dello schermo
          // this.createTableRows(table, tableLayout);
          // tableLayout.setStretchAllColumns(true);
          // // tableLayout.getWidth();
          // } else {
          // tableLayout.setStretchAllColumns(false);
          // this.createTableRows(table, tableLayoutScrollable);
          // }
          final TableLayout provvisorio = new TableLayout(this.context);
          tableLayoutScrollable.removeAllViews();
          tableLayoutScrollable.addView(provvisorio);
  
          final TableLayout provvisorio2 = new TableLayout(this.context);
          tableLayout.removeAllViews();
          provvisorio2.setStretchAllColumns(true);
          tableLayout.addView(provvisorio2);
  
          this.createTableRows(table, provvisorio);
          this.createTableRows(table, provvisorio2);
  
          ViewTreeObserver observer = provvisorio.getViewTreeObserver();
          observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  
              @Override
              public void onGlobalLayout() {
                  init(provvisorio, tableLayoutScrollable);
                  provvisorio.getViewTreeObserver().removeGlobalOnLayoutListener(this);
              }
          });
  
          ViewTreeObserver observer1 = provvisorio2.getViewTreeObserver();
          observer1.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
  
              @Override
              public void onGlobalLayout() {
                  init(provvisorio2, tableLayout);
                  provvisorio2.getViewTreeObserver().removeGlobalOnLayoutListener(this);
              }
          });
  
          // if (provvisorioWidth &lt;= width) {
          // provvisorio.setStretchAllColumns(true);
          // tableLayout.addView(provvisorio);
          // } else {
          // provvisorio.setStretchAllColumns(false);
          // tableLayoutScrollable.addView(provvisorio);
          // }
  
      }
  
      protected void init(TableLayout l, TableLayout tableLayout) {
          Display display = ((Activity) (this.context)).getWindowManager().getDefaultDisplay();
          Point size = new Point();
          display.getSize(size);
          int screenWidth = size.x;
          int b = l.getMeasuredWidthAndState();
          Toast.makeText(this.context, "Width schermo:" + screenWidth + " Widht view:" + b, Toast.LENGTH_SHORT).show();
          if (b &lt;= screenWidth) {
              tableLayout.setVisibility(View.GONE);
          } else {
              tableLayout.setVisibility(View.VISIBLE);
          }
      }
  
      public static class ViewHolder {
          // Child view
          TextView label = null;
          TextView value = null;
  
          TextView tableHeader = null;
  
          LinearLayout labelvalueContainer = null;
  
          HorizontalScrollView tableScroll = null;
  
          TableLayout tableLayout = null;
  
          TableLayout tableLayoutScrollable = null;
  
          // Group view
          TextView tv = null;
      }
  }
  </code></pre>