Hide Keyboard and show it by button - android

I am creating a program that write in EditText by barcode reader so I don't want to show the keyboard immediately even if I focused on it I don't wanna it to be visible , I need to press a button to show keyboard only to Edit sometimes .
and thanks

I would disable the EditText button from the beginning:
editText.setEnabled(false);
And to answer your question, yes. Even if it is disabled, you can change the text. Disabled only means user can't change it. You can programmatically edit it.
Then when the button is pressed:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText.setEnabled(true);
editText.requestFocus();
}
});
This should automatically show the keyboard when the button is pressed.
Bonus:
If you want to disable the EditText once the editing is done, you can do this:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
editText.setEnabled(false);
}
}
});

Related

Android Long Click vs. Default Click

I have an EditText that is focused and a Button that is not focused.
When I click the Button, the EditText loses focus.
When I long-click the Button, the EditText does not lose focus.
Whats the source of this behaviour? I want to achieve the long-click behaviour within a default click, is this possible?
Long click behavior is for by default for ClipBoard actions . If you want to override it with Single click . You can do this as follows.
editText=(EditText)findViewById(R.id.txt);
editText.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Do your stuff
return true;
}
});
editText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.performLongClick();
}
});
If you are doing it to just get rid of Focusing problem then its not the way.

Disabling edittext searchview after user input

How to disable EditText searchview after completion of input on fragment?
You can set View.OnFocusChangeListener to your editText and after focus change call setEnabled(false). for eg.
mEditText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
//let the user input
} else {
//disable you search view
mEditText.setEnabled(false);
}
}
});
P.S. -If you are still facing a problem, please update your code snippet.
If user have to click button after enter text into searchview, you can handle this in event button click method. In your button click method you can write:
search.setEnabled(false);

EditText focus method not working as expected in android [duplicate]

This question already has answers here:
How to make an EditText uneditable/disabled
(11 answers)
Closed 6 years ago.
I have a button "show" and when the button is clicked , the edit text box must not be editable and when I click next, I must give back the focus to edit text box. I tried the below code
showans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusable(false);
}
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusable(true);
editText.setClickable(true);
}
}
I tried changing values between setFocusable and setClickable but nothing helped. When I click the showans button for the first time, the edit box loses focus and its not clickable though I set the value to true when I click the "next" button and all these buttons .All these text boxes and buttons are dynamically generated.Any help would be great !! Thanks
You can use
editText.setInputType(InputType.TYPE_NULL); for uneditable EditText
and
editText.setInputType(InputType.TYPE_CLASS_TEXT)
to make it editable
Please change to editText.requestFocus();
You can use editText.clearFocus();
You can use setEnabled() method to disable or enable the editText
//to disable
editText.setEnabled(false);
//to re enable
editText.setEnabled(true);
You could try using setFocusableInTouchMode - so your code will change to something like this:
showans.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
}
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setFocusable(true);
editText.setClickable(true);
editText.setFocusableInTouchMode(true);
}
}
Give this a try and let me know if it helps.

Single click edittext

How can make an EditText have a onClick event so that on single click an action is done.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
}
this is not working as excepted....single click gives just the onscreen keypad but not the datepicker dialog which appears only if i double click
if we just add android:focusableInTouchMode="false" in edittext on layout page it should work in a singleclick on its onclicklistener. no need to handle onFocusChangeListener.
Change your code from an onClickListener to an OnFocusChangeListener.
private void addListenerOnButton() {
dateChanger = (EditText) findViewById(R.id.date_iWant);
dateChanger.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
showDialog(DATE_DIALOG_ID);
}
}
});
}
EditText is not meant for singleClick.
I mean you should not use Click Listener with it.
rather you can do like,
Use onFocusChangeListener which is also not 100% correct approach.
Best would be instead the EditText use one TextView write onClick of that and if needed give a background image to that TextView.
Rewrite
I have an EditText that launches a dialog when the user either clicks it once or navigates to it with a trackball / directional pad. I use this approach:
Use an OnFocusChangeListener for gaining focus to open the dialog.
Override dismissDialog() to clear the focus from the EditText when the user closes the dialog, preventing the user from entering text without the dialog (as far as I can tell)
.
I have also tried this (however I now remember this method did respond to trackball movement):
Use an OnClickListener for touch events.
Set setFocusable(false) to prevent user input.
Hope that helps.

Soft-Keyboard intercepting my onClickListener?

In Android I have the following code to blank an EditText box when clicked.
This is to remove any existing information in the box and start with a blank slate.
It sort off works. When you first click in the edit box the soft keyboard appears, but the existing information in the EditBox is still there. If you click a second time in the EditBox the information dissapers.
How can I get it to clear the data on the first click? I am guessing the first click is intercepted by the soft keyboard.
Many thanks for any help.
// Blank EditText field when clicked
myEditBox.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View v) {
tcA.setText("");
}
});
Im not sure, but try clearing it when the EditText gets focus.
myEditBox.setOnFocusChangeListener( new onFocusChangeListener(){
#Override
public void onFocusChange(View arg0, boolean hasFocus){
tcA.setText("");
}
});

Categories

Resources