3

I am in a condition that my app needs TabHost using only java code not the xml . But i need the TabHost height to be set 20dp. Is that possible using only java code?

Please help?

Navdroid
  • 4,453
  • 7
  • 29
  • 47

3 Answers3

3

I think this can help you:

TabHost tabHost = new TabHost(context);
tabHost.setLayoutParams(new ViewGroup.LayoutParams(YOUR_WIDTH_HERE, YOUR_HEIGHT_HERE));

You can use ViewGroup.LayoutParams.FILL_PARENT or WRAP_CONTENT or MATCH_PARENT

Cata
  • 11,133
  • 11
  • 65
  • 86
  • Then you have to change the LayoutParams class type from ViewGroup.LayoutParams to other class... if you pay attention to your classCastException, I think the error tell you what is the expected LayoutParams class.. – Cata Feb 04 '12 at 11:44
  • can u tell me how can i display same tabhost at bottom of the screen using java? – Navdroid Feb 04 '12 at 12:27
  • the best approach is to put the tab host into a RelativeLayout and then do something like this: ((RelativeLayout.LayoutParams)tabHost.getLayoutParams()).layout_alignParentBottom = true; - I didn't tried this code but it might help.. – Cata Feb 04 '12 at 12:35
  • I am new to this can u please elaborate how to put the tabhost in layout? – Navdroid Feb 04 '12 at 12:45
  • I think you have to open a new question for this otherwise I can't help you more.. – Cata Feb 04 '12 at 12:46
  • http://stackoverflow.com/questions/9141019/how-to-set-a-tabhost-at-bottom-of-the-screen-using-only-java new question please help? – Navdroid Feb 04 '12 at 12:53
1

Try this. hope it may be helpful

tabHost.getTabWidget().getChildAt(index).getLayoutParams().height =(int) height;
Dray
  • 1,904
  • 5
  • 27
  • 42
  • Do you know how can i display this tabhhost at bottom of the screen..using only java!! – Navdroid Feb 04 '12 at 12:26
  • or check this. it ll help you http://stackoverflow.com/questions/2395661/android-tabs-at-the-bottom – Dray Feb 06 '12 at 10:54
1

For any view you can provide custom width and heiht by providing LayoutParams. All sizies in LayoutParams set in pixels. If you want to use dp - use this code:

float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 14, getResources().getDisplayMetrics());
Jin35
  • 8,602
  • 3
  • 32
  • 52