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

AndroidApps/ApplicationsMobile Development

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.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout 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 = ".MainActivity"
   android:background = "#dde4dd"
   android:gravity = "center"
   android:orientation = "vertical">
   <Button
      android:id = "@+id/click"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content"
      android:text = "click for second"/>
</LinearLayout>

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.

<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   xmlns:app = "http://schemas.android.com/apk/res-auto"
   xmlns:tools = "http://schemas.android.com/tools"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".Main2Activity">
</android.support.constraint.ConstraintLayout>

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

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://schemas.android.com/apk/res/android"
   package = "com.example.andy.myapplication">
   <application
      android:allowBackup = "true"
      android:icon = "@mipmap/ic_launcher"
      android:label = "@string/app_name"
      android:roundIcon = "@mipmap/ic_launcher_round"
      android:supportsRtl = "true"
      android:theme = "@style/AppTheme">
      <activity
         android:name = ".MainActivity"
         android:screenOrientation = "portrait">
         <intent-filter>
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <activity android:name = ".Main2Activity"></activity>
   </application>
</manifest>

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

raja
Published on 29-Jan-2019 13:10:35
Advertisements