Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I figured out a way to get the result I wanted, just not the exact way I was trying and never managed to get the height of the <code>tabcontent</code> directly but indirectly.</p> <p>I found two methods to do this and I'll post them below.</p> <p>First I did <strong>Method 2</strong> but then discovered I preferred <strong>Method 1</strong> and decided to go with that since it's more extendable.</p> <p><strong>Method 1</strong></p> <p>This way I found from <a href="https://stackoverflow.com/questions/2159320/how-to-size-an-android-view-based-on-its-parents-dimensions">How to size an Android view based on its parent&#39;s dimensions</a> and is the most customizable and readable method. In a nut shell, you need to extend <code>FrameLayout</code> and override the <code>onMeasure</code> method.</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0dip" android:layout_margin="0dip"&gt; &lt;LinearLayout android:id="@+id/topLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0dip" android:layout_margin="0dip"&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="50dp" android:padding="0dip" android:layout_margin="0dip" /&gt; &lt;view class="com.tmcaz.patch.TabContentFrameLayout" android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0dip" android:layout_margin="0dip" /&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p>The major difference is using a custom class for this where you can handle the sizing from the event itself, similar to <strong>Method 2</strong> but no need to do any calculations to get the content height.</p> <p>I did this to give myself access to the event and handle all of the sizing in the content. Someone reading this may very well need to override something else and deal with the <code>onMeasure</code> event totally differently.</p> <p>The code is below</p> <pre><code>public class TabContentFrameLayout extends FrameLayout { // add constructors, etc @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ // Should turn these in to member variables and reference it throughout. int parentWidth = MeasureSpec.getSize(widthMeasureSpec); int parentHeight = MeasureSpec.getSize(heightMeasureSpec); this.setMeasuredDimension(parentWidth, parentHeight); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } </code></pre> <p><strong>Method 2</strong></p> <p>I assigned an <code>id</code> to the <code>LinearLayout</code> that held the <code>TabWidget</code> and <code>FrameLayout</code>. Here is my main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0dip" android:layout_margin="0dip"&gt; &lt;LinearLayout android:id="@+id/topLayout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0dip" android:layout_margin="0dip"&gt; &lt;TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="50dip" android:padding="0dip" android:layout_margin="0dip" /&gt; &lt;FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="0dip" android:layout_margin="0dip" /&gt; &lt;/LinearLayout&gt; &lt;/TabHost&gt; </code></pre> <p>I assigned a DIP height to the <code>tabs</code> and then grabbed the <code>LayoutParams</code> for the <code>LinearLayout</code> which I simply subtract the height of the <code>tabs</code> from the result. I've added code here for basic illustrative purposes only and can do it a bit more efficiently, it's not my production code :)</p> <p>One thing to note is that you can't appear to directly pull the height of the layout during the onCreate event where it's most useful. You need to create a <code>GlobalLayoutListener</code> to capture the change in the layout and get the size.</p> <pre><code>public class MyTabActivity extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LinearLayout ll = (LinearLayout)findViewById(R.id.topLayout); ll.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { public void onGlobalLayout() { DisplayLayoutDimensions(); } } ); // Code here to add activities to the tabs, etc } </code></pre> <p>.</p> <pre><code>public void DisplayLayoutDimensions() { // Put code to calculate the heights you need instead of printing // out the values obviously. Resources r = getResources(); LinearLayout topLayout = (LinearLayout)findViewById(R.id.topLayout); LayoutParams tabWidgetParams = getTabHost().getTabWidget().getLayoutParams(); float scale = r.getDisplayMetrics().density; float pxTabContent = topLayout.getHeight() - tabWidgetParams.height; /*** The commented out DIP calculations didn't work for me in any AVD so manually I calculated them with the scale which worked fine ***/ //float dipTopLayout = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, topLayout.getHeight(), r.getDisplayMetrics()); //float dipTabWidget = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, tabWidgetParams.height, r.getDisplayMetrics()); //float dipTabContent = dipTopLayout - dipTabWidget; Log.d("MyTabActivity", "LinearLayout (topLayout) Height: " + topLayout.getHeight() + "px / " + (topLayout.getHeight() / scale) + "dp"); Log.d("MyTabActivity", "TabWidget Height: " + tabWidgetParams.height + "px / " + (tabWidgetParams.height / scale) + "dp"); Log.d("MyTabActivity", "Calculated (tabcontent) Height: " + pxTabContent + "px / " + (pxTabContent / scale) + "dp"); } </code></pre> <p>Hope this helps someone at some point. If someone has a better way to do this, please speak up.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload