I'm overriding the onFocusChanged to pinpoint the TextEdit that just lost focus. The point is to remove this TextEdit if it doesn't have any text in it. The problem is that the if((EditText... is not valid. I get cannot resolve method 'getText()'. I've tried casting it to tell that it's a EditText view.
listItemsView is the id of the LinearLayout and listItems is an ArrayList of EditText.
#Override
public void onFocusChange(View view, boolean hasFocus) {
if (!hasFocus) {
int removeCurrent = listItems.indexOf(this)+1;
// code to execute when EditText loses focus
if((EditText)view.getText().equals("")) {
}
listItemsView.removeViewAt(removeCurrent);
listItems.remove(removeCurrent);
}
}
I've also tried:
if((EditText)listItemsView.getChildAt(removeCurrent).getText().equals(""))
And got the same result.
When you want to cast a value to a type, you do this:
(T)V
Where T is the type you want to cast to, and V is the value.
So far so good?
In your code,
(EditText)view.getText().equals("")
What is V and what is T? The answer might surprise you! T is obviously EditText, but V here is actually view.getText().equals("")!
In other words, you did not cast view to EditText. That's why the compiler can't find a method named getText. To tell the compiler to specifically cast view, add parentheses:
((EditText)view).getText().equals("")
Related
I have an EditText wrapped in a TextInputLayout. The TextInputLayout has an option for maximum length. Example below:
app:counterMaxLength="12"
Is it possible to have an option for counterMinLength? For example, if I am entering a password where the length cannot be less than a certain length.
I don't think it has an attribute like that, because when activity created it has an empty string value which breaks "min length" constraint.
So to resolve that I think you should verify the length in java/kotlin code like:
if(editText.getText().toString().length() > MIN_VALUE) {
// do somthing
}
I dont think that they are supporting this option at the moment :)
But I think if you want you can easily create a custom Edittext and then validate the minimum number of input field by using
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
if (edt.getText().toString().trim().length() < 8) {
edt.setError("Minimum length exception");
}
}
or you can you addTextChangedListener for your edittext
Something like that. ^^
I am inflating edit text in adapter class within of a list view. View is looking cool multiple edit text are appearing properly but when I do focus on the edit text or if I try to type something it loosing focus I tried to adding listeners, changing manifest file but nothing works for me.
following code is not working for me
#Override
public View getChildView(int position, int i1, boolean b, View view,
ViewGroup viewGroup) {
EditText comment = view.findViewById(R.id.txtRecordComment);
comment.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
if (lastFocussedPosition == -1 || lastFocussedPosition == position) {
lastFocussedPosition = position;
edittext.requestFocus();
}
}
}, 200);
} else {
lastFocussedPosition = -1;
}
}
});
return view;
}
please help me to resolve this issue
Since you are using a ListView, it is bound to happen with your current code implementation.
You must remember that if your EditText is in a ListView, every key down will result in the edit text losing and getting focus because all the views are redrawn, so the edit text representing whatever row used to be focused is now a completely different object.
To get the expected behaviour, declare a variable in your adapter: int focusedRow. In getView method of your adapter, add an onFocusChanged listener to the EditText and when that edit text gains focus, set focusedRow = whatever row the focused EditText happens to be in.
Also set any edit text that is in the currentlyFocusedRow to be focused.
Update:
If you have multiple EditTexts, add an onFocusChanged listener to each edit text.
Add below code in Android Manifest file
<activity android:name=".ActivityName"
android:label="#string/app_name"
android:windowSoftInputMode="adjustPan">
Hope this will work
I'm using Butterknife(8.4.0) to instantiate my views in a fragment that has several EditTexts.
I'm using these EditTexts to set a string to a specific Model attribute. I don't want to create loads of bind methods for each Edittext so on #onTextChanged I pass in all the edit texts. I'm only interested in AfterTextChanged() so I've also passed that in. I then use the editText ids to specify which model attribute should be set.
#BindView(R.id.edit_text_one) EditText textFieldOne;
#BindView(R.id.edit_text_two) EditText textFieldTwo;
#OnTextChanged(value = {R.id.edit_text_one, R.id.edit_text_two}, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void setEditTextFields(EditText editText, Editable editable) {
switch (editText.getId()) {
case R.id.edit_text_one:
myModel.setStringOne(editable.toString());
break;
case R.id.edit_text_two:
myModel.setStringTwo(editable.toString());
break;
}
}
However I'm getting a compile error
Error:(117, 10) error: #OnTextChanged methods can have at most 1 parameter(s). (com.skeeno.android.gamecabinet.Fragment.EditorFragment.setEditTextFields)
I've read here that you just pass in the view as the first argument but that doesn't seem to work since AfterTextChanged is only expecting an editable.
Is there a way to do this?
Any help will be greatly appreciated. Thanks.
Passing View with #onTextChanged is not possible currently. I tried too.
However, below code can be used to get the current view,
//Inside a fragment
View view = getActivity().getCurrentFocus();
Hope this helped.
It is a little bit late but if anyone will have this issue, answer can be helpful therefore, here how i make it work. You cannot send two parameter method for AFTER_TEXT_CHANGED therefore; you need to change method void setEditTextFields(EditText editText, Editable editable) to void setEditTextFields(Editable editable). In this case you need to write #OnTextChanged for all EditText you want to listen.
Here is final code should be;
#BindView(R.id.edit_text_one) EditText textFieldOne;
#BindView(R.id.edit_text_two) EditText textFieldTwo;
#OnTextChanged(value =R.id.edit_text_one, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void edit_text_oneChanged(Editable editable) {
myModel.setStringOne(editable.toString());
}
#OnTextChanged(value =R.id.edit_text_two, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void edit_text_twoChanged(Editable editable) {
myModel.setStringTwo(editable.toString());
}
In my activity I have the following views
TextView player1;
TextView player2;
TextView player3;
TextView player4;
EditText player1name;
EditText player2name;
EditText player3name;
EditText player4name;
Each of the TextView's has the onclick listener applied to it. and so fires the OnClick function.
When we get to the onClick this is what i am currently doing:
#Override
public void onClick(View v) {
//the v variable is the clicked textview, in this case "player1"
//hide the textview and show the resultant edittext
v.setVisibility(View.GONE);
player1name.setVisibility(View.VISIBLE);
//set focus on edit text and when focus is lost hide it and set the textview text
player1name.requestFocus();
imm.showSoftInput(player1name, InputMethodManager.SHOW_FORCED);
player1name.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View y, boolean x) {
v.setVisibility(View.VISIBLE);
player1name.setVisibility(View.GONE);
imm.hideSoftInputFromWindow(player1name.getWindowToken(), 0);
String name = player1name.getText().toString();
if (name.equals("")) {
v.setText("Player Name1");
} else {
v.setText(name);
}
}
});
}
However with this solution I will need to duplicate this code and change the view names for player2 - player2name, player3 - player3name etc
i can obviously grab the clicked TextView via v, however what i cant seem to do is grab its corresponding EditText.
i had thought of doing this:
View test = v + "name";
//then i replace all references to player1name with the test variable
but it doesnt work it wants me to convert View test; into a string
any suggestions?
EDIT: made it easier to understand my question
View test = v + "name";
will give a compile error. Because "v" is not a string type. and also even if it was String, test is not. This line is pretty wrong.
There a few options to achieve what you want,
You can use hashmap
Declare a global field for hashmap
private final HashMap<Integer,EditText> map = new HashMap<Integer,EditText>();
and in onCreate method put your textview id as key, and put your edittext variables in value.
player1name = (EditText) findViewById(R.id.player1name);
map.put(R.id.textView1, player1name);
// for the rest
in onClick method
EditText e = map.get(v.getId());
Then replace them with "e"
e.requestFocus(); //example
Will you please state your problem clearly? Currently, your language is very ambiguous and I can not figure out, exactly what are you looking for. It will help us to know your problem and in turn solve it.
with this code, my program just force close(error)
***public View x = findViewById(R.string.nfoname);***
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
//edittext
***final EditText infoname=(EditText)findViewById(R.id.infoname);***
//clear,confirm
Button clear = (Button)findViewById(R.id.buttonclear);
Button confirm = (Button)findViewById(R.id.buttonconfirm);
//clear button
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
infoname.setText("");
}
});
//confirm button
confirm.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
***x=(View) infoname.getText();***
}
});
}
the one with the * are the source of error
program function:
if the user clicks confirm, his name will be set to R.string.nfoname
which will then be used in another layout through TextView x = setText(R.string.nfoname);
I am not sure that you can save text to the R.string. This is a generated class that the compiler creates for you. It gets packaged with your apk. Think of the resources as a means of translation and to present text to the screen.
I think what you would want to do is save the user input as a SharedPreference or in a database.
See:SharedPreferences on the android docs for an example usage.
At least in the case of your variable infoname scoping is most likely causing your application to throw an error. infoname is a local variable to the function onCreate(), not an instance variable for your class, so it can't be accessed by your onClick() methods because they are part of an anonymous class.
Another thing I'd question is why you marked infoname as final? It goes out of scope when onCreate() exits so if it gets changed, you can see who changed it since it only exists while the method is executing.
You cannot set values to R.string.xxx because all these values will be constants much like a read only stuff. If you want to use the value of edit text to another layout use class variables or intent.putextra()
Coming to ur source code i see this
public View x = findViewById(R.string.nfoname);
How can a view be found by R.String? This should be R.id.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Why this editText has to be final?
***x=(View) infoname.getText();***
You just use infoname.getText().toString() you will get the string value of the Edittext's current text.
Dude you can do stuff simply.
public View x = findViewById(R.string.nfoname);
This can't work as not only are you trying to find a View using a R.string resource id, you are doing it before setContenView(...) is called in your onCreate(...) method. Even if you used a valid View resource id such as R.id.infoname then x will be null because the content view hasn't been inflated yet.
final EditText infoname=(EditText)findViewById(R.id.infoname);
Apart from the pointless use of final this should'nt cause problems as long as R.id.infoname is actually the resource id of an EditText.
x=(View) infoname.getText();
Not only will x be null but calling getText() on an EditText returns an Editable which is not a View nor is it possible to cast it to View. Even if you used getText().toString() which is the correct way to get the text from an EditText it still wouldn't be possible to cast a String to a View.
Also, as for this...
TextView x = setText(R.string.nfoname);
It would have to be...
TextView x = (TextView) findViewById(<some id>);
x.setText(getString(R.string.nfoname));