Matheus Barreto Matheus Barreto - 1 month ago 6
Android Question

How to get a int from an Edit View and display in a Text View? I'm having a hard time to do it

These are my xml. I have a EditText and a Text View

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/resultado"/>

<EditText
android:id="@+id/a_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Insira a"
android:inputType="number"
android:padding="36dp" />


My java file. I already converted EditText's value to int, I guess. What am I doing wrong?

public class DiagnosticoActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);

// Get a value from a editText
EditText editTextA = (EditText) findViewById(R.id.a_edit_text);
TextView resul = (TextView) findViewById(R.id.resultado);
String variableA = editTextA.getText().toString(); //this will get a string

int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
resul.setText(""+a); //Added "" here
}
catch (NumberFormatException e) {
// handle incorrect text entry here
} // a will be 0 if exception occurred

}
}


How can I display entered number with TextView?

I Tried this but it ain't worked...What I should do?

public class DiagnosticoActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);

}

public void calcular(View view){
// Get a value from a editText
EditText editTextA = (EditText) findViewById(R.id.a_edit_text);
TextView resul = (TextView) findViewById(R.id.resultado);
String variableA = editTextA.getText().toString(); //this will get a string
int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
resul.setText(a);
}
catch (NumberFormatException e) {
// handle incorrect text entry here
} // a will be 0 if exception occurred

}
}


<Button
android:id="@+id/button_calcular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calcular"
android:layout_marginRight="8dp"
android:onClick="calcular"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/resultado"/>

<EditText
android:id="@+id/a_edit_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Insira a"
android:inputType="number"
android:padding="36dp" />


 

New java code

public class DiagnosticoActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diagnostico);

}

public void calcular(View view){
// Get a value from a editText
EditText editTextA = (EditText) findViewById(R.id.a_edit_text);
TextView resul = (TextView) findViewById(R.id.resultado);
String variableA = editTextA.getText().toString(); //this will get a string

int a = 0;
try {
a = Integer.parseInt(variableA);// will only work on numeric entries
Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
resul.setText(""+a); //Added "" here
}
catch (NumberFormatException e) {
// handle incorrect text entry here
} // a will be 0 if exception occurred
}
}

Answer
public class DiagnosticoActivity extends AppCompatActivity {
EditText editTextA;
TextView resul;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_diagnostico);
    editTextA = (EditText) findViewById(R.id.a_edit_text);
    resul = (TextView) findViewById(R.id.resultado);
}

public void calcular(View view){
    // Get a value from a editText

    String variableA = editTextA.getText().toString(); //this will get a string
    resul.setText(variableA);

    int a = 0;
    try {
        a = Integer.parseInt(variableA);// will only work on numeric entries
        Log.v("DiagnosticoActivity", "Number a: " + a); // this log doesn't work...why?
    }
    catch (NumberFormatException e) {
       a = 0; // handle incorrect text entry here
    } // a will be 0 if exception occurred
}
}