উদাহরণের আগে জানতে হবে TabHost কী। TabHost ট্যাবের একটি সেট ধারণ করে। প্রতিটি ট্যাবে প্রজেক্টের স্পেসিফিকেশন অনুযায়ী ফ্র্যাগমেন্ট বা এক্টিভিটি থাকে। ব্যবহারকারী ট্যাবগুলো বাম থেকে ডান বা ডান থেকে বাম স্ক্রল করতে পারে।
এই উদাহরণটি দেখায় অ্যান্ড্রয়েডে TabHost কীভাবে ব্যবহার করতে হয়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
অ্যান্ড্রয়েড স্টুডিওতে File → New Project এ যান এবং প্রজেক্ট তৈরির জন্য প্রয়োজনীয় বিবরণ পূরণ করুন।
ধাপ ২: activity_main.xml-এ লেআউট যোগ করুন
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1" />
</LinearLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab2" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
এই লেআউটে FrameLayout TabWidget-এর চাইল্ড হিসেবে ডিক্লেয়ার করা হয়েছে (android.com অনুযায়ী TabWidget-এর জন্য FrameLayout কন্টেন্ট হিসেবে আবশ্যক)।
ধাপ ৩: MainActivity.java-এ কোড যোগ করুন
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.TabHost;
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost tabs = (TabHost) findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec = tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("First");
tabs.addTab(spec);
spec = tabs.newTabSpec("tag2");
spec.setContent(R.id.tab2);
spec.setIndicator("second");
tabs.addTab(spec);
}
}
ধাপ ৪: Manifest.xml পরিবর্তন করার প্রয়োজন নেই
আউটপুট
অ্যাপ রান করুন। ডিফল্ট স্ক্রিন:

দ্বিতীয় ট্যাবে ক্লিক করলে:
