Hi im trying to add on click listener to editText so i can disable the softkeyboard when user clicks on edittext using this code below, how to do that?
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
First it needs to be focusable...
<EditText
...
android:inputType="none"
android:focusable="false"
... />
You have to implement it in your code and than just add this to get an click listener...
myEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// hide the keyboard
// show own keyboard or buttons
}
});
try it and set OnClickListener
<androidx.appcompat.widget.AppCompatEditText
android:id="#+id/edt"
android:inputType="none"
android:focusable="false"
android:editable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The easiest and straight forward way you can set it is by editing the xml file as follows:
android:onClick="onClickMyEditText"
and define the same method in Activity class:
public void onClickMyEditText(View view) {
//your code here
}
Related
I'm using Date and Time Picker to handle the meeting date field, so there is no need for the keyboard. How can I prevent the keyboard from showing after clicking on the field?enter image description here
There is a better way of programatically hiding the keyboard.
Go in xml and add the following attribute android:focusable="false"
E.g
<EditText
android:id="#+id/time_date_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/prompt_light"
android:focusable="false"
android:textSize="17sp" />
The above attribute ensures that the keyboard won't appear!
You can use property android:focusableInTouchMode="false" in xml
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:inputType="text|date"/>
And add click listener to this view in code
editText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//show date picker
}
});
val activity: Activity = this //if you are in the Activity
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = activity.currentFocus
if (view == null) {
view = View(activity)
}
imm.hideSoftInputFromWindow(view.windowToken, 0)
this piece of code should hide your virtual keyboard. Just put it in a method and call. :)
I have set the onClick property of an EditText which is located in a fragment:
<EditText android:id="#+id/edittext1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:inputType="none" android:maxLines="1"
android:singleLine="true" android:layout_marginTop="16dp"
android:focusable="false"
android:longClickable="false"
android:clickable="true"
android:onClick="doSomething"
android:cursorVisible="false"
android:editable="false">
Then in the fragment class I have:
public void doSomething(View view) {
//show dialogfragment...
}
But the method doSomething is grayed out and I get the warning 'Method doSomething is never used'.
Note: This code was originally in an activity and was working fine.
Is there another way to handle onClick in fragments?
first initialize an EditText instance in the top of your fragment
EditText et;
then
in your onCreateView()
add:
etEmployee=(EditText)rootView.findViewById(R.id.edittext1);
then implement your onClickListener()
et.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//what ever you need to do goes here
}
});
Note: in fragments you have to refer to the inflatedView to be able to access your editText in this case rootView.
also the code you are using doesn't work becouse here you are using fragments and using onClick attribute in your xml would only make you able to use it in the MainActivity that contain your fragment.
hope this will help.
If you add android:onClick="doSomething" to your fragment then method will be invoked in your activity but not in Activity. If you want the callback in your fragment add through pragmatically.
edittext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code.
}
});
You can always try to add a OnClickListener to the EditText if it does not matter to you in which way the function gets called.
edittext1 = (EditText) view.findViewById(R.id.edittext1);
edittext1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
doSomething();
}
});
In your layout XML try specifying a tools:context="com.mypackage.path.MyFragment". Don't know if it'll fix it for 'onClick', but it has fixed similar issues for me in the past.
This goes in the top most 'ViewGroup' in your layout file, example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.mypackage.path.MyFragment" />
I have an Activity with an EditText and a Button.
When the User clicks on the EditText, the keyboard is shown and he can type in some Text - fine.
But when the user clicks on the Button I want the EditText to be no more in focus i.e. the keyboard hides til the user clicks again on the EditText.
What can I do to 'hide the focus' of the EditText, after the Button is clicked.
Some Code I can add in the OnClick Method of the Button to do that?
EDIT:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt_SearchDest"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:textSize="18sp"
android:hint="Enter your look-up here.." />
<Button
android:id="#+id/btn_SearchDest"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Search" />
</LinearLayout>
Best Regards
Put this in your button listener:
InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
EDIT
The solution above will break your app if no EditText is focused on. Modify your code like this:
add this method to you class:
public static void hideSoftKeyboard (Activity activity, View view)
{
InputMethodManager imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
}
Then, in your button listener, call the method like this:
hideSoftKeyboard(MainActivity.this, v); // MainActivity is the name of the class and v is the View parameter used in the button listener method onClick.
One workaround is to create a fake view to transfer focus to when you clearFocus in your edittext:
<EditText
android:id="#+id/edt_thief"
android:layout_width="0dp"
android:layout_height="0dp"
android:focusable="true"
android:focusableInTouchMode="true"
Note that this view is invisible so it doesn't require any space in the layout.
In the control class, you can add a method like the following to trigger this focus transfer:
public void clearFocus(){
yourEdittext.clearFocus();
edtThief.requestFocus();
}
You can then minimize the keyboard once edtThief has focus:
public static void hideKeyboard(final View view) {
InputMethodManager imm = (InputMethodManager) view.getContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
I've successfully used the following in the onClick button code:
editText.setEnabled(false);
editText.setEnabled(true);
Somewhat less complex than other methods...
The most elegant solution that I could find is this:
You can save this method in your utility class:
public static void hideSoftKeyboard(Activity activity) {
if (activity == null) return;
if (activity.getCurrentFocus() == null) return;
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
By simply calling hideSoftKeyboad() method it will hide the keyboard but as you can see, the focus will still be present.
In order to remove the focus we will use a simple trick. Right above your input controls, add a dummy view like this:
<View
android:id="#+id/dummy"
android:layout_width="1dp"
android:layout_height="1dp"
android:focusable="true"
android:focusableInTouchMode="true" />
Then, write this line of code at the place where you call the focus-hiding method:
theTextView.clearFocus();
Since the app needs to pass the focus to the next control it will be passed to our invisible view.
How i solved it.
// xml file
<LinearLayout
...
android:id="#+id/linear_layout"
android:focusableInTouchMode="true"> // 1. make this focusableInTouchMode...
</LinearLayout>
// Activity file
private LinearLayout mLinearLayout; // 2. parent layout element
private Button mButton;
mLinearLayout = findViewById(R.id.linear_layout);
mButton = findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mLinearLayout.requestFocus(); // 3. request focus
}
});
I hope this helps you :)
The top answer definitely works but would add a lot of unnecessary codes in my use case, where there are many buttons and every one of them will need a setOnClickListener code block to remove focus from the EditText.
Instead, my approach is to write a BindingAdapter to perform both focus change and the intended click action.
BindingAdapter
#BindingAdapter("onClickWithFocusChange")
fun View.setOnClickWithFocusChangeListener(clickListener: View.OnClickListener?) {
clickListener?.also {
setOnClickListener(OnClickWithFocusChangeListener(it))
} ?: setOnClickListener(null)
}
class OnClickWithFocusChangeListener(
private val clickListener: View.OnClickListener
) : View.OnClickListener {
override fun onClick(v: View?) {
v?.requestFocusFromTouch()
clickListener.onClick(v)
v?.clearFocus()
}
}
In xml (databinding can now be used instead of programmatically setting every one of the clicklisteners):
<!-- parentview of the EditText -->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true">
<ImageButton
...
onClickWithFocusChange="#{() -> viewModel.buttonClicked()}"
... />
In activity/fragment:
editText.setOnFocusChangeListener { v, hasFocus ->
if (!hasFocus) {
requireContext().hideKeyboard(v)
v.clearFocus()
}
}
And lastly the extension function:
fun Context.hideKeyboard(view: View) {
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
Hope this helps some one!
<LinearLayout
android:id="#+id/parent"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:textSize="18sp"
android:hint="Enter your look-up here.." />
<Button
android:id="#+id/btn"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:text="Search" />
</LinearLayout>
set android:focusableInTouchMode="true" to the parent layout.
on button click transfer the focus to parent.
binding.btn.setOnClickListener {
binding.parent.requestFocus()
// your stuff here
}
private void hideDefaultKeyboard() {
activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);//u have got lot of methods here
}
EDIT:
LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
mTextView.setEnabled(!mTextView.isEnabled());
mTextView.setEnabled(!mTextView.isEnabled());
Sorry late to the answer, but I hope this will be the right answer, as I fixed it using
try {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
if (v != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
}
} catch (Exception ignored) {
}
mEditText.setSelected(false);
mEditText.setFocusable(false);
mEditText.setFocusableInTouchMode(true);
Write the following snippet on your button click.
Why not just disable the EditText in the Button code? That should get rid of the keyboard and the focus.
edt_SearchDest.setEnabled(false);
If you are trying to create a button like in a notes app, just do the following:
note.setEnabled(false);
note.setEnabled(true);
This will make a kind of checkmark like button (hides keyboard and removes cursor).
If i click on my EditText, the virtual keyboard simple not shows up. The cursor is shown, but no keyboard to type on.
I even tried it with manually open but just no works.
Here is my code:
public class CreateNote extends Activity {
EditText titleEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.createnote);
titleEdit = (EditText) findViewById(R.id.titleEdit);
titleEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) CreateNote.this
.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(titleEdit, 0);
}
});
}
}
Snippet of Layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#989898" >
<EditText
android:id="#+id/titleEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittextdrawale"
android:ems="10"
android:textColor="#fff"
android:textColorHint="#fff" >
<requestFocus />
</EditText>
</FrameLayout>
What could be the reason of playing hide and seek of my virtual keyboard ?
I test on real device, not on emulator.
Try with this, it worked for me.
EditText etHorseName = (EditText) getView().findViewById(R.id.horseName);
etHorseName.clearFocus();
in onCreate() or where you want.
Late answer but here is how to solve it without adding code, just remove this from your XML:
<requestFocus />
No idea why the keyboard does not show up when this is set... It does show up however if you first loose the focus and then click on the edit text. I had the problem on Android 2.3.6 but it worked on 4.1.2, so maybe it was an early bug.
It is just a default behavior , you not suppose to do it manually, remove below part from your code.
titleEdit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) CreateNote.this
.getSystemService(Service.INPUT_METHOD_SERVICE);
imm.showSoftInput(titleEdit, 0);
}
});
Try to hide and show the keyboard with this code:
InputMethodManager imm = (InputMethodManager) this.getSystemService(Service.INPUT_METHOD_SERVICE);
// To show keyboard
imm.showSoftInput(titleEdit, 0);
// To hide keyboard
imm.hideSoftInputFromWindow(titleEdit.getWindowToken(), 0);
My XML contains Five EditText box and One Button. My cursor is now pointed to the First EditText box. How can I click a button to place the cursor automaticly to Third EditText box.
Thank you!
on your button's onClick() put..
thirdEditText.requestFocus();
Something like,
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
thirdEditText.requestFocus();
}
});
editText3.requestFocus();
add in onClick method of button.
Use requestFocus() method to gain focus.
Or put < requestFocus/> in your XML layout.
this is the code:
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
}
});
try
EditText editText = (EditText) findViewById(R.id.textId);
editText.requestFocus();
just add <requestFocus/> tag in the EditText like:
<EditText
android:id="#+id/editText"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_below="#id/label"
android:inputType="numberDecimal"
android:textSize="25dp" >
<requestFocus />
</EditText>