ActivityMain.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/cursit_price"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buyingPrice"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/edBuy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:gravity="center"
android:hint="@string/buying_price"
android:inputType="number"
android:textColorHint="#78909C"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/sellPrice"
android:gravity="center"
android:textSize="20sp"/>
<EditText
android:id="@+id/edSell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints=""
android:gravity="center"
android:hint="@string/selling_price"
android:textColorHint="#78909C"
android:textSize="20sp"
android:inputType="number" />
<Button
android:id="@+id/buttonCalculate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/account"
android:textSize="20sp"/>
<TextView
android:id="@+id/tvDisplay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:background="@drawable/profit"/>
</LinearLayout>
MainActivity.java
package com.example.buysalescalculate;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edBuy, edSell;
Button buttonCalculate;
TextView tvDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edBuy = findViewById(R.id.edBuy);
edSell = findViewById(R.id.edSell);
buttonCalculate = findViewById(R.id.buttonCalculate);
tvDisplay = findViewById(R.id.tvDisplay);
buttonCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float byPrice, sellPrice, profit, profitPercent;
String sBuy = edBuy.getText().toString();
byPrice = Float.parseFloat(sBuy);
String sSell = edSell.getText().toString();
sellPrice = Float.parseFloat(sSell);
profit = sellPrice - byPrice;
profitPercent = profit/byPrice*100;
tvDisplay.setText("Profit is: " +profit+ " \n profit Percentage is: " +profitPercent+ "%");
}
});
}
}
Comments
Post a Comment