Issue
I'm new to android studio, and have created 3 buttons. Code of 1 of the button are as follows,
<Button
android:id="@+id/button2"
android:layout_width="160dp"
android:layout_height="120dp"
android:layout_marginBottom="434dp"
android:contentDescription="5"
android:text="$5"
android:textSize="70sp"
android:onClick="addTotal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize buttons
final Button button1 = findViewById(R.id.button1);
final Button button2 = findViewById(R.id.button2);
final Button button3 = findViewById(R.id.button3);
final TextView totalAmt = findViewById(R.id.totalAmt);
public void addTotal (View v) {
// $2 button
button1.setOnClickListener((View view) -> {
// Update total amount
double total = Double.parseDouble(totalAmt.getText().toString()) + 2;
String final_total = String.valueOf(total);
totalAmt.setText(final_total);
});
// $5 button
button2.setOnClickListener((View view) -> {
// Update total amount
double total = Double.parseDouble(totalAmt.getText().toString()) + 5;
String final_total = String.valueOf(total);
totalAmt.setText(final_total);
});
// $8 button
button3.setOnClickListener((View view) -> {
// Update total amount
double total = Double.parseDouble(totalAmt.getText().toString()) + 8;
String final_total = String.valueOf(total);
totalAmt.setText(final_total);
});
}
}
}
The purpose of the 3 buttons is to add either 2, 5, 8 to the total Textview
What am I doing wrong?
Thank you!
Solution
You Don't need to add onClick
in XML file. You have the wrong declaration in MainActivity. Copy following code it will work properly now
xml
<Button
android:id="@+id/button2"
android:layout_width="160dp"
android:layout_height="120dp"
android:layout_marginBottom="434dp"
android:contentDescription="5"
android:text="$5"
android:textSize="70sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.39" />
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button button1, button2, button3;
TextView totalAmt;
double total = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize buttons
button1 = findViewById(R.id.button1);
button2 = findViewById(R.id.button2);
button3 = findViewById(R.id.button3);
totalAmt = findViewById(R.id.totalAmt);
totalAmt.setText("0");
addTotal();
}
public void addTotal() {
// $2 button
button1.setOnClickListener((View view) -> {
// Update total amount
total = Double.parseDouble(totalAmt.getText().toString()) + 2;
String final_total = String.valueOf(total);
totalAmt.setText(final_total);
});
// $5 button
button2.setOnClickListener((View view) -> {
// Update total amount
total = Double.parseDouble(totalAmt.getText().toString()) + 5;
String final_total = String.valueOf(total);
totalAmt.setText(final_total);
});
// $8 button
button3.setOnClickListener((View view) -> {
// Update total amount
total = Double.parseDouble(totalAmt.getText().toString()) + 8;
String final_total = String.valueOf(total);
totalAmt.setText(final_total);
});
}
}
Answered By - Yaqoob Bhatti
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.