I have a customized Dialog. Inside the dialog, there's an EditText. When I touch inside the EditText, the soft keyboard is shown. I want to hide this keyboard when I touch inside the other place of the Dialog.
I know how to hide a keyboard in activity. I just don't know how to do this in Dialog.
Thank you.
You can do that easy by using focuslisners, see my code sample below:
EditText.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
#Override
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(EditText, InputMethodManager.SHOW_IMPLICIT);
}
else
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);
}
}
});
edit:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable = "true"
android:isFocusableInTouchMode = "true"
android:clickable = "true" >
<EditText
android:id="#+id/EditText"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:hint="hint"
android:singleLine="true" />
</LinearLayout>
</RelativeLayout>
\to be sure to get focus:
LinearLayout actionHide = (LinearLayout) findViewById(R.id.wrapper);
actionHide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionHide.requestFocus(); // use this to trigger the focus listner
//or use code below to set the keyboard to hidden
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(EditText.getWindowToken(), 0);
}
});
Try by setting InputMethodManager.SHOW_FORCED for Edittext :
InputMethodManager input_manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
input_manager.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
Related
Despite,
i use xml codes:
android:focusable="true"
android:focusableInTouchMode="true"
Or clear and request focus:
CountEdit1.clearFocus();
CountEdit1.requestFocus();
Or in java set focusable:
view.setFocusableInTouchMode(true);
view.requestFocus();
Or use input manager:
InputMethodManager iMM = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
etv1.requestFocus()
iMM.showSoftInput(etv1, InputMethodManager.SHOW_IMPLICIT);
i didn't get any solution for show input window again(it just Works for first onclick). These are relative code snippet:
imagearama.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.i("tago", "onclicktıklandı");
InputMethodManager iMM = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
etv1.requestFocus();
iMM.showSoftInput(etv1, InputMethodManager.SHOW_IMPLICIT);
editTextAyarla(etv1, imagesimge, logo, imagearama);
}
});
These are backpressed codes :
View view = getCurrentFocus();
InputMethodManager iMM = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
iMM.hideSoftInputFromWindow(view.getWindowToken(), 0);
view.clearFocus();
I want that when user clicks "imagearama" button , input window be opened.(It just occurs first click)
Relative xml code snippet :
<EditText
android:id="#+id/aramaalani"
android:layout_width="250dp"
android:layout_height="50dp"
android:textColor="#33ff33"
android:background="#843221"
android:focusableInTouchMode="true"
android:hint="Aramak istedigini gir"
android:singleLine="true"
/>
I want to make similar feature in my app imageview onClick open keyboard that i have done and keyboard is opening very well.
I'm using a relative layout i have placed the image of keyboard on the edittext which is similar to this reference hangouts image.
problem is when i type something it does not show up in edittext's text field. How to handle it? guide me
protected void onCreate(Bundle savedInstanceState) {
imageview = (ImageView) findViewById(R.id.keyboard);
imageview.setFocusableInTouchMode(true);
imageview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
try {
showSoftKeyboard(imageview);
} catch (Exception e) {
Log.e("Error MA onCreate keyboard open", "Error");
}
break;
}
return true;
}
});
}
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
this is my xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="#+id/search_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:padding="12dp" >
<ImageView
android:id="#+id/keyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="true"
android:src="#drawable/ic_action_keyboard" />
<EditText
android:id="#+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/keyboard"
android:hint="Search Here"
android:focusable="true" />
</RelativeLayout>
<ListView
/>
</RelativeLayout>
Problem: imageView is calling the softkeyboard.
Possible solution: After clicking on the imageView call showSoftKeyboard() method using your edittext as a parameter.
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
// myEditText.requestFocus();//this line is also worth a try
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
}
public void showSoftKeyboard(View view) {
if (view.requestFocus()) {
mEditText.setFocusable(true);
mEditText.requestFocus();// this line is also worth a try
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);//sending mEditText here in the parameter solved my problem instead of sending view
}
}
I have a dialogue which prompts a user to enter in a 4 digit pin.
I would like for the keyboard to automatically be displayed when this dialog fragment popup is shown,
I have tried the following but I have not come right
<EditText
android:id="#+id/entercode_dialog_editText_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_horizontal"
android:inputType="numberPassword" >
<requestFocus />
</EditText>
Try This for Showing KeyBoard:
InputMethodManager imm = (InputMethodManager)getactivity().
getSystemService(Context.INPUT_METHOD_SERVICE);
solutiuon1:
if(imm != null){
imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}
solution2:
if(imm != null){
imm.showSoftInput(ed, 0);
}
where ed is your edittext
one more solution (not tried yet)
after inializing dialog
getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
Use this:-
myEditText.setOnFocusChangeListener( new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
});
To appear the soft keyboard , you can use
EditText yourEditText= (EditText) findViewById(R.id.et);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
To close it you can use
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
Hope Can Help You
I am facing a weird problem in edittext control of android.
The issue is, when i type anything in softkeyboard. the text is not getting printed in editbox.
Here is the code
mEditview.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
}
});
mEditview.requestFocus();
layout code is added
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/filter_text"
android:imeOptions="actionDone"
android:singleLine="true"
android:hint="Search"/>
Here is what i get as output in emulator. Why its coming like this? how to fix this problem
I would like to off android default keyboard when i pressed at editText. Such that the default keyboard will not popup.
I have tried folloing code but didn't worked:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lltest"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText0"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
And code is:
edtEdit1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edtEdit1.getWindowToken(), 1);
return false;
}
});
Have you tried
android:editable="false"
in you xml declaration of edit text??
for hidden
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
for show
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Try this
You put 1 instead of 0
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
To prevent the Android keyboard from appearing when an EditText is touched, just return true in the onTouch() method.
edtEdit1.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});