android


Android auto update EditText if others are filled


I'm new to Android and Java programing and I'm working on my first application, but I hit a problem.
I have three EditTexts: FuelAmount, FuelCost, CashSpend and what i want to achieve is - when FA is filled and FC is beeing filed, CS should be auto filled.
I want that to work in all possible combinations FA>FC=CS, FA>CS=FC etc.
So I tried and find a addTextChangedListener to all of my EditTexts. But when I'm testing all ways of input the data the app crashes with
java.lang.StackOverflowError
on combinations like when I put FuelCost and then CashSpend or FuelAmount and then CashSpend.
import android.app.DatePickerDialog;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class NewRefuelActivity extends AppCompatActivity {
EditText KmCounter,FuelAmount, FuelPrice,CashSpend,RefuelDate;
Context context = this;
DBHelper dbHelper;
SQLiteDatabase sqLiteDatabase;
Calendar myCalendar = Calendar.getInstance();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_refuel);
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null){
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
KmCounter = (EditText) findViewById(R.id.NewRefuel_KmCounter_Input);
FuelAmount = (EditText) findViewById(R.id.NewRefuel_FuelAmount_Input);
FuelPrice = (EditText) findViewById(R.id.NewRefuel_FuelPrice_Input);
CashSpend = (EditText) findViewById(R.id.NewRefuel_CashSpend_Input);
RefuelDate = (EditText) findViewById(R.id.NewRefuel_Date_Input);
updateLabel_RefuelDate();
RefuelDate.setFocusableInTouchMode(false);
RefuelDate.setFocusable(false);
final DatePickerDialog.OnDateSetListener DTPListener = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR,year);
myCalendar.set(Calendar.MONTH,monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
updateLabel_RefuelDate();
}
};
RefuelDate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(NewRefuelActivity.this,
DTPListener,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
//error
EditText_AutoFill();
}
public void EditText_AutoFill(){
FuelAmount.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) {
if (s.length() != 0){
if (FuelPrice.getText().toString().trim().length() != 0){
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float CS = FA * FC;
CashSpend.setText(CS.toString());
}
else if (CashSpend.getText().toString().trim().length() != 0){
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FC = CS / FA;
FuelAmount.setText(FC.toString());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
FuelPrice.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) {
if (s.length() != 0) {
if (FuelAmount.getText().toString().trim().length() != 0) {
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float CS = FA * FC;
CashSpend.setText(CS.toString());
} else if (CashSpend.getText().toString().trim().length() != 0) {
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FA = CS / FC;
FuelAmount.setText(FA.toString());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
CashSpend.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) {
if (s.length() != 0) {
if (FuelAmount.getText().toString().trim().length() != 0) {
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FA = Float.parseFloat(FuelAmount.getText().toString());
Float FC = CS / FA;
FuelPrice.setText(FC.toString());
} else if (FuelPrice.getText().toString().trim().length() != 0) {
Float CS = Float.parseFloat(CashSpend.getText().toString());
Float FC = Float.parseFloat(FuelPrice.getText().toString());
Float FA = CS / FC;
FuelAmount.setText(FA.toString());
}
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
private void updateLabel_RefuelDate(){
String myFormat = "dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(myFormat, Locale.US);
RefuelDate.setText(simpleDateFormat.format(myCalendar.getTime()));
}
public void addNewRefuel(){
Float km_counter = Float.parseFloat(KmCounter.getText().toString());
Float fuel_amount = Float.parseFloat(FuelAmount.getText().toString());
Float fuel_price = Float.parseFloat(FuelPrice.getText().toString());
Float cash_spend = Float.parseFloat(CashSpend.getText().toString());
String fuel_date = RefuelDate.getText().toString();
dbHelper = new DBHelper(context);
sqLiteDatabase = dbHelper.getWritableDatabase();
dbHelper.addRefuel(fuel_amount, fuel_price, cash_spend, km_counter, fuel_date, sqLiteDatabase);
Toast.makeText(getBaseContext(),"Tankowanie dodane", Toast.LENGTH_SHORT).show();
dbHelper.close();
}
public void clearRefuelEditText(){
//KmCounter.getText().clear();
KmCounter.setText("");
FuelAmount.setText("");
FuelPrice.setText("");
CashSpend.setText("");
RefuelDate.setText("");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_new_refuel_activity, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_refuel_tab) {
return true;
}
if (id == R.id.action_add_refuel){
addNewRefuel();
clearRefuelEditText();
finish();
//TODO: przetestowac alternatywe finish()
/*
Intent i=new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
*/
return true;
}
case android.R.id.home:
// this takes the user 'back', as if they pressed the left-facing triangle icon on the main android toolbar.
// if this doesn't work as desired, another possibility is to call `finish()` here.
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Can someone suggest me any solution?
Thanks in advance.
Edit. Entire error
04-22 06:28:18.280 1816-1816/? E/MessageQueue-JNI: java.lang.StackOverflowError
at android.text.TextUtils.getChars(TextUtils.java:77)
at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
at android.text.TextUtils.getChars(TextUtils.java:77)
at android.graphics.Paint.getTextRunAdvances(Paint.java:1800)
at android.text.TextLine.handleText(TextLine.java:747)
at android.text.TextLine.handleRun(TextLine.java:956)
at android.text.TextLine.measureRun(TextLine.java:414)
at android.text.TextLine.measure(TextLine.java:293)
at android.text.TextLine.metrics(TextLine.java:267)
at android.text.Layout.getLineExtent(Layout.java:998)
at android.text.Layout.getLineStartPos(Layout.java:504)
at android.text.Layout.getHorizontal(Layout.java:861)
at android.text.Layout.getHorizontal(Layout.java:830)
at android.text.Layout.getPrimaryHorizontal(Layout.java:805)
at android.text.Layout.getPrimaryHorizontal(Layout.java:795)
at android.widget.TextView.getFocusedRect(TextView.java:5135)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:120)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:94)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.View.focusSearch(View.java:6392)
at android.widget.TextView.onCreateInputConnection(TextView.java:5641)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1120)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1070)
at android.widget.TextView.setText(TextView.java:3691)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$5.onTextChanged(NewRefuelActivity.java:162)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$4.onTextChanged(NewRefuelActivity.java:130)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$5.onTextChanged(NewRefuelActivity.java:162)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$4.onTextChanged(NewRefuelActivity.java:130)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$5.onTextChanged(NewRefuelActivity.java:162)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)04-22 06:28:18.280 1816-1816/? E/MessageQueue-JNI: java.lang.StackOverflowError
at android.text.TextUtils.getChars(TextUtils.java:77)
at android.text.method.ReplacementTransformationMethod$ReplacementCharSequence.getChars(ReplacementTransformationMethod.java:151)
at android.text.TextUtils.getChars(TextUtils.java:77)
at android.graphics.Paint.getTextRunAdvances(Paint.java:1800)
at android.text.TextLine.handleText(TextLine.java:747)
at android.text.TextLine.handleRun(TextLine.java:956)
at android.text.TextLine.measureRun(TextLine.java:414)
at android.text.TextLine.measure(TextLine.java:293)
at android.text.TextLine.metrics(TextLine.java:267)
at android.text.Layout.getLineExtent(Layout.java:998)
at android.text.Layout.getLineStartPos(Layout.java:504)
at android.text.Layout.getHorizontal(Layout.java:861)
at android.text.Layout.getHorizontal(Layout.java:830)
at android.text.Layout.getPrimaryHorizontal(Layout.java:805)
at android.text.Layout.getPrimaryHorizontal(Layout.java:795)
at android.widget.TextView.getFocusedRect(TextView.java:5135)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:120)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:94)
at android.view.FocusFinder.findNextFocus(FocusFinder.java:65)
at android.view.ViewGroup.focusSearch(ViewGroup.java:670)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.ViewGroup.focusSearch(ViewGroup.java:672)
at android.view.View.focusSearch(View.java:6392)
at android.widget.TextView.onCreateInputConnection(TextView.java:5641)
at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:1120)
at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:1070)
at android.widget.TextView.setText(TextView.java:3691)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$5.onTextChanged(NewRefuelActivity.java:162)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$4.onTextChanged(NewRefuelActivity.java:130)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$5.onTextChanged(NewRefuelActivity.java:162)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$4.onTextChanged(NewRefuelActivity.java:130)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3604)
at com.example.pietrzyk.sqlite1.NewRefuelActivity$5.onTextChanged(NewRefuelActivity.java:162)
at android.widget.TextView.sendOnTextChanged(TextView.java:7318)
at android.widget.TextView.setText(TextView.java:3774)
at android.widget.TextView.setText(TextView.java:3629)
There are few things which you have to keep in mind:
EditText which you are using can also contains spaces which may also lead to crash. To avoid that rather than taking editText.length() its better to take editText.getText().toString().trim().length().
Secondly, make sure that in that layout you should constrain the user to enter only numbers for which you have to set attribute android:inputType="number".

Related Links

View clipped on rotation animation on Y axis when perpendicular to screen
using listView and listAdaptor, list not getting displayed correctly
Android WebView won't scroll
Android Web view swiping for Arabic version
Bitmap Shader with alpha channel. (Alpha is drawing black..?)
How we can get BootAnimation.zip Location in android with adb.exe?
Shortcuts for <activity-alias> disappear on Jelly Bean
Viewflow unable to set TextView Data into other layout
How to authenticate multiple accounts in Android dropbox sdk 1.5.1?
What Type of Hash is the Test Device ID of AdMob?
Move Image Taken With Camera Leaves Broken Link Unitl SD Card Remount
startService restart my service if currently running, the Android docs say the opposite
Localization for android EditText [duplicate]
Programatically give space between buttons in table layout
Not receiving an Intent back from Zxing barcode scanner
Android app doesn't work on Android 4

Categories

HOME
search
kentico
checkstyle
spark-streaming
time-complexity
pagination
jmx
mysql-workbench
knitr
nuget
carousel
ado.net
zip
vsm
boto3
discrete-mathematics
augeas
android-securityexception
activecollab
emoji
naivebayes
stellar.js
mongodb-csharp
azure-iot-hub
symfony-2.8
dynamic-sql
imessage
mapstruct
replication
zxing
highstock
definitelytyped
browser-sync
sqlpackage
mockjax
jett
lint
openalpr
unordered-map
resolution
parser-generator
ssrs-2014
robomongo
truezip
pdf-conversion
pybrain
rxtx
node-request
dotnetnuke-7
jboss-tools
concept-insights
toran-proxy
tfs-workitem
r-package
fiware-monitoring
computer-architecture
doctype
viola-jones
cwrsync
mraid
android-broadcast
at-job
uncaught-typeerror
sip-server
contrast
lnk
jquery-slider
memset
renderer
amslidemenu
conditionaltagsupport
ora-06550
breakout
booksleeve
fragmenttransaction
sentestingkit
iboutlet
correlated-subquery
user-tracking
umra
collectionviewsource
dot-emacs
tabpanel
purepdf
visualj#
todos
fdf

Resources

Encrypt Message