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

Android-এ সফট কিবোর্ড খোলা ও বন্ধ হওয়ার লিসেনার লিখুন

অ্যান্ড্রয়েডে সফট কিবোর্ডের ওপেন/ক্লোজ ইভেন্ট শোনা

এই উদাহরণটি দেখায় কিভাবে একটি অ্যাক্টিভিটিতে সফট কিবোর্ড খোলা বা বন্ধ হওয়ার ইভেন্ট শোনা যায় ViewTreeObserver.OnGlobalLayoutListener ব্যবহার করে।

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

Android Studio-তে File → New Project এ যান এবং একটি নতুন প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট XML-এ কোড যোগ করুন

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:id="@+id/rootView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        tools:ignore="MissingConstraints" />

    <Button
        android:id="@+id/btnButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here to hide"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

ধাপ ৩: MainActivity.java-এ কোড যোগ করুন

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ConstraintLayout constraintLayout;
    private EditText editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        constraintLayout = findViewById(R.id.rootView);
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.btnButton);

        editText.requestFocus();

        // গ্লোবাল লেআউট লিসেনার যোগ করুন
        constraintLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                constraintLayout.getWindowVisibleDisplayFrame(r);
                int screenHeight = constraintLayout.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;

                // যদি কিবোর্ডের উচ্চতা স্ক্রিনের ১৫% এর বেশি হয়, তবে কিবোর্ড খোলা আছে
                if (keypadHeight > screenHeight * 0.15) {
                    Toast.makeText(MainActivity.this, "Keyboard is showing", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Keyboard closed", Toast.LENGTH_SHORT).show();
                }
            }
        });

        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btnButton) {
            hideSoftKeyboard(v);
        }
    }

    private void hideSoftKeyboard(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

ধাপ ৪: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.app.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>

অ্যাপটি চালান

ডিভাইস সংযুক্ত করে Run ক্লিক করুন। EditText-এ ফোকাস করলে কিবোর্ড খুলবে এবং বাটনে ক্লিক করলে বন্ধ হবে। টোস্ট মেসেজে কিবোর্ডের স্টেট দেখাবে।

Android-এ সফট কিবোর্ড খোলা ও বন্ধ হওয়ার লিসেনার লিখুন

  1. এন্ড্রয়েডে অ্যাক্টিভিটি রিস্টার্ট করার পদ্ধতি

  2. অ্যান্ড্রয়েডে কল পিক আপ এবং হ্যাং আপ করার পদ্ধতি

  3. অ্যান্ড্রয়েডে Assets ফোল্ডারে ফাইল লেখার পদ্ধতি

  4. অ্যান্ড্রয়েডে HEIC ইমেজ খোলা ও কনভার্ট করার পদ্ধতি