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

অ্যান্ড্রয়েডে EditText-এর বাইরে ক্লিক করলে সফ্ট কিবোর্ড লুকানোর পদ্ধতি

EditText-এ বাইরে ক্লিক করলে কিবোর্ড হাইড করা

অ্যান্ড্রয়েডে ডিফল্টভাবে EditText-এ বাইরে ক্লিক করলে কিবোর্ড লুকায় না। এই টিউটোরিয়ালে InputMethodManager ব্যবহার করে কিবোর্ড প্রোগ্রাম্যাটিক্যালি লুকানোর পদ্ধতি দেখানো হয়েছে।

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

Android Studio-তে File → New Project থেকে প্রজেক্ট তৈরি করুন।

ধাপ ২: লেআউট XML (activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:padding="16dp"
    tools:context="MainActivity">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:hint="Enter text here"
        android:textSize="20sp" />
    
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp" />
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="16dp"
        android:onClick="setText"
        android:text="Set Text"
        android:textStyle="bold" />
</LinearLayout>

ধাপ ৩: MainActivity.java বাস্তবায়ন

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    EditText editText;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        editText = findViewById(R.id.editText);
        textView = findViewById(R.id.textView);
    }

    public void setText(View view) {
        String newText = editText.getText().toString();
        textView.setText(newText);
        closeKeyBoard();
    }

    private void closeKeyBoard() {
        // বর্তমান ফোকাসড ভিউ পান
        View view = this.getCurrentFocus();
        if (view != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                // সফ্ট ইনপুট লুকান
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }
    
    // বোনাস: রুট লেআউটে ক্লিক লিসেনার যোগ করে যেকোনো জায়গায় ক্লিক করলে কিবোর্ড লুকাবে
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (view instanceof EditText) {
                Rect outRect = new Rect();
                view.getGlobalVisibleRect(outRect);
                if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
                    view.clearFocus();
                    closeKeyBoard();
                }
            }
        }
        return super.dispatchTouchEvent(event);
    }
}

InputMethodManager-এর মূল মেথড

  • hideSoftInputFromWindow(windowToken, flags): নির্দিষ্ট উইন্ডোর কিবোর্ড লুকায়
  • showSoftInput(view, flags): কিবোর্ড দেখায়
  • isAcceptingText(): কিবোর্ড খোলা আছে কিনা চেক করে

XML-এ অটো-হাইড সেটিং (API 11+)

parent layout-এ এই атриবিউট যোগ করলে EditText-এ বাইরে টাচ করলে কিবোর্ড লুকাবে:

<LinearLayout
    android:focusableInTouchMode="true"
    android:descendantFocusability="beforeDescendants"
    ... >

ধাপ ৪: AndroidManifest.xml

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

অ্যাপ্লিকেশন টেস্ট করুন

অ্যাপ রান করুন, EditText-এ ক্লিক করলে কিবোর্ড আসবে। "Set Text" বাটনে ক্লিক করলে বা EditText-এ বাইরে যেকোনো জায়গায় টাচ করলে কিবোর্ড লুকিয়ে যাবে।

  1. অ্যান্ড্রয়েডে EditText ফোকাস পেলে সফ্ট কিবোর্ড দেখানোর পদ্ধতি

  2. অ্যান্ড্রয়েডে EditText খালি কিনা চেক করার পদ্ধতি

  3. Android-এ সফট কীবোর্ড তৈরি করার পদ্ধতি

  4. অ্যান্ড্রয়েড এক্টিভিটিতে নেভিগেশন বার স্থায়ীভাবে কীভাবে লুকাবেন?