How to make Two activities with different colored status bar in Android.

There are so many situations, where we need to change the different action bar colors according toproject requirement . This example demonstrate about how to make Two activities with different colored status bar.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.



   

In the above code we have created on button when you click on button it going to call second activity.

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
   @Override
      protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(Color.parseColor("#FFFF00"));
         }
         setContentView(R.layout.activity_main);
         Button button = findViewById(R.id.click);
         button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent i = new Intent(MainActivity.this,Main2Activity.class);
               startActivity(i);
            }
         });
      }
   }
}

To change the status bar code we have used setStatusBarColor() as shown below -

getWindow().setStatusBarColor(Color.parseColor("#FFFF00"));

Step 4 − Add the following code to res/layout/activity_main2.xml.



Step 5 − Add the following code to src/MainActivity2.java

package com.example.andy.myapplication;

import android.graphics.Color;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.LOLLIPOP) {
         getWindow().setStatusBarColor(Color.parseColor("#B22222"));
      }
      setContentView(R.layout.activity_main2);
   }
}

Step 6 − Add the following code to manifest.java



   
      
         
            
            
         
      
      
   

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result, it indicates the first activity with yellow color status bar, now click on button, it will call second activity with red color status as shown below -

Click here to download the project code

Updated on: 2019-07-30T22:30:24+05:30

709 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements