How to permanently hide soft keyboard on searchview and edittext - android

I want to hide soft keyboard everywhere in my application permanently. So I can use my custom keyboard. I have checked many solutions but still soft keyboard is appearing. Here is my code to register searchview.
EditText mEditText;
SearchView mSearchView;
public void registerSearchView(final SearchView sview, final MenuItem searchItem ) {
mSearchView = sview;
SearchManager searchManager = (SearchManager) mHostActivity.getSystemService(Context.SEARCH_SERVICE);
mSearchView.setSearchableInfo(searchManager.getSearchableInfo(mHostActivity.getComponentName()));
mEditText = (EditText) mSearchView.findViewById(R.id.search_src_text);
ImageView closeButton = (ImageView) mSearchView.findViewById(R.id.search_close_btn);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mEditText.getText() == null || mEditText.getText().length() < 1) {
mSearchView.onActionViewCollapsed();
MenuItemCompat.collapseActionView(searchItem);
hideCustomKeyboard();
} else {
mEditText.setText("");
mSearchView.setQuery("", false);
}
//Collapse the search widget
}
});
mEditText.setOnTouchListener(onTouch);
mSearchView.setOnTouchListener(onTouch);
mEditText.setOnFocusChangeListener(oFocusChange);
mSearchView.setOnFocusChangeListener(oFocusChange);
mEditText.setOnClickListener(onClickListener);
mSearchView.setOnClickListener(onClickListener);
mSearchView.setOnSearchClickListener(onClickListener);
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
showCustomKeyboard(v);
v.clearFocus();
v.requestFocus();
}
};
private View.OnTouchListener onTouch = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent motionEvent) {
if (mEditText.getText()==null || mEditText.getText().length() < 1){
v.clearFocus();
v.requestFocus();
}
((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
v.onTouchEvent(motionEvent);
return true;
}
};
private View.OnFocusChangeListener oFocusChange = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean b) {
if (b == false) {
hideCustomKeyboard();
} else {
((InputMethodManager) mHostActivity.getSystemService(ActionBarActivity.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
showCustomKeyboard(v);
}
}
};

This will work as you can force android to hide the keyboard !!
public static void disableSoftInputFromAppearing(EditText editText) {
if (Build.VERSION.SDK_INT >= 11) {
editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);
} else {
editText.setRawInputType(InputType.TYPE_NULL);
editText.setFocusable(true);
}
}
above code will work foresure !!
as well for search view
mSearchView.clearFocus();
will solve the issue

Add this to you manifest :
<activity android:name="com.your.package.ActivityName"
android:windowSoftInputMode="stateHidden" />

Related

Edittext Focus and click

I have Two EditTexts. I want On First EditTexts click clear the second and on second edittext click clear the first one. So I have tried OnClickListener and OnTouchListener. But it is not working properly.
et_email.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!(et_email.getText().toString().equalsIgnoreCase("") && et_mobile.getText().toString().equalsIgnoreCase(""))) {
if (MotionEvent.ACTION_UP == event.getAction()) {
et_mobile.setText("");
et_mobile.setFocusableInTouchMode(true);
et_mobile.setFocusable(true);
et_mobile.requestFocus();
}
return true; // return is important...
} else
return false;
}
});
et_mobile.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (!(et_email.getText().toString().equalsIgnoreCase("") && et_mobile.getText().toString().equalsIgnoreCase(""))) {
if (MotionEvent.ACTION_UP == event.getAction()) {
et_email.setText("");
et_mobile.setFocusableInTouchMode(true);
et_mobile.setFocusable(true);
et_mobile.requestFocus();
}
return true; // return is important...
}
else
return false;
}
});
But the problem is Focus is not setting on first touch and while clicking and unable to clear EditText.
First add this to your Edittext layouts:
android:focusableInTouchMode="false"
Without this line, the EditText will react to touch-mode, and only listen to your onClick() the second time you click your EditText bar.
This will disable touch-mode for EditText, and fire onClick() in first time
focusableInTouchMode
Note: boolean that controls whether a view can take focus while in touch mode. If this is true for a view, that view can gain focus when clicked on, and can keep focus if another view is clicked on that doesn't have this attribute set to true.
than do as follows:
EditText1
EditText et_email = (EditText) findViewById(R.id.yourEditText1);
et_email.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do this
et_mobile.setText("");
//or this
et_mobile.getText().clear();
}
});
EditText2
EditText et_mobile = (EditText) findViewById(R.id.yourEditText2);
et_mobile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do this
et_email.setText("");
//or this
et_email.getText().clear();
}
});
This should help u out.
you are clearing the first one one touching the first one, change your variable names in first listener like this
if (!(et_email.getText().toString().equalsIgnoreCase("") && et_mobile.getText().toString().equalsIgnoreCase(""))) {
if (MotionEvent.ACTION_UP == event.getAction()) {
et_mobile.setText("");
et_email.setFocusableInTouchMode(true);
et_email.setFocusable(true);
et_email.requestFocus();
}
return true; // return is important...
} else
I hope this code helps you (it works for me)
edit1 = (EditText) findViewById(R.id.edit1);
edit2 = (EditText) findViewById(R.id.edit2);
edit1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
edit2.setText("");
}
});
edit2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
edit1.setText("");
}
});

setOnQueryTextFocusChangeListener vs setOnFocusChangeListener

The android SearchView has two methods: setOnQueryTextFocusChangeListener and setOnFocusChangeListener. Are they interchangeable? What is the difference between them?
1."setOnFocusChangeListener" (inherit from android.view.View) is for the whole view.
2."setOnQueryTextFocusChangeListener" is just for the AutoCompleteTextView.
See the code of SearchView:
public void setOnQueryTextFocusChangeListener(OnFocusChangeListener listener) {
mOnQueryTextFocusChangeListener = listener;
}
******
mQueryTextView = (SearchAutoComplete) findViewById(R.id.search_src_text);
******
mQueryTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (mOnQueryTextFocusChangeListener != null) {
mOnQueryTextFocusChangeListener.onFocusChange(SearchView.this, hasFocus);
}
}
});

Dismiss keyboard when EditText loses focus

I have an EditText that I want to control the keyboard. When the EditText has focus the keyboard should appear, then as soon as I click on any other view, I want the keyboard to disappear. I try the following code but it did work
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} else {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
}
}
});
Assuming your outermost layout is RelativeLayout(You can do the similar thing for others as well), you can do something like following:
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//....
layout = (RelativeLayout) findViewById(R.id.yourOutermostLayout);
onTapOutsideBehaviour(layout);
}
private void onTapOutsideBehaviour(View view) {
if(!(view instanceof EditText) || !(view instanceof Button)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard(YourCurrentActivity.this);
return false;
}
});
}
}
\\Function to hide keyboard
private static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
In onTapOutsideBehaviour function here, other that your EditText and Button views, if user clicks anywhere else, it will hide the keyboard. If you have any complex custom layout, you can exclude other views on which if user clicks, it does not hide the keyboard.
It worked for me. Hope it helps you.

Multiple (dynamic) EditText and onFocusChange problems

I generate multiple EditText in a for-loop and want to update a "score" when focus is lost from one of the EditTexts. I also wish to check that the input is valid and if not, empty the EditText and set focus back to it.
The following code adds EditTexts and a onFocusChangeListener.
The problem is that when a score is not valid (updateScore(id) returns false), I want to empty the EditText and set the focus back to this view.
The problem in my code is that if I enter a value in EditText A that are not valid, and then click in EditText B. Both A and B have focus... I only want A to have focus...
How can I set the focus back to the previous EditText and be ready for new inputs to this view?
for (int player = 0; player < numPlayers; player++)
{
EditText valueET = new EditText(this);
valueET.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Log.v("Focus", "Lagrer unna den som har blitt klikket, " + String.valueOf(v.getId()));
etHasFocus = (EditText) v;
}
});
valueET.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
if(!updateScore(v.getId()))
{
if (etHasFocus != null && v.getId() != etHasFocus.getId())
etHasFocus.clearFocus();
v.requestFocus();
}
});
valueET.setId(200+player*100+row);
valueET.setInputType(InputType.TYPE_CLASS_NUMBER);
valueET.setTextColor(Color.BLACK);
valueET.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tr.addView(valueET);
}
tl.addView(tr, new TableLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
The code for updateScore(id) is (simplified):
boolean updateScore(int id)
{
EditText t = (EditText) findViewById(id);
String text = t.getText().toString();
if( !text.equals(""))
{
int score = Integer.parseInt(text);
}
if(score != 9)
return false;
TextView total = (TextView) findViewById(ID_toal);
t2.setText(String.valueOf(score));
return true;
}
Code is updated, problem now is that onClick never is called... (So etHasFocus = null)
You can try keeping a track of which EditText has been clicked. For this, create a EditText variable:
EditText etHasFocus;
In addition to adding OnFocusChangeListener, add an OnClickListener as well:
valueET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
etHasFocus = (EditText) v;
}
});
Now, inside your OnFocusChangeListener, make the following changes:
valueET.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
if(!updateScore(v.getId())) {
// Clear focus from another EditText
if (etHasFocus != null && v.getId() != etHasFocus.getId()) {
etHasFocus.clearFocus();
}
v.requestFocus();
}
}
}
});
Edit:
You are right. the onClick(View) method is called on second click. I can suggest you an alternate approach. I have tried it and its working fine:
Create a EditText variable:
EditText etCurrent;
Set an OnTouchlistener on each EditText:
valueET.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (etCurrent != null) {
if (etCurrent.getId() != v.getId()) {
if (!check(etCurrent.getId())) {
etCurrent.setSelection(0,
etCurrent.getText().toString().length());
return true;
} else {
etCurrent = (EditText) v;
}
}
} else {
etCurrent = (EditText) v;
}
}
return false;
}
});
Remove OnClickListener, onFocusChangeListener and etHasFocus.
You don't need to initialize etCurrent as if (etCurrent != null) { } else { } takes care of that.

Android - Using same dialog for multiple Views - trouble setting up

I'm having some trouble with a simple dialog i'm working on. I have three different TextView's that when they are clicked I have a dialog box pop up with only the Title and one EditText. I'm trying to make it so I use the same custom dialog for all three text views, but depending on which TextView is tapped the dialog set the new value to that TextView. Below is my code:
#Override
protected Dialog onCreateDialog(int id) {
super.onCreateDialog(id);
CustomDialogPercent dialog = null;
switch(id){
case 1:
dialog = new CustomDialogPercent(this, id);
dialog.setTitle("Shipping Percent");
dialog.show();
break;
case 2:
dialog = new CustomDialogPercent(this, id);
dialog.setTitle("Tax Percent");
dialog.show();
break;
case 3:
dialog = new CustomDialogPercent(this, id);
dialog.setTitle("Commission Percent");
dialog.show();
break;
default:
dialog = null;
}
return dialog;
}
private void registerListeners() {
shippingPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(1);
}
});
taxPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(2);
}
});
commissionPercent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showDialog(3);
}
});
}
public class CustomDialogPercent extends Dialog {
int id = 0;
public CustomDialogPercent(Context context, int id) {
super(context);
this.id = id;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_dialog);
basicDialogEntry = (EditText) findViewById(R.id.basic_dialog_entry);
basicDialogEntry.setKeyListener(DigitsKeyListener.getInstance(true,true));
}
#Override
public void onStart() {
super.onStart();
switch(id) {
case 1:
basicDialogEntry.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(basicDialogEntry.getWindowToken(), 0);
shippingPercent.setText(basicDialogEntry.getText().toString());
try {
if ((Float.parseFloat(basicDialogEntry.getText().toString())) < 0) {}
}catch(Exception ex) {
shippingPercent.setText("0");
}
mathCalculations();
CustomDialogPercent.this.dismiss();
}
return false;
}
});
break;
case 2:
basicDialogEntry.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(basicDialogEntry.getWindowToken(), 0);
taxPercent.setText(basicDialogEntry.getText().toString());
try {
if ((Float.parseFloat(basicDialogEntry.getText().toString())) < 0) {}
}catch(Exception ex) {
taxPercent.setText("0");
}
mathCalculations();
CustomDialogPercent.this.dismiss();
}
return false;
}
});
break;
case 3:
basicDialogEntry.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(basicDialogEntry.getWindowToken(), 0);
commissionPercent.setText(basicDialogEntry.getText().toString());
try {
if ((Float.parseFloat(basicDialogEntry.getText().toString())) < 0) {}
}catch(Exception ex) {
commissionPercent.setText("0");
}
mathCalculations();
CustomDialogPercent.this.dismiss();
}
return false;
}
});
break;
}
}
}
OK. With this code what is happening is the first time I tap each TextView they work perfectly. But once I tap any of them again the dialog boxes pop up correctly with the correct title, but values that are set back to the TextView are incorrect.
Example; i enter 8 for the shippingPercent and them 25 for the commissionPercent. And then i go back to shippingPercent and enter 5. The 5 doesn't get set. Instead it will be 25. If i keep using the dialog boxes it seems like my mathCalculations() stops working aswell.
Thanks for all the help.
Maybe you could assign a variable
activeTextView = theTextViewYouWant;
...then update activeTextView with the value you want?

Categories

Resources