Lucas Pereira Lucas Pereira - 4 months ago 7x
Java Question

How to show text instead numbers as a result of a math formula in Android App?

How are you? I need help with my first Android App.

I want to calculate the result of the gasoline price divided by the etanol price. I've already done that, with the result being displayed in the app.

But now I want to make it better. Instead of a number, I want the text field to display "Gasoline", if the result of gasoline / etanol is equal or greater than 0.7, and "Etanol", if the result is lower than 0.7.

How can I do that? I'm adding the code I already have. Thanks!


import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Inicio extends AppCompatActivity {

TextView totalTextView;
EditText gasolineTxt;
EditText etanolTxt;

protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(;

totalTextView = (TextView) findViewById(;
gasolineTxt = (EditText) findViewById(;
etanolTxt = (EditText) findViewById(;

Button calcBtn = (Button) findViewById(;
calcBtn.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
float gasoline = Float.parseFloat(gasolineTxt.getText().toString());
float etanol = Float.parseFloat(etanolTxt.getText().toString());
float total = etanol / gasoline;

public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(, menu);
return true;

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 == {
return true;

return super.onOptionsItemSelected(item);


Should just be a simple if statement, yeah?

Also, your question said gasoline / etanol, so I used that instead of what you had in the code.

float total = gasoline / etanol;
if (total < 0.7)

Or you can one-line that

totalTextView.setText( (gasoline / etanol) < 0.7 ? "Ethanol" : "Gasoline");