এই টিউটোরিয়ালে আমরা শিখব কীভাবে বাটনে ক্লিক করলে তার ব্যাকগ্রাউন্ড রঙ প্রোগ্রাম্যাটিক্যালি পরিবর্তন করা যায়।
প্রয়োজনীয় ধাপগুলো
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল তৈরি করুন
res/layout/activity_main.xml ফাইলে নিচের কোড যোগ করুন:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here to change color!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>
ধাপ ৩: মেইন এক্টিভিটিতে ক্লিক লিসেনার বাস্তবায়ন করুন
src/MainActivity.java ফাইলে নিচের কোড যোগ করুন:
package app.com.sample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
});
}
}
ধাপ ৪: ম্যানিফেস্ট কনফিগার করুন
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>
আধুনিক পদ্ধতি (API 23+)
API 23 (Marshmallow) থেকে getColor(int) ডিপ্রিকেটেড। নতুন পদ্ধতিঃ
// ContextCompat ব্যবহার করুন (সাপোর্ট লাইব্রেরি) button.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)); // অথবা সরাসরি (API 23+) button.setBackgroundColor(getColor(R.color.colorPrimary));
StateListDrawable ব্যবহার করে উন্নত সমাধান
ক্লিক স্টেট অনুযায়ী ভিন্ন রঙের জন্য res/drawable/button_selector.xml তৈরি করুন:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/colorPrimaryDark" />
<item android:state_focused="true" android:color="@color/colorAccent" />
<item android:color="@color/colorPrimary" /> <!-- ডিফল্ট -->
</selector>
তারপর লেআউটে: android:background="@drawable/button_selector"
রঙের রিসোর্স সংজ্ঞায়িত করা
res/values/colors.xml-এ রঙ সংজ্ঞায়িত করুন:
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
</resources>