Your cart is currently empty!
Kotlin + Java Android Code Stock Profit Loss Calculate [Full Source Code]
This is the complete source code for a stock profit/loss calculator app on Android, written in Kotlin and Java. The app allows users to enter stock data and calculates the profit/loss based on the input, with the text color turning red for negative results and green for positive ones. It’s ideal for beginner to intermediate-level Android developers who want to learn Kotlin and Java while building practical skills.
Here is an example code in Kotlin that creates an Android app with input fields for the stock data and calculates the profit/loss based on those inputs:
Kotlin Code
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val buyPrice = findViewById<EditText>(R.id.buy_price) val sellPrice = findViewById<EditText>(R.id.sell_price) val shares = findViewById<EditText>(R.id.shares) val commission = findViewById<EditText>(R.id.commission) val calculateButton = findViewById<Button>(R.id.calculate) val results = findViewById<TextView>(R.id.results) calculateButton.setOnClickListener { val buyPriceValue = buyPrice.text.toString().toFloat() val sellPriceValue = sellPrice.text.toString().toFloat() val sharesValue = shares.text.toString().toInt() val commissionValue = commission.text.toString().toFloat() val buyCost = buyPriceValue * sharesValue val sellCost = sellPriceValue * sharesValue val totalCost = buyCost + sellCost + commissionValue val profitLoss = sellCost - buyCost - commissionValue val percentProfitLoss = (profitLoss / buyCost) * 100 if (profitLoss < 0) { results.setTextColor(ContextCompat.getColor(this, R.color.red)) } else { results.setTextColor(ContextCompat.getColor(this, R.color.green)) } results.text = "Buy Cost: $${String.format("%.2f", buyCost)}\n" + "Sell Cost: $${String.format("%.2f", sellCost)}\n" + "Total Cost: $${String.format("%.2f", totalCost)}\n" + "Profit/Loss: $${String.format("%.2f", profitLoss)}\n" + "Percent Profit/Loss: ${String.format("%.2f", percentProfitLoss)}%" results.visibility = View.VISIBLE } } }
JAVA Code
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText buyPrice = findViewById(R.id.buy_price); EditText sellPrice = findViewById(R.id.sell_price); EditText shares = findViewById(R.id.shares); EditText commission = findViewById(R.id.commission); Button calculateButton = findViewById(R.id.calculate); TextView results = findViewById(R.id.results); calculateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { float buyPriceValue = Float.parseFloat(buyPrice.getText().toString()); float sellPriceValue = Float.parseFloat(sellPrice.getText().toString()); int sharesValue = Integer.parseInt(shares.getText().toString()); float commissionValue = Float.parseFloat(commission.getText().toString()); float buyCost = buyPriceValue * sharesValue; float sellCost = sellPriceValue * sharesValue; float totalCost = buyCost + sellCost + commissionValue; float profitLoss = sellCost - buyCost - commissionValue; float percentProfitLoss = (profitLoss / buyCost) * 100; if (profitLoss < 0) { results.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.red)); } else { results.setTextColor(ContextCompat.getColor(MainActivity.this, R.color.green)); } results.setText("Buy Cost: $" + String.format("%.2f", buyCost) + "\n" + "Sell Cost: $" + String.format("%.2f", sellCost) + "\n" + "Total Cost: $" + String.format("%.2f", totalCost) + "\n" + "Profit/Loss: $" + String.format("%.2f", profitLoss) + "\n" + "Percent Profit/Loss: " + String.format("%.2f", percentProfitLoss) + "%"); results.setVisibility(View.VISIBLE); } }); } }
First, in the layout file (activity_main.xml), you can create the input fields and a button for the user to calculate the profit/loss:
XML Code
<EditText android:id="@+id/buy_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="Buy Price" /> <EditText android:id="@+id/sell_price" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="Sell Price" /> <EditText android:id="@+id/shares" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="number" android:hint="Shares" /> <EditText android:id="@+id/commission" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="numberDecimal" android:hint="Commission" /> <Button android:id="@+id/calculate" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Calculate Profit/Loss" /> <TextView android:id="@+id/results" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone" />
In this code, the results
TextView’s text color is set to red if the profit/loss value is negative and green if it’s positive. The ContextCompat.getColor
method is used to get the color value from the app’s resources (defined in the colors.xml file).
You can define the red and green colors in the colors.xml file as follows:
<color name="red">#FF0000</color> <color name="green">#00FF00</color>
With these changes, the results
TextView font color will change to red or green depending on the profit/loss value.
Comments
Grabber Pro
Original price was: $59.$39Current price is: $39.Insertcart Custom WooCommerce Checkbox Ultimate
Original price was: $39.$19Current price is: $19.Android App for Your Website
Original price was: $49.$35Current price is: $35.Abnomize Pro
Original price was: $30.$24Current price is: $24.Medical Portfolio Pro
Original price was: $31.$24Current price is: $24.
Latest Posts
- How to Handle Sudden Traffic Spike in Website – Do Node Balancer Really Help
- 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
Leave a Reply