Change color notification bar android studio

0

I want that depending on the activity the notification bar (where the battery comes out, signal, etc.) change color.

For example, if I am in Activity_main this bar is blue and if I am in Activity_about it should be orange.

Otherwise, how can I change the hexadecimal in colors.xml programmatically?

    
asked by César Alejandro M 07.01.2018 в 14:06
source

1 answer

1

If all you want is to change the color of the notification bar when you change your activity, you just have to apply a different theme in each activity, which you can do from xml.

The first thing is to define the themes that you will use in the activities, they can be very similar topics in which you only change the color of the notification bar. The property that modifies this color is colorPrimaryDark . The themes are defined in the styles.xml file.

<!--Thema por defecto-->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<!--Tema de la SegundaActivity-->
<style name="ThemeSegundaActivity" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@android:color/holo_orange_light</item> <!--Color naranja-->
    <item name="colorAccent">@color/colorAccent</item>
</style>

Then in the AndroidManifest.xml you assign a different theme to each activity.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="davidminaya.respuestasstackoverflow">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="Respuestas Stack Overflow"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".SegundaActivity"
            <!--Con esta sentencia le asignas el tema a la actividad-->
            android:theme="@style/ThemeSegundaActivity">
        </activity>

    </application>

</manifest>

In this case the color of the notification bar in the SecondActivity will be orange.

  

Keep in mind that if you do not assign a topic to the activity, the default theme of this is the theme of the application.

    
answered by 07.01.2018 / 15:38
source