How to Hide or Close Android Soft Keyboard

How to Hide or Close Android Soft Keyboard

Close/hide the Android Soft Keyboard: The latest (VKB) virtual keyboard of Android has become throughout the years to wind up one of the best prescient delicate consoles available. Not only this as well as those of us who compose Android code as a profession once in a while need to give console enter a misgiving because of the window delicate information mode alternatives Google insightfully gave as a major aspect of the show for every single movement.
There are a couple of cases, notwithstanding, when you might need to physically drive the VKB to appear or, more probable, stow away. A true situation may be the client has the VKB indicating when abruptly she chooses to tap something in your menu bar that starts a part supplant. Those moves can look quite awful with the VKB up front.

Hide-Close-Android-Soft-Keyboard
Hide-Close-Android-Soft-Keyboard

The accompanying illustration demonstrates a straightforward technique to get a reference to the information strategy (IME) supervisor, and how to utilize that reference to appear and conceal the VKB when proper. You can take after alongside the regulated guidelines, or download and import the whole venture straightforwardly into Eclipse.

1. Make another Android venture in Eclipse. Target SDK 14 (ICS) or better.

2. In the AndroidManifest.xml record, set the underlying delicate data mode and startup state.

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>

<manifest xmlns:android=”http://schemas.android.com/apk/res/android”

package=”com.authorwjf.kbdtoggled”

android:versionCode=”1″

android:versionName=”1.0″ >

 

<uses-sdk

android:minSdkVersion=”14″

android:targetSdkVersion=”17″ />

 

<application

android:allowBackup=”true”

android:icon=”@drawable/ic_launcher”

android:label=”@string/app_name”

android:theme=”@style/AppTheme” >

<activity

android:name=”.MainActivity”

android:label=”@string/app_name”

android:windowSoftInputMode=”adjustResize|stateHidden” >

<intent-filter>

<action android:name=”android.intent.action.MAIN” />

 

<category android:name=”android.intent.category.LAUNCHER” />

</intent-filter>

</activity>

</application>

 

</manifest>

  1. Modify activity_main.xml in the /res/layout folder, to view text and stack two buttons in a linear layout.

activity_main.xml

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:layout_gravity=”center”

android:gravity=”center”

android:orientation=”vertical”>

 

<Button

android:id=”@+id/hide_button”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:layout_margin=”10dp”

android:hint=”Hide VKB” />

<EditText

android:id=”@+id/content”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”/>

<Button

android:id=”@+id/show_button”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:layout_margin=”10dp”

android:hint=”Show VKB” />

</LinearLayout>

  1. The main thing forgot to substance is our MainActivity.java file. In the on make override we get a reference to the IME service, and in the click audience we conjure either the show or the shroud technique with a fitting target.

ActivityMain.java

package com.authorwjf.kbdtoggled;

 

import android.app.Activity;

import android.content.Context;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.inputmethod.InputMethodManager;

 

public class MainActivity extends Activity implements OnClickListener {

 

private InputMethodManager mIMEMgr;

 

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mIMEMgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

findViewById(R.id.show_button).setOnClickListener(this);

findViewById(R.id.hide_button).setOnClickListener(this);

}

 

@Override

public void onClick(View v) {

if (v.getId()==R.id.show_button) {

mIMEMgr.showSoftInput(findViewById(R.id.content), 0);

} else {

mIMEMgr.hideSoftInputFromWindow(findViewById(R.id.content).getWindowToken(), 0);

}

}

 

}

Run the resulting APK on a device or an emulator to see the buttons in action

Share this post

Post Comment