The code below will calculate your BMI and display the result in the screen.
see the screen shots below
// FirstFragment.ktpackagecom.example.cs4405_02importandroid.os.Bundleimportandroidx.fragment.app.Fragmentimportandroid.view.LayoutInflaterimportandroid.view.Viewimportandroid.view.ViewGroupimportandroidx.navigation.fragment.findNavControllerimportcom.example.cs4405_02.databinding.FragmentFirstBindingimportandroid.graphics.ColorenumclassBMI_DATA(valrange:ClosedFloatingPointRange<Double>,valmessage:String,valcolor:Int){UNDERWEIGHT(0.0..18.5,"You are underweight",Color.YELLOW),NORMAL(18.5..24.9,"Your weight is normal",Color.GREEN),OVERWEIGHT(25.0..Double.MAX_VALUE,"You are overweight",Color.RED);companionobject{funget(bmi:Double):BMI_DATA{returnvalues().first{it.range.contains(bmi)}}}}funcalculateBMI(weight:Double,height:Double,gender:String):Double{returnweight/((height.toDouble()/100)*(height/100))// height is in cm, so divided by 100 to convert it to meters}/** * A simple [Fragment] subclass as the default destination in the navigation. */classFirstFragment:Fragment(){privatevar_binding:FragmentFirstBinding? =null// This property is only valid between onCreateView and// onDestroyView.privatevalbindingget()=_binding!!overridefunonCreateView(inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?):View? {_binding=FragmentFirstBinding.inflate(inflater,container,false)returnbinding.root}overridefunonViewCreated(view:View,savedInstanceState:Bundle?){super.onViewCreated(view,savedInstanceState)binding.calculate.setOnClickListener{valweight=binding.weight.text.toString().toDouble()valheight=binding.height.text.toString().toDouble()valgender=if(binding.male.isChecked)"Male"else"Female"valbmiScore=calculateBMI(weight,height,gender)valbmi=BMI_DATA.get(bmiScore)binding.result.text="Your BMI: $bmiScore. ${bmi.message}"binding.result.setTextColor(bmi.color)binding.result.visibility=View.VISIBLE}binding.buttonFirst.setOnClickListener{findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)}}overridefunonDestroyView(){super.onDestroyView()_binding=null}}
<!-- fragment_first.xml --><?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".FirstFragment"><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="112dp"android:orientation="vertical"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.554"app:layout_constraintStart_toStartOf="parent"><TextViewandroid:id="@+id/result"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@color/black"android:textSize="30sp"android:visibility="gone"/><EditTextandroid:id="@+id/weight"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Enter weight in kg"/><EditTextandroid:id="@+id/height"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="Enter height in cm"/><RadioGroupandroid:id="@+id/gender"android:layout_width="358dp"android:layout_height="wrap_content"><RadioButtonandroid:id="@+id/male"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Male"/><RadioButtonandroid:id="@+id/female"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Female"/></RadioGroup><Buttonandroid:id="@+id/calculate"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Calculate BMI"/><Buttonandroid:id="@+id/button_first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginBottom="112dp"android:text="Open Calculator"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.1"app:layout_constraintStart_toStartOf="parent"/></LinearLayout></androidx.constraintlayout.widget.ConstraintLayout>
The calculator will perform basic arithmetic operations.
It is another fragment within the same activity.
You can go between the two fragments using the buttons.
screenshots below
packagecom.example.cs4405_02importandroid.os.Bundleimportandroidx.fragment.app.Fragmentimportandroid.view.LayoutInflaterimportandroid.view.Viewimportandroid.view.ViewGroupimportandroidx.navigation.fragment.findNavControllerimportcom.example.cs4405_02.databinding.FragmentSecondBindingenumclassOperations{ADD,SUBTRACT,MULTIPLY,DIVIDE}classCalculator(privatevaloperand1:Double,privatevaloperand2:Double){funcalculate(operation:Operations):Double{returnwhen(operation){Operations.ADD->operand1+operand2Operations.SUBTRACT->operand1-operand2Operations.MULTIPLY->operand1*operand2Operations.DIVIDE->if(operand2!=0.0)operand1/operand2elseDouble.NaN}}}/** * A simple [Fragment] subclass as the second destination in the navigation. */classSecondFragment:Fragment(){privatevar_binding:FragmentSecondBinding? =null// This property is only valid between onCreateView and// onDestroyView.privatevalbindingget()=_binding!!overridefunonCreateView(inflater:LayoutInflater,container:ViewGroup?,savedInstanceState:Bundle?):View? {_binding=FragmentSecondBinding.inflate(inflater,container,false)returnbinding.root}overridefunonViewCreated(view:View,savedInstanceState:Bundle?){super.onViewCreated(view,savedInstanceState)binding.buttonSecond.setOnClickListener{findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)}binding.calculate.setOnClickListener{valop1=binding.operand1.text.toString().toDouble()valop2=binding.operand2.text.toString().toDouble()val_op=binding.operation.selectedItem.toString()valoperation=when(_op){"ADD"->Operations.ADD"SUBTRACT"->Operations.SUBTRACT"MULTIPLY"->Operations.MULTIPLY"DIVIDE"->Operations.DIVIDEelse->throwIllegalArgumentException("Unknown operation.")}valcalculator=Calculator(op1,op2)valresult=calculator.calculate(operation)binding.result.text=result.toString()}}overridefunonDestroyView(){super.onDestroyView()_binding=null}}
<!-- fragment_second.xml --><?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".SecondFragment"><TextViewandroid:id="@+id/textview_second"android:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintBottom_toTopOf="@id/button_second"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"/><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="16dp"><EditTextandroid:id="@+id/operand1"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="numberDecimal"android:hint="Enter first number"/><EditTextandroid:id="@+id/operand2"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="numberDecimal"android:hint="Enter second number"android:layout_marginTop="10dp"/><Spinnerandroid:id="@+id/operation"android:layout_width="match_parent"android:layout_height="wrap_content"android:entries="@array/operations"android:layout_marginTop="10dp"/><Buttonandroid:id="@+id/calculate"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Calculate"android:layout_marginTop="10dp"/><TextViewandroid:id="@+id/result"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="30sp"android:layout_marginTop="10dp"/></LinearLayout><Buttonandroid:id="@+id/button_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="To BMI"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@id/textview_second"/></androidx.constraintlayout.widget.ConstraintLayout>