Here are my 2 cents and it works for me ...
Add this first to your oncreate
TabHost.TabSpec spec; // Resusable TabSpec for each tab Intent intent; // Reusable Intent for each tab MyView view = null;
and then inside your creation, create it for each tab ...
intent = new Intent().setClass(this, BrownieTab.class); view = new MyView(this, R.drawable.ic_tab_brownie_2, R.drawable.ic_tab_brownie_2, "Yourself"); view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground)); spec = tabHost.newTabSpec("inprogress").setIndicator(view).setContent(intent); tabHost.addTab(spec);
Finally, here is MyView ...
private class MyView extends LinearLayout { ImageView iv; TextView tv; public MyView(Context c, int drawable, int drawableselec, String label) { super(c); iv = new ImageView(c); StateListDrawable listDrawable = new StateListDrawable(); listDrawable.addState(SELECTED_STATE_SET, this.getResources() .getDrawable(drawableselec)); listDrawable.addState(ENABLED_STATE_SET, this.getResources() .getDrawable(drawable)); iv.setImageDrawable(listDrawable); iv.setBackgroundColor(Color.TRANSPARENT); iv.setLayoutParams(new LayoutParams(48, 48, (float) 0.0)); setGravity(Gravity.CENTER); tv = new TextView(c); tv.setText(label); tv.setGravity(Gravity.CENTER); tv.setBackgroundColor(Color.TRANSPARENT); tv.setTextColor(Color.BLACK); tv.setTextSize(12); tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, (float) 1.0)); setOrientation(LinearLayout.VERTICAL); addView(iv); addView(tv); setBackgroundDrawable(this.getResources().getDrawable( R.layout.selecttabbackground)); } }
you can change all tab attributes in my view and change text and background in oncreate methods
source share