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

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

এই উদাহরণটি একটি বেসিক সফট কিবোর্ড (Input Method Editor - IME) তৈরি করার কনসেপ্ট দেখায়। কিন্তু মূল উদাহরণটি আসলে একটি সাধারণ EditText ইনপুট ডেমো, কোনো রিয়েল IME সার্ভিস নয়। নিচে আসল IME তৈরি করার ধাপগুলো দেওয়া হলো।

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

Android Studio-তে File → New Project এ যান এবং "No Activity" সিলেক্ট করে একটি নতুন প্রজেক্ট তৈরি করুন (IME-এ UI Activity দরকার নেই)।

দ্বিতীয় ধাপ: IME সার্ভিস ক্লাস তৈরি করুন

একটি কোটলিন ক্লাস তৈরি করুন MyInputMethodService.kt নামে:

import android.inputmethodservice.InputMethodService
import android.inputmethodservice.Keyboard
import android.inputmethodservice.KeyboardView
import android.view.View
import android.view.inputmethod.EditorInfo

class MyInputMethodService : InputMethodService(), KeyboardView.OnKeyboardActionListener {
    private lateinit var keyboardView: KeyboardView
    private lateinit var keyboard: Keyboard

    override fun onCreateInputView(): View {
        keyboardView = layoutInflater.inflate(R.layout.input_keyboard, null) as KeyboardView
        keyboard = Keyboard(this, R.xml.qwerty_keyboard)
        keyboardView.keyboard = keyboard
        keyboardView.setOnKeyboardActionListener(this)
        return keyboardView
    }

    override fun onStartInputView(editorInfo: EditorInfo, restarting: Boolean) {
        super.onStartInputView(editorInfo, restarting)
        keyboardView.setKeyboard(keyboard)
    }

    override fun onKey(primaryCode: Int, keyCodes: IntArray?) {
        currentInputConnection?.commitText(primaryCode.toChar().toString(), 1)
    }

    override fun onPress(primaryCode: Int) {}
    override fun onRelease(primaryCode: Int) {}
    override fun onText(text: CharSequence?) {}
    override fun swipeLeft() {}
    override fun swipeRight() {}
    override fun swipeDown() {}
    override fun swipeUp() {}
}

তৃতীয় ধাপ: কিবোর্ড লেআউট XML তৈরি করুন

res/layout/input_keyboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.inputmethodservice.KeyboardView
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/keyboardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:keyPreviewLayout="@layout/key_preview" />

চতুর্থ ধাপ: কিবোর্ড কী ডেফিনিশন XML তৈরি করুন

res/xml/qwerty_keyboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<Keyboard xmlns:android="https://schemas.android.com/apk/res/android"
    android:keyWidth="10%p"
    android:horizontalGap="0px"
    android:verticalGap="0px"
    android:keyHeight="60dp">

    <Row>
        <Key android:codes="113" android:keyLabel="q" android:keyEdgeFlags="left"/>
        <Key android:codes="119" android:keyLabel="w"/>
        <Key android:codes="101" android:keyLabel="e"/>
        <Key android:codes="114" android:keyLabel="r"/>
        <Key android:codes="116" android:keyLabel="t"/>
        <Key android:codes="121" android:keyLabel="y"/>
        <Key android:codes="117" android:keyLabel="u"/>
        <Key android:codes="105" android:keyLabel="i"/>
        <Key android:codes="111" android:keyLabel="o"/>
        <Key android:codes="112" android:keyLabel="p" android:keyEdgeFlags="right"/>
    </Row>
    <!-- আরও রো যোগ করুন -->
</Keyboard>

পঞ্চম ধাপ: ম্যানিফেস্টে IME সার্ভিস ডিক্লেয়ার করুন

androidManifest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <service
            android:name=".MyInputMethodService"
            android:label="My Soft Keyboard"
            android:permission="android.permission.BIND_INPUT_METHOD">
            <meta-data
                android:name="android.view.im"
                android:resource="@xml/method" />
            <intent-filter>
                <action android:name="android.view.InputMethod" />
            </intent-filter>
        </service>

    </application>
</manifest>

ষষ্ঠ ধাপ: IME মেটাডেটা XML তৈরি করুন

res/xml/method.xml:

<?xml version="1.0" encoding="utf-8"?>
<input-method xmlns:android="https://schemas.android.com/apk/res/android">
    <subtype
        android:label="English (US)"
        android:imeSubtypeLocale="en_US"
        android:imeSubtypeMode="keyboard" />
</input-method>

গুরুত্বপূর্ণ নোট

  • এটি একটি বেসিক কিবোর্ড ইমপ্লিমেন্টেশন। প্রোডাকশন লেভেল কিবোর্ডে গেসচার টাইপিং, প্রেডিকশন, থিম, মাল্টিলینگুয়াল সাপোর্ট, ইমোজি, ভয়েস ইনপুট ইত্যাদি থাকে।
  • Android Studio-তে New → Module → Input Method Service সিলেক্ট করলে বয়লারপ্লেট কোড জেনারেট হয়।
  • টেস্ট করার জন্য ডিভাইস সেটিংস → Languages & input → Virtual keyboard → Manage keyboards থেকে আপনার কিবোর্ড ইনেবল করুন।

মূল উদাহরণের কোড (সাধারণ EditText ডেমো)

আপনার দেওয়া উদাহরণটি আসলে একটি সাধারণ EditText ইনপুট ডেমো। সেটিও নিচে দেওয়া হলো রেফারেন্সের জন্য:

// MainActivity.kt (সাধারণ EditText ডেমো)
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    private lateinit var editText: EditText
    private lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        editText = findViewById(R.id.editText)
        textView = findViewById(R.id.textView)
    }

    fun showText(view: View) {
        val newText = editText.text.toString()
        textView.text = newText
    }
}
  1. Android-এ সফট কীবোর্ড তৈরি করার পদ্ধতি

  2. Kotlin ব্যবহার করে Android-এ প্রোগ্রামেটিক্যালি Wi-Fi নেটওয়ার্কে কানেক্ট করা

  3. Kotlin ব্যবহার করে Android-এ রানটাইমে TextView স্টাইল পরিবর্তন করা

  4. অ্যান্ড্রয়েড এমুলেটরের IP ঠিকানা কোটলিন ব্যবহার করে পাওয়ার উপায়