Your cart is currently empty!
Android Kotlin & Java Code for Stock Average Calculator [Full Source Code]
Looking for ready-to-use Android code to build a stock average calculator? Check out our collection of Kotlin and Java code snippets that can help you create a user-friendly app to calculate stock average prices. With these code examples, you can easily retrieve user input, perform calculations, and display results using text views, edit text views, buttons, and other UI components. Use our code snippets as a starting point for your project, or modify them to fit your specific needs. Whether you’re a beginner or an experienced Android developer, these code examples can save you time and help you build a high-quality stock average calculator app.
Here is simple code to calculate stocks average using android Kotlin and Java code. You can check demo website for PHP code and Source code for PHP stock average calculator
Kotlin Code
package com.insertcart.adavancecal.ui import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import androidx.navigation.ui.AppBarConfiguration import com.insertcart.adavancecal.R import com.insertcart.adavancecal.databinding.ActivityStockavgBinding class StockavgActivity : AppCompatActivity() { private lateinit var quantityArray: Array<EditText> private lateinit var priceArray: Array<EditText> private lateinit var resultText: TextView private lateinit var totalcostText: TextView private lateinit var totalshareText: TextView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_stockavg) val quantity1 = findViewById<EditText>(R.id.quantity1) val quantity2 = findViewById<EditText>(R.id.quantity2) val quantity3 = findViewById<EditText>(R.id.quantity3) val quantity4 = findViewById<EditText>(R.id.quantity4) val quantity5 = findViewById<EditText>(R.id.quantity5) val quantity6 = findViewById<EditText>(R.id.quantity6) val quantity7 = findViewById<EditText>(R.id.quantity7) val quantity8 = findViewById<EditText>(R.id.quantity8) val quantity9 = findViewById<EditText>(R.id.quantity9) val quantity10 = findViewById<EditText>(R.id.quantity10) val price1 = findViewById<EditText>(R.id.price1) val price2 = findViewById<EditText>(R.id.price2) val price3 = findViewById<EditText>(R.id.price3) val price4 = findViewById<EditText>(R.id.price4) val price5 = findViewById<EditText>(R.id.price5) val price6 = findViewById<EditText>(R.id.price6) val price7 = findViewById<EditText>(R.id.price7) val price8 = findViewById<EditText>(R.id.price8) val price9 = findViewById<EditText>(R.id.price9) val price10 = findViewById<EditText>(R.id.price10) quantityArray = arrayOf(quantity1, quantity2, quantity3, quantity4, quantity5, quantity6, quantity7, quantity8, quantity9, quantity10) priceArray = arrayOf(price1, price2, price3, price4, price5, price6, price7, price8, price9, price10) resultText = findViewById(R.id.resultText) totalcostText = findViewById(R.id.totalcostText) totalshareText = findViewById(R.id.totalshareText) val calculateButton: Button = findViewById(R.id.calculateButton) calculateButton.setOnClickListener { val quantityArray = arrayOf(quantity1, quantity2, quantity3, quantity4, quantity5, quantity6, quantity7, quantity8, quantity9, quantity10) val priceArray = arrayOf(price1, price2, price3, price4, price5, price6, price7, price8, price9, price10) var totalValue = 0.0 var totalQuantity = 0 for (i in 0 until quantityArray.size) { val quantity = quantityArray[i].text.toString().toIntOrNull() ?: 0 val price = priceArray[i].text.toString().toDoubleOrNull() ?: 0.0 totalValue += quantity * price totalQuantity += quantity } val averagePrice = if (totalQuantity == 0) { 0.0 } else { totalValue / totalQuantity } resultText.text = getString(R.string.average_price, averagePrice) totalcostText.text = totalValue.toString() totalshareText.text = totalQuantity.toString() } } }
Java Code
package com.insertcart.adavancecal.ui; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.insertcart.adavancecal.R; public class StockavgActivity extends AppCompatActivity { private EditText[] quantityArray; private EditText[] priceArray; private TextView resultText; private TextView totalcostText; private TextView totalshareText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_stockavg); EditText quantity1 = findViewById(R.id.quantity1); EditText quantity2 = findViewById(R.id.quantity2); EditText quantity3 = findViewById(R.id.quantity3); EditText quantity4 = findViewById(R.id.quantity4); EditText quantity5 = findViewById(R.id.quantity5); EditText quantity6 = findViewById(R.id.quantity6); EditText quantity7 = findViewById(R.id.quantity7); EditText quantity8 = findViewById(R.id.quantity8); EditText quantity9 = findViewById(R.id.quantity9); EditText quantity10 = findViewById(R.id.quantity10); EditText price1 = findViewById(R.id.price1); EditText price2 = findViewById(R.id.price2); EditText price3 = findViewById(R.id.price3); EditText price4 = findViewById(R.id.price4); EditText price5 = findViewById(R.id.price5); EditText price6 = findViewById(R.id.price6); EditText price7 = findViewById(R.id.price7); EditText price8 = findViewById(R.id.price8); EditText price9 = findViewById(R.id.price9); EditText price10 = findViewById(R.id.price10); quantityArray = new EditText[]{quantity1, quantity2, quantity3, quantity4, quantity5, quantity6, quantity7, quantity8, quantity9, quantity10}; priceArray = new EditText[]{price1, price2, price3, price4, price5, price6, price7, price8, price9, price10}; resultText = findViewById(R.id.resultText); totalcostText = findViewById(R.id.totalcostText); totalshareText = findViewById(R.id.totalshareText); Button calculateButton = findViewById(R.id.calculateButton); calculateButton.setOnClickListener(view -> { EditText[] quantityArray = new EditText[]{quantity1, quantity2, quantity3, quantity4, quantity5, quantity6, quantity7, quantity8, quantity9, quantity10}; EditText[] priceArray = new EditText[]{price1, price2, price3, price4, price5, price6, price7, price8, price9, price10}; double totalValue = 0.0; int totalQuantity = 0; for (int i = 0; i < quantityArray.length; i++) { int quantity = Integer.parseInt(quantityArray[i].getText().toString()); double price = Double.parseDouble(priceArray[i].getText().toString()); totalValue += quantity * price; totalQuantity += quantity; } double averagePrice; if (totalQuantity == 0) { averagePrice = 0.0; } else { averagePrice = totalValue / totalQuantity; } resultText.setText(getString(R.string.average_price, averagePrice)); totalcostText.setText(String.valueOf(totalValue)); totalshareText.setText(String.valueOf(totalQuantity)); } } }
XML File
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context=".ui.StockavgActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/enter_the_quantity" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 1:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 2:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 3:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price3" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 4:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price4" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 5:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price5" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 6:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price6" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 7:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price7" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 8:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price8" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 9:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price9" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stock 10:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/quantity10" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Quantity" /> <EditText android:id="@+id/price10" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Price" /> </LinearLayout> </LinearLayout> </ScrollView> <Button android:id="@+id/calculateButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Calculate Average Price" /> <TextView android:id="@+id/averagePriceText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start" android:textAppearance="?android:textAppearanceLarge" /> <TextView android:id="@+id/resultText" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textAppearance="?android:textAppearanceLarge" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/totalcostTxt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/total_price_of_stocks" android:textAppearance="?android:textAppearanceLarge" /> <TextView android:id="@+id/totalcostText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceLarge" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/totalshareTxt" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/total_numbers_of_stocks" android:textAppearance="?android:textAppearanceLarge" /> <TextView android:id="@+id/totalshareText" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textAppearance="?android:textAppearanceLarge" /> </LinearLayout> </LinearLayout> </RelativeLayout>
Strings
Download full code and View on github here
Comments
Grabber Pro
Original price was: $ 59.00.$ 39.00Current price is: $ 39.00.Insertcart Custom WooCommerce Checkbox Ultimate
Original price was: $ 39.00.$ 19.00Current price is: $ 19.00.Android App for Your Website
Original price was: $ 49.00.$ 35.00Current price is: $ 35.00.Abnomize Pro
Original price was: $ 30.00.$ 24.00Current price is: $ 24.00.Medical Portfolio Pro
Original price was: $ 31.00.$ 24.00Current price is: $ 24.00.
Latest Posts
- How to Use AWS SES Email from Localhost or Website: Complete Configuration in PHP
- How to Upload Images and PDFs in Android Apps Using Retrofit
- [Fix] File Permission Issue in Apache Website WordPress Not Writable to 775
- Best PHP ini Settings for WordPress & WooCommerce: Official Recommendations
- Script to Get Users System or Browser Data When Link Open Grab Using PHP & JavaScript
Leave a Reply