টগল বোতাম যোগ করুন
সেভ করা পৃষ্ঠা গুছিয়ে রাখতে 'সংগ্রহ' ব্যবহার করুন
আপনার পছন্দ অনুযায়ী কন্টেন্ট সেভ করুন ও সঠিক বিভাগে রাখুন।
রচনা পদ্ধতি চেষ্টা করুন
জেটপ্যাক কম্পোজ হল Android এর জন্য প্রস্তাবিত UI টুলকিট। কম্পোজে উপাদান যোগ করতে শিখুন।
আপনি যদি একটি View
-ভিত্তিক লেআউট ব্যবহার করেন তবে টগলগুলি বাস্তবায়নের জন্য তিনটি প্রধান পছন্দ রয়েছে। আমরা উপাদান উপাদান লাইব্রেরি থেকে SwitchMaterial
উপাদান ব্যবহার করার পরামর্শ দিই:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<com.google.android.material.switchmaterial.SwitchMaterial
android:id="@+id/material_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/material_switch"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
লিগ্যাসি অ্যাপগুলি এখনও পুরানো SwitchCompat
AppCompat উপাদান ব্যবহার করতে পারে, যেমনটি নিম্নলিখিত উদাহরণে দেখানো হয়েছে:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<androidx.appcompat.widget.SwitchCompat
android:id="@+id/switchcompat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/switchcompat"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
নিম্নলিখিত উদাহরণটি AppCompatToggleButton
দেখায়, যেটি আরেকটি লিগ্যাসি উপাদান যার একটি লক্ষণীয়ভাবে ভিন্ন UI রয়েছে:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/toggle_button_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/toggle"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintBaseline_toBaselineOf="@id/toggle"
android:text="@string/toggle_button" />
<androidx.appcompat.widget.AppCompatToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/toggle_button_label"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
এই তিনটি উপাদান একই আচরণ অফার করে কিন্তু দেখতে ভিন্ন। SwitchMaterial
এবং SwitchCompat
মধ্যে পার্থক্য সূক্ষ্ম, কিন্তু AppCompatToggleButton
লক্ষণীয়ভাবে ভিন্ন:
চিত্র 1. তিনটি টগল বোতাম প্রকার।
রাষ্ট্র পরিবর্তন হ্যান্ডেল
SwitchMaterial
, SwitchCompat
, এবং AppCompatToggleButton
হল CompoundButton
এর সমস্ত উপশ্রেণী, যা তাদের চেক করা অবস্থার পরিবর্তনগুলি পরিচালনা করার জন্য একটি সাধারণ প্রক্রিয়া দেয়৷ আপনি CompoundButton.OnCheckedChangeListener
এর একটি উদাহরণ বাস্তবায়ন করুন এবং এটিকে বোতামে যুক্ত করুন, যেমনটি নিম্নলিখিত উদাহরণে দেখানো হয়েছে:
কোটলিন
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: SwitchLayoutBinding = SwitchLayoutBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.materialSwitch.setOnCheckedChangeListener { _, isChecked ->
if (isChecked) {
// The switch is checked.
} else {
// The switch isn't checked.
}
}
}
}
জাভা
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SwitchLayoutBinding binding = SwitchLayoutBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.materialSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
if (isChecked) {
// The switch is checked.
} else {
// The switch isn't checked.
}
});
}
}
CompoundButton.OnCheckedChangeListener
হল একটি একক বিমূর্ত পদ্ধতি ইন্টারফেস (বা SAM ইন্টারফেস ), তাই আপনি এটিকে ল্যাম্বডা হিসাবে প্রয়োগ করতে পারেন। যখনই চেক করা অবস্থা পরিবর্তিত হয় তখনই ল্যাম্বডা বলা হয়, এবং ল্যাম্বডাকে পাস করা isChecked
বুলিয়ানের মান নতুন চেক করা অবস্থা নির্দেশ করে।
এই পৃষ্ঠার কন্টেন্ট ও কোডের নমুনাগুলি Content License-এ বর্ণিত লাইসেন্সের অধীনস্থ। Java এবং OpenJDK হল Oracle এবং/অথবা তার অ্যাফিলিয়েট সংস্থার রেজিস্টার্ড ট্রেডমার্ক।
2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।
[[["সহজে বোঝা যায়","easyToUnderstand","thumb-up"],["আমার সমস্যার সমাধান হয়েছে","solvedMyProblem","thumb-up"],["অন্যান্য","otherUp","thumb-up"]],[["এতে আমার প্রয়োজনীয় তথ্য নেই","missingTheInformationINeed","thumb-down"],["খুব জটিল / অনেক ধাপ","tooComplicatedTooManySteps","thumb-down"],["পুরনো","outOfDate","thumb-down"],["অনুবাদ সংক্রান্ত সমস্যা","translationIssue","thumb-down"],["নমুনা / কোড সংক্রান্ত সমস্যা","samplesCodeIssue","thumb-down"],["অন্যান্য","otherDown","thumb-down"]],["2025-07-29 UTC-তে শেষবার আপডেট করা হয়েছে।"],[],[],null,["# Add toggle buttons\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to add components in Compose. \n[Switch →](/develop/ui/compose/components/switch) \n\n\u003cbr /\u003e\n\nIf you're using a `View`-based layout, there are three main choices for\nimplementing toggles. We recommend using the\n[`SwitchMaterial`](https://m3.material.io/components/switch/overview) component\nfrom the [Material\nComponents](https://m3.material.io/develop/android/mdc-android) library: \n\n \u003candroidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:padding=\"16dp\"\u003e\n\n \u003ccom.google.android.material.switchmaterial.SwitchMaterial\n android:id=\"@+id/material_switch\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"@string/material_switch\"\n app:layout_constraintEnd_toEndOf=\"parent\"\n app:layout_constraintStart_toStartOf=\"parent\"\n app:layout_constraintTop_toTopOf=\"parent\" /\u003e\n\n \u003c/androidx.constraintlayout.widget.ConstraintLayout\u003e\n\nLegacy apps might still use the older\n[`SwitchCompat`](/reference/androidx/appcompat/widget/SwitchCompat) AppCompat\ncomponent, as shown in the following example: \n\n \u003candroidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:padding=\"16dp\"\u003e\n\n \u003candroidx.appcompat.widget.SwitchCompat\n android:id=\"@+id/switchcompat\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n android:text=\"@string/switchcompat\"\n app:layout_constraintEnd_toEndOf=\"parent\"\n app:layout_constraintStart_toStartOf=\"parent\"\n app:layout_constraintTop_toTopOf=\"parent\" /\u003e\n\n \u003c/androidx.constraintlayout.widget.ConstraintLayout\u003e\n\nThe following example shows\n[`AppCompatToggleButton`](/reference/androidx/appcompat/widget/AppCompatToggleButton),\nwhich is another legacy component that has a noticeably different UI: \n\n \u003candroidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:app=\"http://schemas.android.com/apk/res-auto\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:padding=\"16dp\"\u003e\n\n \u003cTextView\n android:id=\"@+id/toggle_button_label\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n app:layout_constraintStart_toStartOf=\"parent\"\n app:layout_constraintEnd_toStartOf=\"@id/toggle\"\n app:layout_constraintHorizontal_chainStyle=\"packed\"\n app:layout_constraintBaseline_toBaselineOf=\"@id/toggle\"\n android:text=\"@string/toggle_button\" /\u003e\n\n \u003candroidx.appcompat.widget.AppCompatToggleButton\n android:id=\"@+id/toggle\"\n android:layout_width=\"wrap_content\"\n android:layout_height=\"wrap_content\"\n app:layout_constraintEnd_toEndOf=\"parent\"\n app:layout_constraintStart_toEndOf=\"@id/toggle_button_label\"\n app:layout_constraintTop_toTopOf=\"parent\"\n app:layout_constraintBottom_toBottomOf=\"parent\"/\u003e\n\n \u003c/androidx.constraintlayout.widget.ConstraintLayout\u003e\n\nThese three components offer the same behavior but look different. The\ndifferences between the `SwitchMaterial` and `SwitchCompat` are subtle, but\n`AppCompatToggleButton` is noticeably different:\n\n\n**Figure 1.** Three toggle button types.\n\n\u003cbr /\u003e\n\n### Handle state changes\n\n`SwitchMaterial`, `SwitchCompat`, and `AppCompatToggleButton` are all subclasses\nof [`CompoundButton`](/reference/android/widget/CompoundButton), which\ngives them a common mechanism for handling checked state changes. You implement\nan instance of\n[`CompoundButton.OnCheckedChangeListener`](/reference/android/widget/CompoundButton.OnCheckedChangeListener)\nand add it to the button, as shown in the following example: \n\n### Kotlin\n\n```kotlin\nclass MainActivity : AppCompatActivity() {\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n\n val binding: SwitchLayoutBinding = SwitchLayoutBinding.inflate(layoutInflater)\n setContentView(binding.root)\n\n binding.materialSwitch.setOnCheckedChangeListener { _, isChecked -\u003e\n if (isChecked) {\n // The switch is checked.\n } else {\n // The switch isn't checked.\n }\n }\n }\n}\n```\n\n### Java\n\n```java\npublic class MainActivity extends AppCompatActivity {\n\n @Override\n protected void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n\n SwitchLayoutBinding binding = SwitchLayoutBinding.inflate(getLayoutInflater());\n setContentView(binding.getRoot());\n\n binding.materialSwitch.setOnCheckedChangeListener((buttonView, isChecked) -\u003e {\n if (isChecked) {\n // The switch is checked.\n } else {\n // The switch isn't checked.\n }\n });\n }\n}\n```\n\n`CompoundButton.OnCheckedChangeListener` is a single abstract method interface\n(or *SAM interface* ), so you can implement it as a lambda. The lambda is called\nwhenever the checked state changes, and the value of the `isChecked` boolean\nthat is passed to the lambda indicates the new checked state."]]