Simple Edittext Math Android - excel

new SO user here and I'm new to android as well. I need help doing a simple calculation between two edittext boxes - without a button, using a textwatcher, so that after the user is done using the edittext boxes it can add the two together and output the answer in a textview. I'd post my code here, but it just isn't any good. Can anyone provide an example of how to take two edittext boxes, assign a textwatcher to both, then put the output into a textview. Also, please keep in mind that this is eventually going to use multiple edittext boxes while autoupdating each other. This would be similar to creating forumulas in Microsoft excel and having them update immediately. One of the main problems I seem to have is catching the numberformatexception when just one is empty. Thank you for your time.

I implemented a simple app - I hope it helps you.
public class MainActivity extends Activity implements TextWatcher {
TextView tvResult;
EditText tvNumberOne, tvNumberTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvResult = (TextView)findViewById(R.id.tvResult);
tvNumberOne = (EditText)findViewById(R.id.tvNumberOne);
tvNumberTwo = (EditText)findViewById(R.id.tvNumberTwo);
tvNumberOne.addTextChangedListener(this);
tvNumberTwo.addTextChangedListener(this);
}
#Override
public void afterTextChanged(Editable s) {
int numOne = 0, numTwo = 0;
try{
numOne = Integer.valueOf(String.valueOf(tvNumberOne.getText()));
numTwo = Integer.valueOf(String.valueOf(tvNumberTwo.getText()));
}catch(Exception ex){
Toast.makeText(getApplicationContext(), "Parsing error!",Toast.LENGTH_SHORT).show();
}
tvResult.setText(String.valueOf(numOne + numTwo));
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
}

Related

How to listen to EditText changes and change ImageView Visibility at runtime?

Is there a way that I can for example make an ImageView to appear only after an InputEditText is not null? So before the InputEditText has some value, ImageView will not appear (hidden or invisible).
Of course.
in the image view's xml add the attribute:
android:visibility="gone"
or
android:visibility="invisible"
to make it invisible by default
You can see a demonstration of the difference between the two, here: http://tips.androidgig.com/invisible-vs-gone-view-in-android/#:~:text=This%20view%20is%20invisible%2C%20and,what%20is%20the%20actual%20difference.
Create a listener in your activity's onCreate() method, to track changes in your InputEditText, and change the visibility of your imageView, based on the text in your input
assuming your InputEditText view ID is "myInput", and your ImageView view ID is "myImage":
InputEditText myInput = findViewById(R.id.myInput);
myInput.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {}
#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){
ImageView myImage= findViewById(R.id.myImage);
myImage.setVisibility(View.VISIBLE);
}
}
});

TextWatcher in Android not setting values in other edittexts

I have three edittext values for which i am using textwatcher to update data.
I want that if i enter any value in anyone of the edit text then according to that value it should calculate and then update the edittext in which i entered the value automatically and after that set the value in other specif edit text and save the final value in string if need to send to any server.
//My Code
public class Land_details extends AppCompatActivity {
EditText land_acre_edit;
EditText land_kanal_edit;
EditText land_marla_edit;
Button land_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_land_details);
land_acre_edit = findViewById(R.id.land_acre_edit);
land_kanal_edit = findViewById(R.id.land_kanal_edit);
land_marla_edit = findViewById(R.id.land_marla_edit);
land_button = findViewById(R.id.save_land_detail);
land_acre_edit.addTextChangedListener(mytext_watcher);
land_marla_edit.addTextChangedListener(mytext_watcher);
land_kanal_edit.addTextChangedListener(mytext_watcher);
}
private TextWatcher mytext_watcher = 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) {
try {
String check_marla = land_marla_edit.getText().toString();
String check_kanal = land_kanal_edit.getText().toString();
String check_acre = land_acre_edit.getText().toString();
if (check_marla != null) {
if ((Integer.valueOf(check_marla) > 8)) {
check_acre =(String.valueOf(Integer.valueOf(check_kanal) + (Integer.valueOf(land_marla) / 20)));
check_marla=( String.valueOf(Integer.valueOf(land_marla) % 20));
check_acre = String.valueOf(check_acre) + (Integer.valueOf(check_kanal) / 8);
check_kanal = String.valueOf(Integer.valueOf(check_kanal) % 8);
land_marla_edit.setText(check_marla);
land_kanal_edit.setText(check_kanal);
land_acre_edit.setText(check_acre);
}}
}catch (NumberFormatException e){
}
}
#Override
public void afterTextChanged(Editable s) {
}
};
I have tried to set my try catch code in aftertextChanged but at first it was giving me numberformatexception as soon as i enter a text above my contion my app crash but after that i tried using try catch to catch a numberformatexception after that i am able to add number above my condition and even after a long wait nothing happen can any one explain where i am going wrong.

Cicular Reveal Animation in splash screen

How to get a circular reveal animation on start of application
CircularRevealTransition();
public void CircularRevealTransition() {
int cy = view.getHeight()/2;
int cx = view.getWidth()/2;
float finalRadius = (float) Math.hypot(cx,cy);
Animator animator = ViewAnimationUtils.createCircularReveal(findViewById(R.id.cicular_reveal_id),cx,cy,0,finalRadius);
view.setVisibility(View.VISIBLE);
animator.start();
}
i have written this code inside onCreate method, its only working when applied with button click , but not with start of the application
OnCreate method is too early place to start animation, because real drawing starts much later. There is OnPreDrawListener it invokes right before view is drawn.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
#Override
public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
CircularRevealTransition();
return true;
}
});
}

If Statement Not Executed in AutocompleteTextView

I'm new to Android Studio and currently working on my first app. Thanks to numerous examples on here i have managed to catch up on the basics very quickly. I now have a problem with my autocompletetextview, i want to get the text selected then use it in a if statement to determine what is shown on the "results" textview. The if statement works fine but only for the "else" part and i can't figure out where i went wrong.
I have commented out the onItemClickListener because the "testfoodph" method does the same.
Here is the activity code:
public class phTester extends AppCompatActivity {
Intent intent = getIntent();
AutoCompleteTextView text;
MultiAutoCompleteTextView text1;
TextView results;
String foodies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ph_tester);
String[] foods = getResources().getStringArray(R.array.foodlist);
text=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,foods);
text.setAdapter(adapter);
text.setThreshold(1);
results=(TextView)findViewById(R.id.phResults);
foodies = text.getText().toString();
text.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//if (foodies.equalsIgnoreCase("Mango")) {
//results.setText(getString(R.string.phAlk));
}
//else {
//results.setText(getString(R.string.Error));
//}
//}
});
}
public void TestFoodPH(View view) {
if (foodies.equalsIgnoreCase("Mango")) {
results.setText(getString(R.string.phAlk));
}
else {
results.setText(getString(R.string.Error));
}
}

What are my mistakes by using sensors?

I slowly make my first tries with using sensors. And i want to show the actual air pressure (My smartphone defently have a airpressuresensor.)
I look at some code and copy some code, and build the app. Now it doesnt work, and I dont know why. Does sensors need sth. in the manifest or so?
There are no errorcodes, just a applicationcrash...
the evil line seems to be, because if I build a try and catch around the app dont crash, but nothing happends. : sMgr.registerListener((SensorEventListener) this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
Here is the code:
public class MainActivity extends ActionBarActivity {
public Handler handler = new Handler();
public double a;
SensorManager sMgr;
Sensor sensor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sMgr = (SensorManager)getSystemService(SENSOR_SERVICE);
List<Sensor> sensors = sMgr.getSensorList(Sensor.TYPE_PRESSURE);
if(sensors.size() > 0) {
sensor = sensors.get(0);
sMgr.registerListener((SensorEventListener) this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void onSensorChanged(SensorEvent event) {
float presure = event.values[0];
Log.d("PressureSens", ""+presure);
TextView textView = (TextView)findViewById(R.id.text);
textView.setText(""+ presure);
}

Resources