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);
}
}
Related
I want to get an in-line autocomplete with an EditText, not a result list but the best suggestion directly in EditText.
Something like this : In-line auto-complete (near the bottom of the page).
Is it possible in Android ?
Thank you.
I have no eclipse now so I will try to give you some hints.
To create a custom autocomplete I would do something like this.
First
In the view layout add the EditText and an OutputText (this with visibility=hidden)
Second
In the activity create a TextWatcher and implement the method afterTextChanged.
Inside this method call a service with the input text and then update the content of the outputText.
Something like:
afterTextChanged(Editable s){
// you know your input is an EditText
final EditText input= (EditText) s;
// TODO make this call async
String suggestedText= someService.getSuggestion(input.getString());
outputText.setText(suggestedText);
outputText.setVisibility(View.VISIBLE);
// to avoid infinite loops
if(suggestedText!=null && !"".equals(suggestedText) && !suggestedText.equals(input.getString())
{
// add a onclick control to update the input
outputText.setOnClickListener(new View.OnClickListener(){
editText.setText(suggestedText);
});
}
}
Third
Implement the suggestion service.
Android has an AutoCompleteTextView which should do the job you want.
According to the official Android docs
"AutoCompleteTextView is an editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.
The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold."
For an example code snippet refer to AutoCompleteTextView
For autocomplete you must another type of EditText called AutocompleteTextView or MultiAutocompleteTextView. Here you can find simple example for that option.
P.S. if you want to create your own type of List Filtration, your Adapter class must implement Filterable interface
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)
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 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.
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.