Skip to content

Instantly share code, notes, and snippets.

@omatt
Last active August 29, 2015 14:05
Show Gist options
  • Save omatt/471a5a8b14b7ed744eb7 to your computer and use it in GitHub Desktop.
Save omatt/471a5a8b14b7ed744eb7 to your computer and use it in GitHub Desktop.
Custom Fragment Tabs reference
public class InfosLegalesActivity extends BaseDrawerNormalActivity {
private FragmentTabHost mTabHost;
private static TextView txtTitleInfosLegales;
@Override
protected int getLayoutContentId() {
return R.layout.layout_infoslegales;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initUI();
}
private void initUI() {
final Resources res = getResources();
Typeface fontIntRegularCondensed = OtherLibs.TypeFace(FontType.IS_REGULAR_CONDENSED, this);
txtTitleInfosLegales = (TextView)findViewById(R.id.txt_title_infoslegales);
txtTitleInfosLegales.setTypeface(fontIntRegularCondensed);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Infos legales",
getResources().getDrawable(android.R.drawable.star_on)),
InfosLegalesFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("CGV",
getResources().getDrawable(android.R.drawable.star_on)),
InfosLegalesCGVFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Regles de diffusion",
getResources().getDrawable(android.R.drawable.star_on)),
InfosLegalesReglesDeDiffusionFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator("Politique de confidentialité",
getResources().getDrawable(android.R.drawable.star_on)),
InfosLegalesPolitiqueDeConfidentialiteFragment.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab5").setIndicator("CGU Conditions Générales d'utilisation",
getResources().getDrawable(android.R.drawable.star_on)),
InfosLegalesCGUFragment.class, null);
mTabHost.getTabWidget().setStripEnabled(true);
mTabHost.getTabWidget().setRightStripDrawable(R.drawable.view_publierannonce_tab_indicator);
mTabHost.getTabWidget().setLeftStripDrawable(R.drawable.view_publierannonce_tab_indicator);
redrawTabHost();
TabWidget tabsInfosLegales = (TabWidget) findViewById(android.R.id.tabs);
LinearLayout linearLayoutTabParent = (LinearLayout) tabsInfosLegales.getParent();
float horizontalScrollWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 750, res.getDisplayMetrics());
final HorizontalScrollView horizontalScroll = new HorizontalScrollView(this);
horizontalScroll.setLayoutParams(new FrameLayout.LayoutParams((int) horizontalScrollWidth, FrameLayout.LayoutParams.WRAP_CONTENT));
linearLayoutTabParent.addView(horizontalScroll, 0);
linearLayoutTabParent.removeView(tabsInfosLegales);
horizontalScroll.addView(tabsInfosLegales);
horizontalScroll.setHorizontalScrollBarEnabled(false);
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
setSelectedTabColor();
float tabWidthFloat = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 750, res.getDisplayMetrics());
float tabWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 220, res.getDisplayMetrics());
int nrOfShownCompleteTabs = ((int) (Math.floor(tabWidthFloat / tabWidth) - 1) / 2) * 2;
int remainingSpace = (int) ((tabWidthFloat - tabWidth - (tabWidth * nrOfShownCompleteTabs)) / 2);
Log.e("INFOS LEGALES", "screen width PX= " + screenWidth);
float screenWidthPX = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, screenWidth, res.getDisplayMetrics());
Log.e("INFOS LEGALES", "screen width DP of PX= " + screenWidthPX);
int a = (int) (mTabHost.getCurrentTab() * tabWidth);
int b = (int) ((int) (nrOfShownCompleteTabs / 2) * tabWidth);
int offset = (a - b) - remainingSpace;
horizontalScroll.smoothScrollTo(offset, 0);
}
});
float layoutMarginHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, res.getDisplayMetrics());
float layoutWidthTab0 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 150, res.getDisplayMetrics());
float layoutWidthTab1 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, res.getDisplayMetrics());
float layoutWidthTab2 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, res.getDisplayMetrics());
float layoutWidthTab3 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 240, res.getDisplayMetrics());
float layoutWidthTab4 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 270, res.getDisplayMetrics());
mTabHost.getTabWidget().getChildAt(0).setLayoutParams(new LinearLayout.LayoutParams((int) layoutWidthTab0,(int) layoutMarginHeight));
mTabHost.getTabWidget().getChildAt(1).setLayoutParams(new LinearLayout.LayoutParams((int) layoutWidthTab1,(int) layoutMarginHeight));
mTabHost.getTabWidget().getChildAt(2).setLayoutParams(new LinearLayout.LayoutParams((int) layoutWidthTab2,(int) layoutMarginHeight));
mTabHost.getTabWidget().getChildAt(3).setLayoutParams(new LinearLayout.LayoutParams((int) layoutWidthTab3,(int) layoutMarginHeight));
mTabHost.getTabWidget().getChildAt(4).setLayoutParams(new LinearLayout.LayoutParams((int) layoutWidthTab4,(int) layoutMarginHeight));
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void redrawTabHost() {
// clear
for (int i = 0; i < 5; i++) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
mTabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);
} else {
mTabHost.getTabWidget().getChildTabViewAt(i).setBackground(null);
}
}
setSelectedTabColor();
}
@SuppressLint("NewApi")
private void setSelectedTabColor() {
Typeface fontIntLightCondensed = OtherLibs.TypeFace(FontType.IS_LIGHT_CONDENSED, this);
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
// change the background color
mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.view_publierannonce_tab_off);
// change the textcolor
//mTabHost.getTabWidget().getChildAt(i).to
TextView tv = (TextView) mTabHost.getTabWidget().getChildAt(i)
.findViewById(android.R.id.title);
tv.setTextColor(getResources().getColor(R.color.bleu_clair));
// tv.setTextColor(Color.parseColor("#4AB9E6"));
tv.setTextSize(16);
tv.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tv.setTypeface(fontIntLightCondensed);
}
// set the selected tab
mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundResource(R.drawable.img_bg_infolegales_tab_on);
TextView tvSelected = (TextView) mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).findViewById(android.R.id.title);
tvSelected.setTextColor(getResources().getColor(R.color.bleu_fonce));
tvSelected.setTextSize(16);
tvSelected.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
tvSelected.setTypeface(fontIntLightCondensed);
}
int currentTab = 0;
int nextTab = 0;
public void previousClicked(View view) {
currentTab = mTabHost.getCurrentTab();
Log.e("InfosLegalesActivity", "previousClicked");
if(currentTab > 0){
nextTab = currentTab - 1;
Log.e("InfosLegalesActivity", "Create Tab: " + nextTab);
mTabHost.setCurrentTab(nextTab);
currentTab--;
}
}
public void nextClicked(View view) {
currentTab = mTabHost.getCurrentTab();
Log.e("InfosLegalesActivity", "nextClicked");
if(currentTab < 4){
nextTab = currentTab + 1;
Log.e("InfosLegalesActivity", "Create Tab" + nextTab);
mTabHost.setCurrentTab(nextTab);
currentTab++;
}
}
}
/*
**
** Tab Fragment
**
*/
public class TabContainerFragment extends Fragment{
private View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.view_infoslegales_fragment, container, false);
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment