How to do something when EditText is activated - android

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?

Related

know if EditText empty

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)

Editable textview on click

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

TextView onClick not responding to single tap

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

How can I react when an EditText is being focused?

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.

Android: How to enable my button back if EditText is not empty?

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.

Categories

Resources