gorgi93
gorgi93

Reputation: 2535

Change android actionbar tab text color on orientation change programaticly?

I want to change color of text in tabs. How can I reference the tab layout in which I want to change color property inside of the function:

public void onConfigurationChanged(Configuration newConfig) 
{
   findViewById(R.id.tab_textview); // returns null
}

Since this returns null. tab_textview is the template for the tab. In the onCreate I just put tabs inside the actionbar and everything works. I just need to change color when orientation is changed so the text is white and visible. Find many similar problems but I cant get it to work. I am very new to android programming.

Upvotes: 0

Views: 608

Answers (1)

CodeDiving
CodeDiving

Reputation: 183

At the onCreate method, we initial the ActionBar like this:

ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    Tab tab = actionBar
            .newTab()
            .setCustomView(R.layout.tab_textview) // use our TextView
            .setTabListener(
                    new Chapter1TabListener<FragmentA>(this, "fragmentA",
                            FragmentA.class));
    TextView tabview = (TextView) tab.getCustomView();
    tabview.setText("First Tab");
    actionBar.addTab(tab);
    tab = actionBar
            .newTab()
            .setCustomView(R.layout.tab_textview)
            .setTabListener(
                    new Chapter1TabListener<FragmentB>(this, "fragmentB",
                            FragmentB.class));
    tabview = (TextView) tab.getCustomView();
    tabview.setText("Second Tab");
    actionBar.addTab(tab);

Override onConfigurationChanged, try as following:

super.onConfigurationChanged(newConfig);
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        ActionBar actionBar = getActionBar();
        for(int i=0; i<actionBar.getTabCount(); i++ ) {
            Tab tab = actionBar.getTabAt(i);
            TextView tv = (TextView) tab.getCustomView();
            tv.setTextColor(getResources().getColor(android.R.color.holo_blue_dark));
        }
    }

Upvotes: 1

Related Questions