Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Zarah

Thread: No color is shown on my Material Design ActionBar or StatusBar. Why?

  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default No color is shown in Lollipop devices, but shows in Pre-Lollipop devices. Why?

    I am followingthis 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

    Java Code:
    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

    Java Code:
    <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

    Java Code:
    <?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

    Java Code:
    <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

    Java Code:
    <?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.
    Last edited by Zarah; 08-04-2015 at 01:04 PM.

  2. #2
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: No color is shown on my Material Design ActionBar or StatusBar. Why?

    Changing the onCreate() to the following solved the problem:

    Java Code:
    @SuppressLint("NewApi")
    	@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            	Window window = getWindow();
            	window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            	window.setStatusBarColor(getResources().getColor(R.color.primaryColorDark)); 
            }
            
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources() 
            		.getColor(R.color.primaryColor)));
            
        }
    DarrylBurke likes this.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: No color is shown on my Material Design ActionBar or StatusBar. Why?

    Thank you for sharing the solution.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: No color is shown on my Material Design ActionBar or StatusBar. Why?

    @DarrylBurke Yes, it might help some future visitor =)

Similar Threads

  1. No result shown
    By Levian in forum JDBC
    Replies: 18
    Last Post: 07-04-2011, 02:53 AM
  2. Anyone suggest and reading material on OOP design
    By Jojomofoman in forum New To Java
    Replies: 0
    Last Post: 01-27-2011, 11:33 PM
  3. Want to call tray message within panel or say statusbar
    By alok_sharma in forum AWT / Swing
    Replies: 0
    Last Post: 08-31-2010, 10:07 AM
  4. No output shown
    By ai_2007 in forum Advanced Java
    Replies: 4
    Last Post: 07-10-2007, 09:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •