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

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.

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.

Hide Keyboard and show it by button

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);
}
}
});

Android single button multiple functions

I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}

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.

Keyboard not shown when i click on edittextview in android?

When i click on the edittextview then some times keyboard shown or some times keyboard are not shown.
In android 2.1 it show the keyboard when i click on the edittextview
but when i start same application it on android 2.2 then it not show the keyboard.
Help me how to show that problem.
OK, This might be a late response, but it worked.
I have met this problem on android 2.1 and 2.3.x(not tested on other versions of SDKs).
I noticed a strange thing that when my click on the EditText was unable to open the keyboard, I pressed the BACK button to show an alert dialog and then I canceled(closed) it, and clicked the EditText again, now the keyboard was brought to life again.
From that I can conclude that the keyboard will always show for the EditText if the EditText does not previously own focus(showing an alert dialog over the EditText view will make the EditText to lose focus).
so call the function below on your EditText when it is brought to front:
mEditText.clearFocus();
or
parentViewThatContainsEditTextView.clearFocus();
I had a similar problem on Galaxy S3 (displaying EditText controls on a PopupWindow - the keyboard was never showing). This solved my issue:
final PopupWindow popUp = new PopupWindow(vbl.getMainLayout());
[....]
popUp.setFocusable(true);
popUp.update();
I didn't want to EditText lose a focus using editText.clearFocus(). Came up to this solution.
#Override
public void onResume() {
super.onResume();
if (Build.VERSION.SDK_INT < 11) {
editText.clearFocus();
editText.requestFocus();
}
}
here's a possible solution:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(final View v, final boolean hasFocus) {
if (hasFocus && editText.isEnabled() && editText.isFocusable()) {
editText.post(new Runnable() {
#Override
public void run() {
final InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText,InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
code is based on the next link:
http://turbomanage.wordpress.com/2012/05/02/show-soft-keyboard-automatically-when-edittext-receives-focus/
In my case it was in a PopupWindow and I simply needed to call popupWindow.setFocusable(true)
I had this same problem when displaying an EditText in a DialogFragment. Despite the EditText getting focus (i.e., when clicked, it showed the flashing caret), the keyboard did not display.
My solution was to add a dummy EditText to the uppermost view of my DialogFragment.
<EditText
android:id="#+id/editTextFix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/fix"
android:importantForAutofill="no"
tools:targetApi="o"
android:inputType="text"
android:visibility="gone"/>
Possible scenarios:
1) On clicking the EditText, usually the keyboard comes up. But if you press the back key button in the emulator the keyboard (not the screen keyboard) dimisses.
2) In code you can disable the keyboard on clicking the EditText by setting a flag.
InputMethodManager inputmethodmgr= (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputmethodmgr.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
It works like a charm, In a case if you even want to hide on click of the edittextView.
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displayKeyboard();
}
});
private void displayKeyboard(){
if (textView != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInputFromWindow(textView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
}
}
In your parent view check if there is android:descendantFocusability="blocksDescendants"
remove it.

Categories

Resources