im new to android, but im developing an application that has conditions in the background.i want if some one inputs the AVB number in the first EditText, the second one should be automatically filled from the ranges in the if statement and then multiplies and puts the answer in the tfeed TextView. please help me out as soon as possible. Here is my fish_feed.java class
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Fish_Feed extends AppCompatActivity { EditText number1; EditText number2; TextView total_feed; Button calc; double num1,num2,product ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fish_feed); Toolbar toolbar = findViewById(R.id.feed_fry); setSupportActionBar(toolbar); getSupportActionBar().setTitle("Calculate Feed Per Fish in a day"); getSupportActionBar().setDisplayHomeAsUpEnabled(true); number1 = (EditText)findViewById(R.id.abw); number2 = (EditText)findViewById(R.id.fish); total_feed = (TextView) findViewById(R.id.tfeed); calc = (Button) findViewById(R.id.feed); calc.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { num1 = Double.parseDouble(number1.getText().toString()); num2 = Double.parseDouble(number2.getText().toString()); product = num1*(num2/100) ; if (num1 < 2){ number2.setText((int) 16.5) ; } else if(num1 >=2 && num1<=10){ number2.setText((int) 7) ; } else if(num1 >=10 && num1<=100){ number2.setText((int) 3) ; } else if(num1 >=100 && num1<=500){ number2.setText((int) 1.5) ; } else if(num1 >500){ number2.setText((int) 1) ; } total_feed.setText("Number of fish to be stocked is " + product); } }); } } **And the activity_fish_feed** <?xml version="1.0" encoding="utf-8"?> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout xmlns: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" android:orientation="vertical" tools:context=".Fish_Feed"> <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/feed_fry" app:titleTextColor="#fff" android:background="#FF018786" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Enter Average Body Weight of Your Fish" android:textSize="20sp" android:textColor="#000" android:layout_marginTop="15dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp"/> <EditText android:id="@+id/abw" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:hint="Enter your ABW" /> <EditText android:id="@+id/fish" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#000" android:layout_marginTop="15dp" android:layout_marginStart="16dp" android:layout_marginEnd="16dp"/> <Button android:id="@+id/feed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:backgroundTint="#FF018786" android:text="CALCULATE TOTAL FEED" android:layout_marginLeft="30dp" android:textColor="@color/white" app:layout_constraintEnd_toEndOf="@+id/yield" app:layout_constraintTop_toBottomOf="@+id/yield" /> <TextView android:id="@+id/tfeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="20sp" android:textColor="#000" android:layout_marginStart="16dp" android:layout_marginEnd="16dp"/> </LinearLayout> </ScrollView>
Advertisement
Answer
Try this once –
XML File-
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/feed_fry" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#FF018786" app:titleTextColor="#fff" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="15dp" android:layout_marginEnd="16dp" android:text="Enter Average Body Weight of Your Fish" android:textColor="#000" android:textSize="20sp" /> <EditText android:id="@+id/abw" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:hint="Enter your ABW" android:inputType="number" /> <EditText android:id="@+id/fish" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginTop="15dp" android:layout_marginEnd="16dp" android:enabled="false" android:textColor="#000" android:textSize="20sp" /> <Button android:id="@+id/feed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="16dp" android:backgroundTint="#FF018786" android:text="CALCULATE TOTAL FEED" android:textColor="@color/white" app:layout_constraintEnd_toEndOf="@+id/yield" app:layout_constraintTop_toBottomOf="@+id/yield" /> <TextView android:id="@+id/tfeed" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:layout_marginEnd="16dp" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000" android:textSize="20sp" /> </LinearLayout> </ScrollView>
Activity File
import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; public class MainActivity2 extends AppCompatActivity { EditText number1; EditText number2; TextView total_feed; Button calc; double product; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Toolbar toolbar = findViewById(R.id.feed_fry); toolbar.setTitle("Calculate Feed Per Fish in a day"); number1 = findViewById(R.id.abw); number2 = findViewById(R.id.fish); total_feed = findViewById(R.id.tfeed); number1.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void onTextChanged(CharSequence s, int start, int before, int count) { if (s.length() == 0) { number2.setText(""); total_feed.setText(""); } else { double number; int value = Integer.parseInt(String.valueOf(s)); if (value < 2) { number = 16.5; } else if (value >= 2 && value <= 10) { number = 7; } else if (value >= 10 && value <= 100) { number = 3; } else if (value >= 100 && value <= 500) { number = 1.5; } else if (value > 500) { number = 1; } else { number = 0; } number2.setText(String.valueOf(number)); } } }); calc = findViewById(R.id.feed); calc.setOnClickListener(v -> { String num1 = number1.getText().toString().trim(); String num2 = number2.getText().toString().trim(); if (num1.isEmpty() && num2.isEmpty()) { Toast.makeText(getApplicationContext(), "Add Num", Toast.LENGTH_LONG).show(); } else { product = Double.parseDouble(num1) * (Double.parseDouble(num2) / 100); total_feed.setText("Number of fish to be stocked is " + product); } }); } }