0

I tried implementing FragmentTabHost it works but i want to animate when i click the tabs.

Here's a snippet of my tab host:

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
        @Override
        public void onTabChanged(String tabId) {
            View currentView = mTabHost.getCurrentView();

            if (mTabHost.getCurrentTab() > currentTab) {
                currentView.setAnimation(inFromRightAnimation());
            } else {
                currentView.setAnimation(outToRightAnimation());
            }

            currentTab = mTabHost.getCurrentTab();
        }
    });

    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("Tab 1").setIndicator("Tab 1", null),
            PostSoovyFragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("Tab 2").setIndicator("Tab 2", null),
            SooviesFragment.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("Tab 3").setIndicator("Tab 3", null),
            SettingsFragment.class, null);

These here are the animations that i am using:

private Animation inFromRightAnimation() {
    Animation inFromRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, +1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    inFromRight.setDuration(240);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
}

private Animation outToRightAnimation() {
    Animation outtoLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, -1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
    outtoLeft.setDuration(240);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
}

Sadly when i test this on my phone which is 4.4.4. There is no animation between tabs.

Jan
  • 3,393
  • 4
  • 21
  • 47
  • Instead you try this [Tabs](http://developer.android.com/training/implementing-navigation/lateral.html) and this [Animations](http://developer.android.com/training/animation/screen-slide.html#pagetransformer) – Harin Mar 25 '15 at 06:54
  • @Harry I tried considering that but one of my tabs has a google map. Since the design is that it fills the whole tabcontent, it will conflict with the gestures of the map – Jan Mar 25 '15 at 07:03
  • Is it working on ICS? – Harin Mar 25 '15 at 07:34
  • @Harry I haven't tested it but the Animation class should be compatible on 4.0+ devices – Jan Mar 25 '15 at 07:40
  • try answer in this [link](http://stackoverflow.com/questions/14352954/how-to-implement-tabhost-sliding-effect) – Harin Mar 25 '15 at 08:00
  • @Harry code is just the same – Jan Mar 25 '15 at 10:25

0 Answers0