কম্পিউটার টিউটোরিয়াল

কোটলিন ব্যবহার করে অ্যান্ড্রয়েডে বাটন টেক্সটের জন্য নির্দিষ্ট ফন্ট সেট করা

ওভারভিউ

এই উদাহরণটি দেখায় কিভাবে কোটলিনে প্রোগ্রাম্যাটিক্যালি বাটনের টেক্সট ফন্ট পরিবর্তন করা যায়। এতে সিস্টেম ফন্ট (Monospace, Serif, Sans-Serif) এবং স্টাইল (Bold, Italic) ব্যবহার করা হয়েছে।

পূর্বশর্ত

  • অ্যান্ড্রয়েড স্টুডিও (কোটলিন)
  • View ও Typeface বেসিক জ্ঞান

ধাপ ১: প্রজেক্ট তৈরি করুন

File → New Project → Empty Activity → Language: Kotlin।

ধাপ ২: লেআউট (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_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:text="Tutorials Point"
        android:textAlignment="center"
        android:textColor="@android:color/holo_green_dark"
        android:textSize="32sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/btnChangeFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Click to change font of the Button" />

    <Button
        android:id="@+id/btnChangeFont2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btnChangeFont"
        android:layout_centerInParent="true"
        android:layout_marginTop="10dp"
        android:text="Click to change font of the Button" />

</RelativeLayout>

ধাপ ৩: MainActivity.kt

package com.example.changefont

import android.graphics.Typeface
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    lateinit var btnChangeFont: Button
    lateinit var btnChangeFont2: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        btnChangeFont = findViewById(R.id.btnChangeFont)
        btnChangeFont.setOnClickListener {
            // Monospace ফন্ট + Bold স্টাইল
            btnChangeFont.setTypeface(Typeface.MONOSPACE, Typeface.BOLD)
        }

        btnChangeFont2 = findViewById(R.id.btnChangeFont2)
        btnChangeFont2.setOnClickListener {
            // Serif ফন্ট + Bold Italic স্টাইল
            btnChangeFont2.setTypeface(Typeface.SERIF, Typeface.BOLD_ITALIC)
        }
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.changefont">

    <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>

চলান ও ফলাফল

  1. প্রথম বাটনে ক্লিক করলে তার ফন্ট Monospace Bold হবে।
  2. দ্বিতীয় বাটনে ক্লিক করলে তার ফন্ট Serif Bold Italic হবে।

Typeface বা ফন্ট স্টাইল অপশনসমূহ

Typeface.DEFAULT / Typeface.SANS_SERIFTypeface.SERIFTypeface.MONOSPACETypeface.create("casual", Typeface.NORMAL)Typeface.create("cursive", Typeface.NORMAL)Typeface.create("sans-serif-condensed", Typeface.NORMAL)
ফন্ট ফ্যামিলিকনস্ট্যান্ট
Default (Sans-Serif)
Serif
Monospace
Casual (API 16+)
Cursive (API 16+)
Sans-Serif Condensed
Typeface.NORMAL (0)Typeface.BOLD (1)Typeface.ITALIC (2)Typeface.BOLD_ITALIC (3)
স্টাইলকনস্ট্যান্ট
Normal
Bold
Italic
Bold Italic

কাস্টম ফন্ট (.ttf/.otf) ব্যবহার করা

  1. ফন্ট ফাইল res/font/ ফোল্ডারে রাখুন (যেমন: myfont.ttf)।
  2. কোটলিনে লোড করুন:
    val typeface = ResourcesCompat.getFont(this, R.font.myfont)
    button.typeface = typeface
  3. XML-এ সরাসরি: android:fontFamily="@font/myfont"।

XML-এ ফন্ট সেট করা (প্রোগ্রাম্যাটিক ছাড়া)

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-condensed"
    android:textStyle="bold|italic"
    android:text="Styled Button" />
  1. অ্যান্ড্রয়েডে বাটন কাস্টমাইজ করে টেক্সট এবং রঙ সেট করার পদ্ধতি

  2. অ্যান্ড্রয়েডে প্রোগ্রাম্যাটিক্যালি বাটনে drawableLeft সেট করার পদ্ধতি

  3. অ্যান্ড্রয়েডে বাটনের টেক্সট ফন্ট পরিবর্তন করা

  4. Tkinter-এ টেক্সটের জন্য ফন্ট সেট করা