You are on page 1of 5

TP 2 Android

Réalisé par :
Nihel BEN CHEIKH MOKHTAR
Emna TURKI

Classe : 3 ING TA C

1
Partie design :

2
3
Partie Code :
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

TextView age = (EditText)findViewById(R.id.txtAge);


TextView taille = (EditText)findViewById(R.id.txtTaille);
TextView poids = (EditText)findViewById(R.id.txtPoids);
TextView resultat = findViewById(R.id.resultat);
TextView message = findViewById(R.id.txtMsg);

RadioGroup grpRadio = findViewById(R.id.GpRadioBtn);


RadioButton rbtH = findViewById(R.id.rbtHomme);
RadioButton rbtF = findViewById(R.id.rbtFemme);
grpRadio.check(rbtH.getId());

Button calculer = findViewById(R.id.button);


ImageView iv = (ImageView)findViewById(R.id.imageView);

calculer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

int ageI = ParseInteger(age.getText().toString());


int tailleD = ParseInteger(taille.getText().toString());
double poidsD = ParseDouble(poids.getText().toString());

if (ageI == 0 || tailleD == 0 || poidsD == 0){


message.setText("Veuillez saisir tous les champs !");
}else{
String poidId = "";
double v_poidsIdeal = 0;
if (rbtH.isChecked()) {
v_poidsIdeal = (tailleD - 100 - ((tailleD - 150) / 4));
} else if (rbtF.isChecked()) {
v_poidsIdeal = (tailleD - 100 - ((tailleD - 150) / 2));
}
poidId = String.valueOf(v_poidsIdeal);
resultat.setText(poidId.toString());

if
(Double.valueOf(resultat.getText().toString()).compareTo(poidsD) != 0) {
iv.setImageResource(R.drawable.sad2);
} else {

4
iv.setImageResource(R.drawable.good2);
}
}

}
});
}
double ParseDouble(String strNumber) {
if (strNumber != null && strNumber.length() > 0) {
try {
return Double.parseDouble(strNumber);
} catch(Exception e) {
return -1;
}
}
else return 0;
}
int ParseInteger(String strNumber) {
if (strNumber != null && strNumber.length() > 0) {
try {
return Integer.parseInt(strNumber);
} catch(Exception e) {
return -1;
}
}
else return 0;
}
}

You might also like