Reputation:
I'm sorry if there is already an answer to this but i havn't found anything that could help me.
I have created my tabs like so:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
String account_information;
TabLayout tabLayout;
Window window;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
//mViewPager.setOnTouchListener(View.OnTouchListener());
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setSelectedTabIndicatorColor(getColor(R.color.BLACK));
account_information = getIntent().getStringExtra("CURRENT_ACCOUNT");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
//returning the current tabs
switch (position) {
case 0:
contactstab_main contactstab1 = new contactstab_main();
return contactstab1;
case 1:
dashboardtab_main dashboardtab2 = new dashboardtab_main();
return dashboardtab2;
case 2:
dropdowntab_main dropdowntab3 = new dropdowntab_main();
Bundle bundle = new Bundle();
bundle.putString("current_account", account_information);
dropdowntab3.setArguments(bundle);
return dropdowntab3;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "CONTACTS";
case 1:
return "DASHBOARD";
case 2:
return "PROFILE-DROPDOWN";
}
return null;
}
}
}
Any information I have found about changing the colour of the tabs is only for selected and unselected tabs but I want to have each ?Tabtitlebackground? to be in a different colour so that you can see the tabbar with 3 colors and each one is representing one tab.
Upvotes: 0
Views: 35
Reputation: 54194
Doing this relies on undocumented code, so it could break... but I doubt it's likely to.
A TabLayout
hosts a private class called SlidingTabStrip
(which extends from LinearLayout
). Each child of the SlidingTabStrip
is a TabView
, i.e. the view that represents a single tab. So you can write code like this to set the background color of the various tabs:
ViewGroup tabStrip = (ViewGroup) tabLayout.getChildAt(0);
int[] colors = new int[] { 0xffccaaff, 0xffffccaa, 0xffaaffcc };
for (int i = 0; i < tabStrip.getChildCount(); i++) {
tabStrip.getChildAt(i).setBackgroundColor(colors[i]);
}
Upvotes: 1