Is it okay to call MutableLiveData's setValue's method rapildy? - android

I have an EditText where I need to hold its value in a MutableLiveData in a viewmodel.
Is it okay to be calling the method setValue("the changed edittext string") every time the user edits something in the EditText?
The reason why I'm doing this is to keep the data alive even if the user for example rotated the screen.
Is there any other way to achieve the same result? Cause I feel like it can be expensive and it's a bad practice calling the method over and over. here's an example in an activity:
public class NoteActivity extends AppCompatActivity {
private ActivityNoteBinding binding;
private NoteViewModel viewmodel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_note);
viewmodel = new ViewModelProvider(this).get(NoteViewModel.class);
addObservers();
addListeners();
}
private void addObservers() {
viewmodel.noteBodyLiveData.observe(this, s -> binding.noteBody.setText(s));
}
private void addListeners() {
binding.noteBody.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
viewmodel.noteBodyLiveData.setValue(String.valueOf(s));
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
The LiveData Objects are initialized with the data corresponding to what's in the database.
Thanks in advance!

Related

How to prevent/stop/disable android SDK EditText exposed to application class

I have an SDK that contains MYSDKActivity. In this activity layout, I have 1 edit text where the user can enter his card number. Any android Client who is integrating this SDK, can use ActivityLifecycleCallbacks in their application class and register text watcher on this edit text and can listen to user card number input.
I want to prevent SDK's edit text usage via the Application class. Only my SDK can access this edit text and perform some action. I want to block usage outside my SDK.
public class MyDemoApp extends Application implements Application.ActivityLifecycleCallbacks {
#Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(this);
}
#Override
public void onActivityCreated(#NonNull Activity activity, #Nullable Bundle savedInstanceState) {}
#Override
public void onActivityStarted(#NonNull Activity activity) {}
#Override
public void onActivityResumed(#NonNull Activity activity) {
if (activity instanceof MYSDKActivity) {
final TextInputLayout textInputLayout = activity.findViewById(R.id.my_sdk_edit_text);
if (textInputLayout != null) {
ViewGroup textInputLayoutViewGroup = ((ViewGroup) textInputLayout.getChildAt(0));
for (int i = 0; i < textInputLayoutViewGroup.getChildCount(); i++) {
View child = textInputLayoutViewGroup.getChildAt(i);
if (child instanceof TextInputEditText) {
Log.v("USER Input", ((TextInputEditText) child).getText().toString());
((TextInputEditText) child).addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.v("USER Input", s.toString());
}
#Override
public void afterTextChanged(Editable s) {}
});
}
}
}
}
}
}
The best way to achieve this
Create a custom layout which doesn't exposes methods like gettext and addTextWatcher.
Never store this data as plain string if you are storing this on the store.(Use encryption).

Fragment Refresh When Activity Textview Value changed

I want to know how can i update fragment recyclerview data of fragment when user change the City name.
Suppose I have one textview(which holds the city name) in my activity I'm sending the value of textview to fragment using intent, for the first time its works fine but when the value of textview changed the fragment recyclerview data wont update according to the textview value(i.e city name).
For better understanding I'm giving an example.
Suppose in my textview city name is set to MUMBAI for the first time the data is fetching perfectly fine from database to recyclerview but when the texview value changed to suppose Pune then the fragment recyclerview data wont get update according to city name.
The working is same as OLX fetching result according to city names.
thank you in advance and hope I explain properly.
I would add a textChangeListener to the textView in the activity, and create a method inside the fragment for receiving the data and updating the fragment.
Something like this perhaps:
Fragment frag = ...
tv.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
...
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
...
}
#Override
public void afterTextChanged(Editable s) {
frag.updateSomething("some argument");
}
});
Create UpdateCallback interface:
interface UpdateCallback {
public void update(String s);
}
Implement update method on fragment:
class MyFragment Extends Fragment implements UpdateCallback {
#Override
public void update(String s){
...
}
}
Finally call method in afterTextChanged():
textView.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
...
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
...
}
#Override
public void afterTextChanged(Editable s) {
myFragment.update(s);
}
});

EditText aftertextchanged throw StackOverflowError

I have an EditText and a TextWatcher. while testing in our test device we never found StackOverflowError, but once we published our app in Google Play Store, we are getting StackOverflowError issue for some user. Why this is happening, I go through some of link but not got the perfect answer. Is anything need to be done in my code.
Skeleton of my code:
weightEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
m_currentWeight = weightEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s)
{
if(!weightEditText.getText().toString().equals("")) {
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2}(\\.[0-9]{0,2}?)?)?$");
Matcher matcher = mPattern.matcher(s.toString());
if (!matcher.find()) {
weightEditText.setText(m_currentWeight);
weightEditText.setSelection(weightEditText.getText().length());
}
}
}
});
To avoid recursion here you need to unregister your textWatcher before setting the text and then reregister it.
Declare the TextWatcher outside the addTextChangedListener(...) method. Then you can do weightEditText.removeTextChangedListener(mWatcher) and weightEditText.addTextChangedListener(mWatcher)
You are trying to call setText() inside of the text watcher which will produce an infinite loop. You can use a flag variable to avoid this.
status variable is set as false by defaut.
status variable indicates whether the TextChange is made by App itself or by the user himself. if it is true, then the TextChange is made by App itself and vice versa.
Try this code. Cheers ! :)
public class MainActivity extends AppCompatActivity {
boolean status=false;//global variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
weightEditText.addTextChangedListener(new TextWatcher()
{
#Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
m_currentWeight = weightEditText.getText().toString();
}
#Override
public void afterTextChanged(Editable s)
{
if(status){
status=false;
return;
}else{
status=true;
if(!weightEditText.getText().toString().equals("")) {
Pattern mPattern = Pattern.compile("^([1-9][0-9]{0,2}(\\.[0-9]{0,2}?)?)?$");
Matcher matcher = mPattern.matcher(s.toString());
if (!matcher.find()) {
weightEditText.setText(m_currentWeight);
weightEditText.setSelection(weightEditText.getText().length());
}
}
}
}
});
}
}

Why cant I call method inside Fragment

Why is it not possible to call the checkButtonAddmethod in my Fragment?
public class AddDataFragment extends Fragment {
DbHelper mydb;
Button buttonadd;
Button buttondelete;
private EditText inputLabel;
//Validator boolean
public boolean labelOk;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_add_data, container, false);
final Context context = getContext();
mydb = new DbHelper(context);
[...]
//Label
inputLabel = (EditText) view.findViewById(R.id.editText_label);
inputLabel.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {
Pattern p = Pattern.compile("^[A-Z]{3}-[0-9]{4}$");
Matcher m = p.matcher(s);
labelOk = m.find();
Log.d("ButtonAdd?", String.valueOf(labelOk));
}
checkButtonAdd(); // <---------------
});
[...]
return view;
}
public void checkButtonAdd(){
if (labelOk){
buttonadd.setEnabled(true);
}else{
buttonadd.setEnabled(false);
}
};
}
I thought this should work because I used it in another Application. But in the MainActivitiy could this be the mistake and if yes, how can I solve it?
You have to call that method from one of the callback methods using this keyword, to get the instance of enclosing Fragment. For instance..
#Override
public void afterTextChanged(Editable s) {
// Your Statements
AddDataFragment.this.checkButtonAdd();
}
No need to declare the method as static. In fact, declaring it static will be a piece of poor code
You are not in the right scope.
Try
AddDataFragment.checkButtonAdd();
instead.

android edittext onchange listener

I know a little bit about TextWatcher but that fires on every character you enter. I want a listener that fires whenever the user finishes editing. Is it possible? Also in TextWatcher I get an instance of Editable but I need an instance of EditText. How do I get that?
EDIT: the second question is more important. Please answer that.
First, you can see if the user finished editing the text if the EditText loses focus or if the user presses the done button (this depends on your implementation and on what fits the best for you).
Second, you can't get an EditText instance within the TextWatcher only if you have declared the EditText as an instance object. Even though you shouldn't edit the EditText within the TextWatcher because it is not safe.
EDIT:
To be able to get the EditText instance into your TextWatcher implementation, you should try something like this:
public class YourClass extends Activity {
private EditText yourEditText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
yourEditText = (EditText) findViewById(R.id.yourEditTextId);
yourEditText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
// you can call or do what you want with your EditText here
// yourEditText...
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
}
Note that the above sample might have some errors but I just wanted to show you an example.
It was bothering me that implementing a listener for all of my EditText fields required me to have ugly, verbose code so I wrote the below class. May be useful to anyone stumbling upon this.
public abstract class TextChangedListener<T> implements TextWatcher {
private T target;
public TextChangedListener(T target) {
this.target = target;
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {
this.onTextChanged(target, s);
}
public abstract void onTextChanged(T target, Editable s);
}
Now implementing a listener is a little bit cleaner.
editText.addTextChangedListener(new TextChangedListener<EditText>(editText) {
#Override
public void onTextChanged(EditText target, Editable s) {
//Do stuff
}
});
As for how often it fires, one could maybe implement a check to run their desired code in //Do stuff after a given a
Anyone using ButterKnife. You can use like:
#OnTextChanged(R.id.zip_code)
void onZipCodeTextChanged(CharSequence zipCode, int start, int count, int after) {
}
I have done it using AutotextView:
AutotextView textView = (AutotextView) findViewById(R.id.autotextview);
textView.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
seq = cs;
}
#Override
public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) {
}
#Override
public void afterTextChanged(Editable arg0) {
new SearchTask().execute(seq.toString().trim());
}
});
myTextBox.addTextChangedListener(new TextWatcher() {
  public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
myOutputBox.setText(s);
}
});
TextWatcher didn't work for me as it kept firing for every EditText and messing up each others values.
Here is my solution:
public class ConsultantTSView extends Activity {
.....
//Submit is called when I push submit button.
//I wanted to retrieve all EditText(tsHours) values in my HoursList
public void submit(View view){
ListView TSDateListView = (ListView) findViewById(R.id.hoursList);
String value = ((EditText) TSDateListView.getChildAt(0).findViewById(R.id.tsHours)).getText().toString();
}
}
Hence by using the getChildAt(xx) method you can retrieve any item in the ListView and get the individual item using findViewById. And it will then give the most recent value.
As far as I can think bout it, there's only two ways you can do it. How can you know the user has finished writing a word? Either on focus lost, or clicking on an "ok" button. There's no way on my mind you can know the user pressed the last character...
So call onFocusChange(View v, boolean hasFocus) or add a button and a click listener to it.
The Watcher method fires on every character input.
So, I built this code based on onFocusChange method:
public static boolean comS(String s1,String s2){
if (s1.length()==s2.length()){
int l=s1.length();
for (int i=0;i<l;i++){
if (s1.charAt(i)!=s2.charAt(i))return false;
}
return true;
}
return false;
}
public void onChange(final EditText EdTe, final Runnable FRun){
class finalS{String s="";}
final finalS dat=new finalS();
EdTe.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {dat.s=""+EdTe.getText();}
else if (!comS(dat.s,""+EdTe.getText())){(new Handler()).post(FRun);}
}
});
}
To using it, just call like this:
onChange(YourEditText, new Runnable(){public void run(){
// V V YOUR WORK HERE
}}
);
You can ignore the comS function by replace the !comS(dat.s,""+EdTe.getText()) with !equal function. However the equal function itself some time work not correctly in run time.
The onChange listener will remember old data of EditText when user focus typing, and then compare the new data when user lose focus or jump to other input. If comparing old String not same new String, it fires the work.
If you only have 1 EditText, then u will need to make a ClearFocus function by making an Ultimate Secret Transparent Micro EditText 😸 outside the windows 😽 and request focus to it, then hide the keyboard via Import Method Manager.
In Kotlin Android EditText listener is set using,
val searchTo : EditText = findViewById(R.id.searchTo)
searchTo.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
// you can call or do what you want with your EditText here
// yourEditText...
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
})
I have taken the solution from #RNGuy thanks for that!
And changed the listener a bit so now it will only accept integers by updating the textView.
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
public abstract class NumberChangedListener implements TextWatcher {
private final EditText target;
private final String defaultValue;
public NumberChangedListener(EditText target, int defaultValue) {
this.target = target;
this.defaultValue = defaultValue + "";
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
#Override
public void afterTextChanged(Editable s) {
this.onTextChanged(target, s);
}
private void onTextChanged(EditText target, Editable s) {
String input = s.toString();
String number = input.replaceAll("[^\\d]", "");
if (!number.equals(input)) {
target.setText(number);
return;
}
Integer integer;
try {
integer = Integer.valueOf(number);
} catch (NumberFormatException ignored) {
target.setText(defaultValue);
return;
}
if (!integer.toString().equals(number)) {
target.setText(integer.toString());
return;
}
onNumberChanged(integer);
}
public abstract void onNumberChanged(int value);
}
and use as
int defaultVal = 10;
mTextView.addTextChangedListener(new NumberChangedListener(mTextView, defaultVal) {
#Override
public void onNumberChanged(int value) {
// use the parsed int
}
});

Categories

Resources