यह उदाहरण दर्शाता है कि Android एक्टिविटी में सॉफ्ट कीबोर्ड की विजिबिलिटी चेंज को कैसे डिटेक्ट किया जाता है।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project पर जाएं और प्रोजेक्ट बनाएं।
चरण 2: लेआउट 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"
android:hint="यहां टाइप करें..."
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="100dp" />
<Button
android:id="@+id/btnButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="कीबोर्ड छुपाएं"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginBottom="100dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 3: MainActivity.java में कोड जोड़ें
package com.app.sample;
import androidx.appcompat.app.AppCompatActivity;
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 {
private View rootView;
private EditText editText;
private boolean isKeyboardVisible = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = findViewById(R.id.rootView);
editText = findViewById(R.id.editText);
Button btnHide = findViewById(R.id.btnButton);
editText.requestFocus();
// ग्लोबल लेआउट लिस्टनर से कीबोर्ड विजिबिलिटी डिटेक्ट करें
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int screenHeight = rootView.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
boolean keyboardVisible = keypadHeight > screenHeight * 0.15;
// केवल स्टेट चेंज पर टोस्ट दिखाएं
if (keyboardVisible != isKeyboardVisible) {
isKeyboardVisible = keyboardVisible;
String msg = keyboardVisible ? "कीबोर्ड खुला" : "कीबोर्ड बंद";
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
}
});
btnHide.setOnClickListener(v -> hideKeyboard());
}
private void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}
}
चरण 4: AndroidManifest.xml
मानक मैनिफेस्ट।
मॉडर्न अल्टरनेटिव: WindowInsets (API 30+)
नए Android वर्जन के लिए WindowInsetsCompat और WindowInsetsAnimation का उपयोग करें जो अधिक विश्वसनीय हैं।
ऐप चलाएं
डिवाइस कनेक्ट करें, Run करें। EditText पर टैप करके कीबोर्ड खोलें और बटन दबाकर बंद करें। टोस्ट संदेश स्टेट चेंज दिखाएंगे।
