0

i'm working FragmentTabHost.i created FragmentTabHost and i can replace some fragments.now i want to change background color in FragmentTabHost and also text color.for example i want background color black and text color white.this is a my xml code

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ff0000"/>

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

and this is a my java code

 public class CustomTabActivity extends FragmentActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

        mTabHost.addTab(
                mTabHost.newTabSpec("tab1").setIndicator("Tab 1", null),
                StradaMenu.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2").setIndicator("Tab 2", null),
                StradaMenu.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("tab3").setIndicator("Tab 3", null),
                StradaMenu.class, null);
    }

}

How i can solve my problem ? if anyone knows solution please help me thanks

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
baggio
  • 11
  • 8

1 Answers1

0

You have to use the Action bar tabs to get the different color for each tabs.

Check out this

MainActivity.java:

public class MainActivity extends FragmentActivity {
 static ViewPager Tab;
 TabsPagerAdapter TabAdapter;
 ActionBar actionBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TabAdapter = new TabsPagerAdapter(getSupportFragmentManager());

    Tab = (ViewPager) findViewById(R.id.pager);

    Tab.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar = getActionBar();
            actionBar.setSelectedNavigationItem(position);
        }
    });

    Tab.setAdapter(TabAdapter);
    actionBar = getActionBar();
    // Enable Tabs on Action Bar
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        @Override
        public void onTabReselected(android.app.ActionBar.Tab tab,
                FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            Tab.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(android.app.ActionBar.Tab tab,
                FragmentTransaction ft) {
            // TODO Auto-generated method stub
        }
    };

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    ActionBar.Tab tab = actionBar.newTab().setText("Home")
            .setTabListener(new TabListener(this, Home.class.getName()));
    View tabView = inflater.inflate(R.layout.fragment_home, null);
    tabView.setBackgroundResource(R.drawable.gradient_shape); // set custom
                                                                // color

    tab.setCustomView(tabView);
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("News")
            .setTabListener(new TabListener(this, News.class.getName()));
    View tabView2 = inflater.inflate(R.layout.fragment_news, null);
    tabView2.setBackgroundResource(R.drawable.gradient_shape2); // set
                                                                // custom
                                                                // color
    tab.setCustomView(tabView2);
    actionBar.addTab(tab);

    tab = actionBar.newTab().setText("Latest")
            .setTabListener(new TabListener(this, Latest.class.getName()));
    View tabView3 = inflater.inflate(R.layout.fragment_latest, null);
    tabView3.setBackgroundResource(R.drawable.gradient_shape3); // set
                                                                // custom
                                                                // color
    tab.setCustomView(tabView3);
    actionBar.addTab(tab);

  }

public static class TabListener extends Fragment implements
        ActionBar.TabListener {

    public TabListener(MainActivity mainActivity, String name) {
        // this(mainActivity,name);
    }

    @Override
    public void onTabSelected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {
        Tab.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {

    }

    @Override
    public void onTabReselected(android.app.ActionBar.Tab tab,
            FragmentTransaction ft) {

    }

}

}
Community
  • 1
  • 1
Stephen
  • 9,899
  • 16
  • 90
  • 137