I have this tablayout with three fragments inside of an Appbar. When I change the current tab I want to change the background color of the appbar too. The code bellow is not working very well because not always the background color is setted correctly. I mean, the color that should be setted to artist is setted to track, or the album background color is setted in artist tab, something like that. And when I start the app no background color is setted.
How can I fix this?
colors.xml
main_activity.xml
MainActivity.java
ArtistFragment.java
AlbumFragment.java
I didn't post TrackFragment because is almost the same code as ArtistFragment and AlbumFragment.
How can I fix this?
colors.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="app_bar_artist">#FFF29312</color>
<color name="app_bar_album">#FFEDEFEB</color>
<color name="app_bar_track">#FF3E86A0</color>
</resources>
main_activity.xml
Code:
<android.support.design.widget.AppBarLayout
android:id="@+id/activity_main_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20">
<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/toolbar_layout"/>
<android.support.design.widget.TabLayout
android:id="@+id/activity_main_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/activity_main_app_bar_layout"
app:tabGravity="fill"
app:tabMode="fixed"/>
</android.support.design.widget.AppBarLayout>
MainActivity.java
Code:
public class MainActivity extends AppCompatActivity {
private AppBarLayout appbar;
// others implementations here but not related to the problem.
public void setTabLayout() {
ArtistFragment artistFragment = new ArtistaFragment();
AlbumFragment albumFragment = new AlbumFragment();
TrackFragment trackFragment = new TrackFragment();
artistFragment.setAppBarLayout(appbar);
albumFragment.setAppBarLayout(appbar);
trackFragment.setAppBarLayout(appbar);
viewPagerAdapter.addFragment(artistFragment, getResources().getString(R.string.fragment_artist_title));
viewPagerAdapter.addFragment(albumFragment, getResources().getString(R.string.fragment_album_title));
viewPagerAdapter.addFragment(trackFragment, getResources().getString(R.string.fragment_track_title));
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appbar = findViewById(R.id.activity_main_app_bar_layout);
// others implementations here but not related to the problem.
setTabLayout();
}
}
ArtistFragment.java
Code:
public class ArtistFragment extends Fragment {
private AppBarLayout appBarLayout;
public ArtistFragment() {}
public void setAppBarLayout(AppBarLayout appBarLayout) {
this.appBarLayout = appBarLayout;
}
private void setBackgroundColor() {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.app_bar_artist));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_artist, container, false);
setBackgroundColor();
return view;
}
}
AlbumFragment.java
Code:
public class AlbumFragment extends Fragment {
private AppBarLayout appBarLayout;
public AlbumFragment() {}
public void setAppBarLayout(AppBarLayout appBarLayout) {
this.appBarLayout = appBarLayout;
}
private void setBackgroundColor() {
appBarLayout.setBackgroundColor(getResources().getColor(R.color.app_bar_album));
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_album, container, false);
setBackgroundColor();
return view;
}
}
I didn't post TrackFragment because is almost the same code as ArtistFragment and AlbumFragment.