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.
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?
Hello I'm new to android developing.
Is there a method in java that equals to #.gotFocus?
Is there in java an events list that I can watch and select like in c# visual studio?
I tried to do #.Focus or something similar but had no success.
I want to reproduce the following scheme:
1- EditText has a certain hint => "Enter a value"
2- The user clicks the edit text and the hint disappears => ""
3- The user fills a certain value => "certain value"
Thank's for helpers :)
Ron Yamin, If I understand your doubt correctly what you want is:
1- Have a field of text for the user to type words/numbers etc --> It is called EditText in android
2- Have an hint so the user knows what to type --> Eg. "Type your name"
3- And react to focus in some way.
The first one you will achieve either through XML or by code. If you have a main.xml in your layouts folder (assuming you are using eclipse/android studio to develop), you can use the interface to drag an edit text to the android screen.
The second one you will achieve still through the XML. If you right click on it, right side of the screen there will be a little window called Proprieties that you can change things like height and width and a hint. Type there your hint.
Finally the last one you need to go to your code in .java and get a reference of your edit text (findViewById).
Either through setOnClickListener or setOnFocusChangeListener.
More info you can checkout here:
http://developer.android.com/guide/topics/ui/controls/text.html
I have googled a tutorial you can check with more detailed information and step by step guide.
Hope it helps:
http://examples.javacodegeeks.com/android/core/widget/edittext/android-edittext-example/
It seems that you changed your question quite a bit, and my C# ignorance got the best of me.
It seems that what you really want is an EditText, the example text you are looking for is the hint.
You can set the hint in the xml file or by code with .setHint(string) method.
Here's where to start:http://developer.android.com/guide/topics/ui/controls/text.html
edit 3 - events in android are dealt with by using listeners. You can use an onClickListener to achieve what you want.
textView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(){
//dostuff
}
}
Assuming your textfield is an instance of EditText (which it probably should be), you can do the following:
textfield.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// this is where you would put your equivalent #.gotFocus logic
}
}
});
It's worth noting that the behavior you've described can be achieved by using textfield.setHint. The hint is text that is cleared automatically when the user selects the EditText. It's designed specifically for the case you describe, e.g. textfield.setHint("Enter a Value")
I'm not familiar with c# but I'm guessing you want event fired when edittext get focus. Try this
EditText txtEdit= (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// do the job here when edittext get focus
}
}
});
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 an EditText that the user can write in, when the app starts there is already a string in the EditText. When the user clicks the EditText it becomes focused and the curser is where the user clicked the EditText text box.
I know that the code for setting the curser to the start is :
editText.setSelection(0);
But I don't know where to put this code, I tried to it in beforeTextChanged but it didn't do the job.
You can do this by setting an putting an OnFocusChangedListener. You'd do something like this:
et.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasFocus){
if(hasFocus){
((EditText)view).setSelection(0);
}
}
});
Where et is the text edit you want to set the listener on.
Full-discolsure: haven't tried this code out myself.
While there is probably a way to do this, I'm not entirely sure it's the best user experience, because when the user taps a text box at a specific spot, they really expect the cursor to be there. Imagine for instance if the user sees "abcd" written there and wants to edit that to "abcde", so they figure "I'll just tap at the end and append an 'e'". Imagine the user's frustration when that doesn't work as expected.
If you expect the user to edit the textbox, I'd consider leaving it empty. If you are using the existing text as a hint ("email#example.com"), it's probably a better idea to indicate that in some other way.