Reputation: 39
How can I define a different Statusbar and Actionbar color for each fragment ?
At the moment.
How it should look.
Upvotes: 1
Views: 1814
Reputation: 4617
First of all, I would highly recommend you to migrate to new approach - Toolbar. It is much more flexible and you can customize it as plain View
.
About your question.
You can just get ActionBar
object and setBackground
programatically.
Here is short example
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable("COLOR IN HEX 0xFFFF6666 for instance"));
I will show how would I implement this. This is more about architecture and patterns.
Use some base class for Fragment
it would be better to have base class for Activity
as well. Lets consider
public class BaseFragment extends Fragment
And you Activity
class in which your fragment lives.
public class MainActivity extends Activity
And you have to define responsibilities of Activity
in this case and create interfaces
In your case to work with ActionBar
Create interface
public interface ActionBarProvider {
void setActionBarColor(ColorDrawable color);
}
Make your activity implement this interface
public class MainActivity extends Activity implements ActionBarProvider {
public void setActionBarColo(ColorDrawable color) {
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(color));
}
}
And finally in BaseFragment
in onAttach
public void onAttach(Context context) {
super.onAttach(context);
mActionBarProvider = (ActionBarProvider) context;
}
Make mActionBarProvider
variable protected and make each fragment extend BaseFragment
and you can change action bar color from any fragment like this mActionBarProvider.setActionBarColor(new ColorDrawable());
Hope this helps.
Upvotes: 1