Reputation: 13165
I implemented tablayout but it is not working properly in emulator. The height is not showing properly in device. Can anybody tell me what the problem is?
this is my layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/black"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@android:id/tabs" />
<TabWidget android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</TabHost>
</LinearLayout>
this is my java code setting height and width
for (int i =0; i<tabWidget.getChildCount(); i++) {
//tabWidget.getChildAt(i).setBackgroundColor(R.color.black);
tabWidget.getChildAt(i).getLayoutParams().height = height;
tabWidget.getChildAt(i).getLayoutParams().width = width;
RelativeLayout relLayout = (RelativeLayout)tabWidget.getChildAt(i);
TextView tv = (TextView)relLayout.getChildAt(1);
tv.setTextSize(10.0f);
tv.setTypeface(myTypeface1);
}
Can anybody tell me how to set height and width for tab bar? Give sample code
Thanks
Upvotes: 0
Views: 946
Reputation: 5961
This is probably related to the difference in screen density between the emulator and your device. Check out DisplayMetrics - you need to get the density of the current device as follows:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
Next, calculate the necessary height and width scaling factor according to the section on density independent pixels on the Android dev site:
float scaling_factor = density / 160;
Finally, set your widget's height and width appropriately:
tabWidget.getChildAt(i).getLayoutParams().height = int ( height * scaling_factor );
tabWidget.getChildAt(i).getLayoutParams().width = int ( width * scaling_factor );
Upvotes: 1