I wanted to have different coloured action bars in each activity but after searching on the internet, I couldn't find anything. I was wondering if this is even possible and if so how?
Asked
Active
Viewed 30 times
0
-
check this answer http://stackoverflow.com/a/17198657/4848308 – Gueorgui Obregon Feb 23 '16 at 20:39
-
Possible duplicate of [Change actionbar color programmatically more than once](http://stackoverflow.com/questions/17076958/change-actionbar-color-programmatically-more-than-once) – Gueorgui Obregon Feb 23 '16 at 20:48
1 Answers
0
Suppose you need a parent activity with setActionBarColor
method.
public class ParentActivity extends ActionBarActivity/*or similar*/ {
public void onCreate(Bundle state) {
super.onCreate(state);
}
public void setActionBarColor(Color color) {
getActionBar().setBackgroundDrawable(new ColorDrawable(color));
}
}
And extends each activity from this
public class ActivityA extends ParentActivity {
@Override
public void onCreate(Bundle state) {
//call ParentActivity.onCreate(state)
super.onCreate(state);
setContentView(R.layout.activity_a);
// some initialization code
setActionBarColor(Color.rgb(248, 248, 248));
}
}
public class ActivityB extends ParentActivity {
@Override
public void onCreate(Bundle state) {
// some initialization code
setActionBarColor(Color.rgb(248, 248, 248));
}
}

Sergey Shustikov
- 15,377
- 12
- 67
- 119
-
The activity seems to crash on launch. I'm I right in thinking that the second bit of code goes in the actual activity? – Zak Woolley Feb 23 '16 at 20:49
-
@ZakWoolley here is a inheritance principle in action. When you start each activity it will call parent methods, without crash if you will not use objects related to childs. – Sergey Shustikov Feb 23 '16 at 20:56
-
@ZakWoolley yes. Actually here is a your activities, that you was declared in manifest and implemented. – Sergey Shustikov Feb 23 '16 at 20:56
-
I don't think I quite understand what you're saying. Have I got to call the parent activities' on create method from the main activities' on create? – Zak Woolley Feb 23 '16 at 21:29
-