I am novice, I am trying to know if my EditText is empty to put a botton enabled or disabled. But when the EditText is empty the button continues appearing enabled. Here is my code.
if((getActivity().findViewById(R.id.textcodigo)).toString().matches("")){
Button aceptar= (Button) getActivity().findViewById(R.id.aceptar);
aceptar.setEnabled(false);
}
the problem is you need findViewById(R.id.textcodigo)).getText().toString().equals("") and not what you currently have
Maybe try
.getText().equals("")
Instead of
.toString().matches("")
Edit complete code :
if(((EditText) getActivity().findViewById(R.id.textcodigo)).toString().matches("")){
Button aceptar= (Button) getActivity().findViewById(R.id.aceptar);
aceptar.setEnabled(false);
}
what you want is .equal("") not matches (which take a regex as parameter)
Related
OnButtonClick listener I want to disable the EditText to edit, and then turn it on again.
With below two lines I disabled EditText
editTextWight.setEnabled(false);
editTextWight.setFocusable(false);
But when I try to turn it back fails. The keyboard does not open.
editTextWight.setFocusable(true);
editTextWight.setFocusableInTouchMode(true);
editTextWight.setClickable(true);
editTextWight.setEnabled(true);
Please help me to enable the EditText.
You can try this two lines that work for my project code:
editTextWight.setFocusable(true);
editTextWight.setFocusableInTouchMode(true);
I do this for example afterTextChanged() is called because I had to disable this EditText first then enable it later!
So, just try it with ONLY two method calls above instead of all the four.
Good luck and let me know if it worked!
Thank you Eenvincible and sajan. Your answers togheter help me to solve my problem. I enabled the editing window with these 4 methods.
editTextWight.setEnabled(true);
editTextWight.setInputType(InputType.TYPE_CLASS_NUMBER);
editTextWight.setFocusable(true);
editTextWight.setFocusableInTouchMode(true);
to disable edit text use
editTextWight.setEnabled(false);
editTextWight.setInputType(InputType.TYPE_NULL);
editTextWight.setFocusable(false);
to enable edit text use
editTextWight.setEnabled(true);
editTextWight.setInputType(InputType.TYPE_CLASS_TEXT);
editTextWight.setFocusable(true);
I'm looking for an option to do something when EditText is touched, but I want it to be writeable, cause I found few solutions for it, but then the EditText turned to be unwriteable and I could only click it.
EditText ed == //to its reference
ed.setFocusableInTouchMode(true);
ed.setOnTouchListener(/*add a listener */);
//also remember to add ed.requestFocus(); in your listener
i guess the above should work right?
I have a screen with textviews now i want to make this editable on click of that
i tried one solution using edittext making it as transparent background but initially it will show cursor and the click is not recognizing properly,if i set focusbaleintouchmode to false in xml it is not getting focus.but some how the click is not working properly as expected.first is this correct approach?
expected result is textview should be there once user clicks on it it should be editable once user clicks outside it it should be not editable. any sample code will helps me a lot.sorry for my english
Thanks in advance
finally i got one solution using below code
in xml edit text i gave foucasbletouchmode to false which makes click works properly after that with in onclick
et.setFocusable(true);
et.setEnabled(true);
et.setFocusableInTouchMode(true);
et.requestFocus();
to lose focus
et.setFocusable(false);
et.setClickable(true);
et.clearFocus();
You can use the below code :
private makeEditable(boolean isEditable,EditText et){
if(isEditable){
et.setBackgroundDrawable("Give the textbox background here");//You can store it in some variable and use it over here while making non editable.
et.setFocusable(true);
et.setEnabled(true);
et.setClickable(true);
et.setFocusableInTouchMode(true);
et.setKeyListener("Set edit text key listener here"); //You can store it in some variable and use it over here while making non editable.
}else{
et.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
et.setFocusable(false);
et.setClickable(false);
et.setFocusableInTouchMode(false);
et.setEnabled(false);
et.setKeyListener(null);
}
}
I have a TextView with the following assignment code:
Answer1TextView = (TextView) findViewById(R.id.editText1);
Answer1TextView.setOnClickListener(answer1TextViewListener);
and here is my onClickListener:
private OnClickListener answer1TextViewListener = new OnClickListener()
{
public void onClick(View v)
{
if(Answer1Win){
Toast.makeText(QuizScreen.this,"Correct ",2).show();
} else
{
Toast.makeText(QuizScreen.this,"Incorrect, Pick Another Answer",2).show();
}
}
};
My problem is the Toast only is displayed after a double tap. I cannot find a setting the drives this behavior, what could be set wrong to not display after a single tap.
The first click just sets the focus to the TextBox then the second click actually gets handled as a click. Rather than using an onClickListener, you may have better luck with an onFocusChangeListener
As Chris said, the first tap focuses the TextView and the second tap clicks it.
Setting android:focusableInTouchMode="false" fixes the problem for touchscreens but without breaking functionality for non-touchscreen devices.
If you were to simply use android:focusable="false" that would prevent, for example, d-pad users from clicking the view at all.
The issue may be that textIsSelectable is true. Set textIsSelectable="false" for the TextView in XML.
The correct way to do that is android:clickable="true"
use OnTouchListener instead onFocusListener triggers twice when you enter and leaves the key
I have 2 EditTexts; 01 and 02. My button will be disabled once the activity is started and when these two EditText contain text, the button has to be enabled again. However my button is always disabled and can't enable it using button.setEnabled(true);.
Can anyone help me with this?
summit.setEnabled(false);
buttonEnable();
public void buttonEnable(){
if (feedback.length()>0 && email.length()>0){
summit.setEnabled(true);
}else{
summit.setEnabled(false);
}
}
You're correct about needing a TextWatcher. The afterTextChanged(Editable) method is the one you're interested in for something like this. Call your buttonEnable() method from it, and add the TextWatcher to any applicable text fields. (Looks like feedback and email from your sample.)
One easy way can also be to set onKeyListener to your editText(), then if there is something in editText(), set button enable if nothing disable it.