অ্যান্ড্রয়েড 5.0+ (Lollipop) এ স্ট্যাটাসবার কালার প্রোগ্রামেটিক্যালি সেট করা
এই উদাহরণটি দেখাবে কীভাবে Window API ব্যবহার করে স্ট্যাটাসবারর কালার অ্যাপের থিম/অ্যাকসেন্ট কালারের সাথে মিলিয়ে পরিবর্তন করা যায়।
ধাপ ১: প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project নির্বাচন করুন।
ধাপ ২: লেআউট (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="30dp"
android:text="Check the Color of the Status Bar"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
ধাপ ৩: মেইন এক্টিভিটি (src/MainActivity.java)
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Window window = MainActivity.this.getWindow();
// সিস্টেমবার ব্যাকগ্রাউন্ড আঁকার অনুমতি দিন
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// ট্রান্সলूसেন্ট স্ট্যাটাসবার ক্লিয়ার করুন
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// স্ট্যাটাসবার কালার সেট করুন (res/values/colors.xml থেকে)
window.setStatusBarColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent));
}
}
ধাপ ৪: কালার ডিফাইন (res/values/colors.xml)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
ধাপ ৫: মেনিফেস্ট (androidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
অতিরিক্ত টিপস
- থিমের মাধ্যমে গ্লোবালি সেট করতে
styles.xml-এ<item name="android:statusBarColor">@color/colorPrimaryDark</item>যোগ করুন (API 21+). - ন্যাভিগেশনবার কালার:
window.setNavigationBarColor(...)(API 21+). - লাইট/ডার্ক আইকন:
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR)(API 23+).
ফলাফল
অ্যাপ্লিকেশন চালু হলে স্ট্যাটাসবার অ্যাকসেন্ট কালার (#FF4081) এ রঙিন হবে।
