कीबोर्ड छुपाने के तरीके
तरीका 1: InputMethodManager (क्लासिक)
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.content.Context;
public void hideKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
// वर्तमान में फोकस्ड व्यू से विंडो टोकन प्राप्त करें
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
// उपयोग: बटन क्लिक पर
button.setOnClickListener(v -> hideKeyboard(v)); // या editText.getWindowToken()
तरीका 2: Window से (एक्टिविटी में)
public void hideKeyboardFromActivity() {
View currentFocus = getCurrentFocus();
if (currentFocus != null) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(currentFocus.getWindowToken(), 0);
}
}
// या सीधे विंडो से
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
तरीका 3: Kotlin एक्सटेंशन फंक्शन
// Extensions.kt
fun View.hideKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(windowToken, 0)
}
fun Activity.hideKeyboard() {
currentFocus?.hideKeyboard()
}
// उपयोग
editText.hideKeyboard()
activity.hideKeyboard()
पूर्ण उदाहरण
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="यहां टाइप करें..."
android:inputType="text"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnHideKeyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="कीबोर्ड छुपाएं"
app:layout_constraintTop_toBottomOf="@id/editText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button btnHideKeyboard;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
btnHideKeyboard = findViewById(R.id.btnHideKeyboard);
// ऑटो-फोकस और कीबोर्ड दिखाएं (वैकल्पिक)
editText.post(() -> {
editText.requestFocus();
showKeyboard(editText);
});
btnHideKeyboard.setOnClickListener(v -> {
hideKeyboard(v);
Toast.makeText(this, "कीबोर्ड छुपाया गया", Toast.LENGTH_SHORT).show();
});
}
private void hideKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
private void showKeyboard(View view) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
view.requestFocus();
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
// टच पर कीबोर्ड छुपाएं (वैकल्पिक)
@Override
public boolean dispatchTouchEvent(android.view.MotionEvent ev) {
if (ev.getAction() == android.view.MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (v != null) {
int[] location = new int[2];
v.getLocationOnScreen(location);
float x = ev.getRawX() - location[0];
float y = ev.getRawY() - location[1];
if (x < 0 || x > v.getWidth() || y < 0 || y > v.getHeight()) {
hideKeyboard(v);
v.clearFocus();
}
}
}
return super.dispatchTouchEvent(ev);
}
}
मैनिफेस्ट में विंडो सॉफ्ट इनपुट मोड
<activity
android:name=".MainActivity"
android:windowSoftInputMode="stateHidden|adjustResize">
<!--
stateHidden: शुरू में कीबोर्ड छुपा हुआ
stateVisible: शुरू में कीबोर्ड दिखाएं
stateAlwaysHidden: हमेशा छुपा हुआ
adjustResize: लेआउट रिसाइज़ करें कीबोर्ड के लिए जगह बनाने को
adjustPan: कंटेंट पैन करें (डिफ़ॉल्ट)
-->
</activity>
सामान्य समस्याएं और समाधान
| समस्या | समाधान |
| getWindowToken() null रिटर्न करता है | व्यू में फोकस नहीं है या विंडो से जुड़ा नहीं | view.requestFocus() पहले कॉल करें, या getCurrentFocus() उपयोग करें |
| कीबोर्ड दोबारा दिख जाता है | EditText में फोकस रहता है | view.clearFocus() कॉल करें या फोकस करने योग्य व्यू पर फोकस ले जाएं |
| डायलॉग में काम नहीं करता | डायलॉग की अपनी विंडो होती है | dialog.getWindow().setSoftInputMode(...) उपयोग करें |
| फुलस्क्रीन मोड में समस्या | सिस्टम UI फ्लैग्स हस्तक्षेप करते हैं | WindowInsetsController API 30+ उपयोग करें |
आधुनिक API (API 30+)
// WindowInsetsController (API 30+)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// छुपाएं
getWindow().getInsetsController().hide(WindowInsets.Type.ime());
// दिखाएं
getWindow().getInsetsController().show(WindowInsets.Type.ime());
// व्यवहार नियंत्रित करें
getWindow().setDecorFitsSystemWindows(false); // एज-टू-एज
}
RecyclerView/ListView में कीबोर्ड छुपाना
recyclerView.addOnItemTouchListener(new RecyclerView.SimpleOnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
View focused = getCurrentFocus();
if (focused != null) {
hideKeyboard(focused);
focused.clearFocus();
}
}
return false;
}
});