KeyBoard Issue when using TabHost in android - android

Hi I am using four tabs in my tabhost, named TabOne, TabTwo, TabThree, TabFour.
In TabOne I have editext to make search option, when I pressed in edit text it show the keyboard.
But without closing the keyboard When I move to TabTwo the keyboard still showing. I dont want the keyboard should enable in TabTwo,TabThree,TabFour.
Because all other three tabs are not having edittext option.
Now My question is how to hide keyboard when clicked on the other tabs.
I have tried the below methods,
Adding the below code in oncreate method of TabB
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
Adding the below xml tag in Android manifest file
android:windowSoftInputMode="stateAlwaysHidden"
Please guide me to solve this issue.

Hide edit text when it lose focus.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
});
void hideKeyboard() {
InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

Please look at this answer
enable the soft keyboard
inputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
and disable the soft keyboard
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

This worked for me:
In the Activity which is triggered when you click on the specific Tab, I used this in onCreate:
inputSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputSearch.getWindowToken(), 0);
}
}});
In the AndroidManifest.xml, I added the following to my MainActivity (Where all Tabs are defined):
android:windowSoftInputMode="stateAlwaysHidden"

this.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
This single line will work for you. check this out.

Related

Android : Soft keyboard is not showing

I am creating a Fragment inside an Activity.I have an EditText in the MainActivity layout and the Fragment layout comes under that EditText.My issue is when I click the EditText the soft keyboard is not showing.
In EditText layout I create
<EditText
android:id="#+id/edt_searchContact"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textCursorDrawable="#drawable/color_cursor"
android:background="#drawable/edit_text_line_contacts"
android:focusable="true"
android:clickable="true"
android:hint="Search..."/>
<requestFocus/>
for showing the soft keyboard the code that I given in the Fragment
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchContact, InputMethodManager.SHOW_IMPLICIT);
In Manifest
android:windowSoftInputMode="adjustPan"
I have 3 Fragment in one Activity,when one Fragment comes that EditText is going to Visible in main Activity layout.
Can anyone please help me
Thanks in advance :)
Try this in your activity
editText = (EditText)findViewById(R.id.txt_yourName);
// Request focus and show soft keyboard automatically
editText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
On your Activity's onResume() Method you can write this:
EditText et = (EditText) findViewById(R.id.et);
et.requestFocus();
InputMethodManager mngr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mngr.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
Try this one :
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchContact, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
The below method worked for me-
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);

How Show soft keyboard automatically when EditText receives focus

I want show keyboard when my EditText receives focus. I tried many methods but nothing not helped.
I tried:
1.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
whith different flags.
2.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
<requestFocus />
4.
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
editText.post(new Runnable() {
#Override
public void run() {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
});
}
});
editText.requestFocus();
4 method is fork but it bad solution. Thus it is written here Show soft keyboard automatically when EditText receives focus
Before, I used the method 2 and it worked. but now no longer. and I created a blank Project and it does not work, none of the methods
UPDATE:
<style name="Theme.TransparencyDemo" parent="android:Theme.Light.NoTitleBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
You can add flags to your activity as well which will show the keyboard automatically
<activity name="package.ActivityName" android:windowSoftInputMode="stateVisible"/>
this is mostly useful if you expect the focus to be applied when the activity launches
Also you can use in Fragment:
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
or in Activity
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Use WindowManager instead of InputMehtodManager inside onFocusChange listener of edittext, As Its reliable.
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
} });

force soft keyboard to show when EditText gets focus [duplicate]

This question already has answers here:
How to show soft-keyboard when edittext is focused
(48 answers)
Closed 9 years ago.
I have an EditText that I am passing focus to programmatically. But when I do, I want the keyboard to show up as well (and then go down when that EditText lose focus). Right now, the user has to click on the EditText to get the keyboard to show up -- even thought the EditText already has focus.
<activity android:name=".YourActivity"
android:windowSoftInputMode="stateVisible" />
Add this to manifest file...
This is how I show the ketyboard:
EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
set this for your activity in your manifest to pop keyboard automatically when your screen comes containing EditText box
<activity android:windowSoftInputMode="stateAlwaysVisible" ... />
To hide keyboard on losing focus set a OnFocusChangeListener for the EditText .
In the onCreate()
EditText editText = (EditText) findViewById(R.id.textbox);
OnFocusChangeListener ofcListener = new MyFocusChangeListener();
editText.setOnFocusChangeListener(ofcListener);
Add this class
private class MyFocusChangeListener implements OnFocusChangeListener {
public void onFocusChange(View v, boolean hasFocus){
if(v.getId() == R.id.textbox && !hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
To show the keyboard, use the following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
To hide the keyboard,, use the code below.
et is the reference to the EditText
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(getActivity().INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
In order to do it based on focus listener you should go for:
final InputMethodManager imm =(InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
imm.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT);
}else{
imm.hideSoftInputFromWindow(et.getWindowToken(), 0);
}
imm.toggleSoftInput(0, 0);
}
});
Hope this helps.
Regards!

how to hide soft key pad after changing one viewpager to another view pager

hi guys how to hide soft keypad when am changing the one view pager to another view pager
the problem is in view pager having four tabs,first tab having search option when i click search edit text then after i click another tab.soft key pad is visible in next tab.
how to reslove this problem, i tryed this way it's not working
((InputMethodManager) getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE))
.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
help
Just add this attribute in your AndroidManifest under activity tag :
<activity
android:name="com.example.myApp.MyActivity"
android:windowSoftInputMode="stateHidden" >
</activity>
Try following code.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(any_focusable_view_reference.getWindowToken(), 0);
Here any_focusable_view_reference means EditText or such which has focus.
just added setOnFocusChangeListener to Edittext , then it working fine .
EditText editTextProfileName = (EditText) view
.findViewById(R.id.nameEditText);
editTextProfileName.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard();
}
}
private void hideKeyboard() {
if (editTextProfileName != null) {
InputMethodManager imanager = (InputMethodManager) getActivity()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imanager.hideSoftInputFromWindow(editTextProfileName.getWindowToken(), 0);
}
}
});
Thanks

Show soft keybord on edit text in a tab with multiple activities?

I am using multiple activities in my tab activity. There is an edit text field in my first activity under the first tab, but when I move from first activity within the first tab to the second activity and when I go back to my first activity using replaceview property and pressing the hardware back button, my edit text field do not show the softinputkeyboard.
What should I do in this? I used onResume this code, but still not working.
edittext.requestFocus();
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
help me out in this. Thanks in advance.
this happens because edittext.requestFocus() does not focus the element immediately.
try this approach:
edittext.setOnFocusChangeListener(new OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if(hasFocus)
{
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(etSearch, InputMethodManager.SHOW_IMPLICIT);
}
}
});

Categories

Resources