added ViewBinding & reduced no.of lines

This commit is contained in:
adpth 2020-10-15 19:51:49 +05:30
parent e8af46d625
commit 71a929a27e
2 changed files with 33 additions and 52 deletions

View File

@ -21,6 +21,10 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
} }
} }
buildFeatures {
viewBinding = true
}
} }
dependencies { dependencies {

View File

@ -14,55 +14,38 @@ import android.widget.RelativeLayout;
import android.widget.SeekBar; import android.widget.SeekBar;
import android.widget.TextView; import android.widget.TextView;
import com.adpth.bmicalculator.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
float height,weight; float height,weight;
TextView height_txt,age;
int count_weight = 50,count_age = 19; int count_weight = 50,count_age = 19;
RelativeLayout weight_plus, weight_minus, age_plus, age_minus;
boolean male_clk = true, female_clk = true, check1 = true, check2 = true; boolean male_clk = true, female_clk = true, check1 = true, check2 = true;
private ActivityMainBinding binding;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
height_txt = findViewById(R.id.height_txt); binding.cardViewMale.setOnClickListener(new View.OnClickListener() {
final TextView female_text = findViewById(R.id.female);
final TextView male_text = findViewById(R.id.male);
CardView card_female = findViewById(R.id.cardView_female);
CardView card_male = findViewById(R.id.cardView_male);
age_minus = findViewById(R.id.age_minus);
age_plus = findViewById(R.id.age_plus);
weight_minus = findViewById(R.id.weight_minus);
weight_plus = findViewById(R.id.weight_plus);
card_male.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (check1) { if (check1) {
if (male_clk) { if (male_clk) {
male_text.setTextColor(Color.parseColor("#FFFFFF")); binding.male.setTextColor(Color.parseColor("#FFFFFF"));
//male.setImageResource(R.drawable.male_white); binding.male.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.male_white,0,0);
male_text.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.male_white,0,0);
male_clk = false; male_clk = false;
check2 = false; check2 = false;
} else { } else {
male_text.setTextColor(Color.parseColor("#8D8E99")); binding.male.setTextColor(Color.parseColor("#8D8E99"));
//male.setImageResource(R.drawable.male); binding.male.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.male,0,0);
male_text.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.male,0,0);
male_clk = true; male_clk = true;
check2 = true; check2 = true;
} }
@ -70,20 +53,20 @@ public class MainActivity extends AppCompatActivity {
} }
}); });
card_female.setOnClickListener(new View.OnClickListener() { binding.cardViewFemale.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (check2) { if (check2) {
if (female_clk) { if (female_clk) {
female_text.setTextColor(Color.parseColor("#FFFFFF")); binding.female.setTextColor(Color.parseColor("#FFFFFF"));
female_text.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.female_white,0,0); binding.female.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.female_white,0,0);
female_clk = false; female_clk = false;
check1 = false; check1 = false;
} }
else { else {
female_text.setTextColor(Color.parseColor("#8D8E99")); binding.female.setTextColor(Color.parseColor("#8D8E99"));
female_text.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.female,0,0); binding.female.setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.female,0,0);
female_clk = true; female_clk = true;
check1 = true; check1 = true;
} }
@ -97,8 +80,7 @@ public class MainActivity extends AppCompatActivity {
CheckAge(); CheckAge();
Button calculate = findViewById(R.id.calculate); binding.calculate.setOnClickListener(new View.OnClickListener() {
calculate.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
CalculateBMI(); CalculateBMI();
@ -110,57 +92,52 @@ public class MainActivity extends AppCompatActivity {
private void CheckAge() { private void CheckAge() {
age = findViewById(R.id.age); binding.agePlus.setOnClickListener(new View.OnClickListener() {
age_plus.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
count_age++; count_age++;
age.setText(String.valueOf(count_age)); binding.age.setText(String.valueOf(count_age));
} }
}); });
age_minus.setOnClickListener(new View.OnClickListener() { binding.ageMinus.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
count_age--; count_age--;
age.setText(String.valueOf(count_age)); binding.age.setText(String.valueOf(count_age));
} }
}); });
} }
private void CheckWeight() { private void CheckWeight() {
final TextView weight_txt = findViewById(R.id.weight); binding.weightPlus.setOnClickListener(new View.OnClickListener() {
weight_plus.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
count_weight++; count_weight++;
weight_txt.setText(String.valueOf(count_weight)); binding.weight.setText(String.valueOf(count_weight));
} }
}); });
weight_minus.setOnClickListener(new View.OnClickListener() { binding.weightMinus.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
count_weight--; count_weight--;
weight_txt.setText(String.valueOf(count_weight)); binding.weight.setText(String.valueOf(count_weight));
} }
}); });
weight = Float.parseFloat(weight_txt.getText().toString()); weight = Float.parseFloat(binding.weight.getText().toString());
} }
private void CheckSeekbarStatus() { private void CheckSeekbarStatus() {
SeekBar Seekbar = findViewById(R.id.Seekbar); binding.Seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override @Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
String ht = progress + getResources().getString(R.string.cm); String ht = progress + getResources().getString(R.string.cm);
height_txt.setText(ht); binding.heightTxt.setText(ht);
height = (float)(progress)/100; height = (float)(progress)/100;
} }
@ -181,7 +158,7 @@ public class MainActivity extends AppCompatActivity {
float BMI = weight / (height * height); float BMI = weight / (height * height);
Intent intent = new Intent(MainActivity.this,ResultActivity.class); Intent intent = new Intent(MainActivity.this,ResultActivity.class);
intent.putExtra("BMI",BMI); intent.putExtra("BMI",BMI);
intent.putExtra("age",age.getText().toString()); intent.putExtra("age",binding.age.getText().toString());
startActivity(intent); startActivity(intent);
} }
} }