so this is my first application on Android Studio. I want the user to input 4 values: the first assignment mark, a second and a third with then the last value being the exam mark.
There should be three calculations, one determining the yearmark which is calculated with this equation
Year Mark = Assigment1Mark * .15 Assigment2Mark .35 Assigment3Mark .50**
the second calculation should be for determining the final mark
Final Mark = yearMark .49 examMark .5**
My Main Activity.java file
package com.example.mycalc;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
//onCreate Method
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initiating and referencing for Calculate Button
Button button = (Button)findViewById(R.id.CalcButton);
//onClickListener for the Calculate Button
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//TODO Auto generated method stub
//Variables declared
TextView ass1Mark = findViewById(R.id.assignment1);
TextView ass2Mark = findViewById(R.id.assignment2);
TextView ass3Mark = findViewById(R.id.assignment3);
TextView examMark = findViewById(R.id.assignment4);
TextView yearMark = findViewById(R.id.yearMark);
TextView finalMark = findViewById(R.id.finalMark);
//Calculations to execute when button is pressed
yearMark.setText(ass1Mark * .15 ass2Mark * .35 ass3Mark * .50);
finalMark.setText(yearMark*.49 examMark*.5);
}
});
}
}
My XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@ id/layoutField"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_700"
android:importantForAccessibility="no"
android:textAlignment="center"
android:visibility="visible"
tools:context=".MainActivity"
tools:visibility="visible"
>
<EditText
android:id="@ id/studentNum"
android:layout_width="413dp"
android:layout_height="50dp"
android:autofillHints=""
android:background="@color/black"
android:hint="@string/studentNum"
android:linksClickable="false"
android:longClickable="false"
android:text="@string/studentNum"
android:textAlignment="center"
android:textColor="#009688"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="TouchTargetSizeCheck"
android:inputType="text"
/>
<EditText
android:id="@ id/assignment1"
android:layout_width="386dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:digits="0 - 100"
android:ems="10"
android:hint="@string/ass1Mark"
android:importantForAutofill="no"
android:inputType="number"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/studentNum"
tools:ignore="TouchTargetSizeCheck"
tools:text="@string/ass1Mark" />
<EditText
android:id="@ id/assignment2"
android:layout_width="386dp"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:autofillHints=""
android:digits="0 - 100"
android:ems="10"
android:hint="@string/ass2Mark"
android:inputType="number"
android:keepScreenOn="true"
android:minHeight="50dp"
android:singleLine="true"
android:text="@string/ass2Mark"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/assignment1" />
<EditText
android:id="@ id/assignment3"
android:layout_width="386dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:autofillHints="example 50"
android:digits="0 - 100"
android:ems="10"
android:hint="@string/ass3Mark"
android:inputType="number"
android:minHeight="50dp"
android:text="@string/ass3Mark"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.48"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/assignment2" />
<EditText
android:id="@ id/assignment4"
android:layout_width="386dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:digits="0 - 100"
android:ems="10"
android:hint="@string/examMark"
android:inputType="number"
android:textColor="#000000"
android:textColorHint="#000000"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/assignment3"
tools:ignore="TouchTargetSizeCheck"
tools:text="@string/examMark"
android:autofillHints="" />
<TextView
android:id="@ id/errorMsg"
android:layout_width="386dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/assignment4" />
<Button
android:id="@ id/CalcButton"
android:layout_width="386dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:background="@color/black"
android:backgroundTint="#000000"
android:backgroundTintMode="multiply"
android:foregroundGravity="right"
android:textAllCaps="true"
app:icon="@color/black"
app:iconTintMode="add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/errorMsg"
app:strokeColor="#009688"
tools:text="@string/calculate" />
<TextView
android:id="@ id/finalMark"
android:layout_width="236dp"
android:layout_height="50dp"
android:layout_marginStart="13dp"
android:layout_marginTop="20dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/yearMark"
tools:text="@string/finalMark" />
<TextView
android:id="@ id/status"
android:layout_width="138dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@ id/yearMark"
tools:text="@string/status" />
<TextView
android:id="@ id/yearMark"
android:layout_width="386dp"
android:layout_height="50dp"
android:layout_marginTop="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.52"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/CalcButton"
tools:text="@string/yearMark" />
</androidx.constraintlayout.widget.ConstraintLayout>
The error that I get on my java file
Operator '*' cannot be applied to 'android.widget.TextView', 'double'
CodePudding user response:
You can update your code as below. You have to take their value and parse them to a Double variable. and then you can calcualte and set those values to your desired TextViews.
public class MainActivity extends AppCompatActivity {
//onCreate Method
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initiating and referencing for Calculate Button
Button button = (Button)findViewById(R.id.CalcButton);
TextView ass1Mark = findViewById(R.id.assignment1);
TextView ass2Mark = findViewById(R.id.assignment2);
TextView ass3Mark = findViewById(R.id.assignment3);
TextView examMark = findViewById(R.id.assignment4);
TextView yearMark = findViewById(R.id.yearMark);
TextView finalMark = findViewById(R.id.finalMark);
//onClickListener for the Calculate Button
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//TODO Auto generated method stub
//Variables declared
//Calculations to execute when button is pressed
Double year_mark = Double.parseDouble(ass1Mark.getText().toString())*.15
Double.parseDouble(ass2Mark.getText().toString())*0.35 Double.parseDouble(ass3Mark.getText().toString())*0.50;
Double final_mark = year_mark*0.49 Double.parseDouble(examMark.getText().toString())*0.5;
yearMark.setText(year_mark.toString());
finalMark.setText(final_mark.toString());
}
});
}
}
CodePudding user response:
You're trying to multiply a TextView
and a number (double
) as mentioned in the error Operator '*' cannot be applied to 'android.widget.TextView', 'double'
. How can it work?
What you've to do is first get the value from the TextView
, parse it to int
as it will be a String
and then multiply it.
To get the value from a TextView, its getText()
is used as
String text = ass2Mark.getText().toString();
Then, Parse it to integer/double as required:
//For Integer
int number = Integer.parseInt(text);
//For Double
double number = Double.parseDouble(text);
Then only, you can multiply it.
CodePudding user response:
You are trying to apply the * operator to an object of type TextView, at most the thing you could do is
double value = Double.parseDouble(editText.getText())
and then apply the operator *