I am following this tutorial to use colors in (my ActionBar and StatusBar) in Android Material design. I did follow the tutorial but no color is reflected, and the same dark ActionBar and StatusBar are shown when I run the app in my API22
emulator, while color is shown in the ActionBar of Pre-lollipop devices The question is why and how can I fix this?
minimumSdkVersion
is 8
and targetSdkVersion
is 22
. It compiles with 5.1.1 API 22
. The emulator to test for Lollipop devices is Nexus One but customised to run API 22
, whereas the emulator to test for Pre-lollipop devices is running API 08
MainActivity.java
public class MainActivity extends ActionBarActivity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= 21) {
getWindow().setStatusBarColor(getResources().getColor(R.color.primaryColorDark));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primaryColor">#FF5722</color>
<color name="primaryColorDark">#E64A19</color>
<color name="accentColor">#9C27B0</color>
</resources>
res/values/styles.xml
<resources>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar" ></style>
<style name="AppTheme" parent="AppTheme.Base">
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryDark">@color/primaryColorDark</item>
<item name="colorAccent">@color/accentColor</item>
</style>
</resources>
The same (as ^) code is there in res/values-v11/styles.xml
and res/values-14/styles.xml
.
res/values-v21/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:colorPrimary">@color/primaryColor</item>
<item name="android:colorPrimaryDark">@color/primaryColorDark</item>
<item name="android:colorAccent">@color/accentColor</item>
</style>
</resources>
The same (as ^) code is there in res/values-v22/styles.xml
.
EDIT The left one is running API 22, and the right one is running API 08.