Android Studio : Error parsing XML, failed to build application - java

When i'm trying to build my application it gives me the following to errors.
Error:(21) Error parsing XML: not well-formed (invalid token)
Error:Execution failed for task ':app:processDebugResources'.
com.android.ide.common.process.ProcessException: Failed to execute aapt
Here is my XML code, and this is the only XML code, please help me.
package com.example.madhur.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class InnerWorkings extends Activity implements View.OnClickListener {
TextView tvvresults;
Button bvBackSpace, bvClear, bvPlusMinus, bvRoot, bvDivision, bvMultiply, bvSubtract, bvDot, bvEqual, bvAddition;
Button bv0, bv1, bv2, bv3, bv4, bv5, bv6, bv7, bv8, bv9;
int iResults, container, arithmaticCode, finalCalculation, remainder;
protected void onCreate(Bundle calculations) {
super.onCreate(calculations);
Initialization();
}
private void Initialization() {
tvvresults = (TextView) findViewById(R.id.tvResults);
bvBackSpace = (Button) findViewById(R.id.bBackSpace);
bvClear = (Button) findViewById(R.id.bClear);
bvPlusMinus = (Button) findViewById(R.id.bPlusMinus);
bvRoot = (Button) findViewById(R.id.bRoot);
bvDivision = (Button) findViewById(R.id.bDivision);
bvMultiply = (Button) findViewById(R.id.bMultiply);
bvSubtract = (Button) findViewById(R.id.bSubtract);
bvDot = (Button) findViewById(R.id.bDot);
bvEqual = (Button) findViewById(R.id.bEqual);
bvAddition = (Button) findViewById(R.id.bAddition);
bv0 = (Button) findViewById(R.id.b0);
bv1 = (Button) findViewById(R.id.b1);
bv2 = (Button) findViewById(R.id.b2);
bv3 = (Button) findViewById(R.id.b3);
bv4 = (Button) findViewById(R.id.b4);
bv5 = (Button) findViewById(R.id.b5);
bv6 = (Button) findViewById(R.id.b6);
bv7 = (Button) findViewById(R.id.b7);
bv8 = (Button) findViewById(R.id.b8);
bv9 = (Button) findViewById(R.id.b9);
bvBackSpace.setOnClickListener(this);
bvClear.setOnClickListener(this);
bvPlusMinus.setOnClickListener(this);
bvRoot.setOnClickListener(this);
bvDivision.setOnClickListener(this);
bvMultiply.setOnClickListener(this);
bvSubtract.setOnClickListener(this);
bvDot.setOnClickListener(this);
bvEqual.setOnClickListener(this);
bvAddition.setOnClickListener(this);
bv0.setOnClickListener(this);
bv1.setOnClickListener(this);
bv2.setOnClickListener(this);
bv3.setOnClickListener(this);
bv4.setOnClickListener(this);
bv5.setOnClickListener(this);
bv6.setOnClickListener(this);
bv7.setOnClickListener(this);
bv8.setOnClickListener(this);
bv9.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.bBackSpace:
remainder = iResults % 10;
iResults = iResults - remainder;
tvvresults.setText(iResults);
break;
case R.id.bClear:
iResults = 0;
tvvresults.setText(iResults);
break;
case R.id.bPlusMinus:
iResults = iResults * (-1);
tvvresults.setText(iResults);
break;
case R.id.bRoot:
break;
case R.id.bDivision:
arithmaticCode = 1;
container = iResults;
iResults = 0;
break;
case R.id.bMultiply:
arithmaticCode = 2;
container = iResults;
iResults = 0;
break;
case R.id.bSubtract:
arithmaticCode = 3;
container = iResults;
iResults = 0;
break;
case R.id.bDot:
break;
case R.id.bEqual:
Calculations();
break;
case R.id.bAddition:
arithmaticCode = 4;
container = iResults;
iResults = 0;
break;
case R.id.b0:
iResults = (iResults * 10);
tvvresults.setText(iResults);
break;
case R.id.b1:
iResults = (iResults * 10) + 1;
tvvresults.setText(iResults);
break;
case R.id.b2:
iResults = (iResults * 10) + 2;
tvvresults.setText(iResults);
break;
case R.id.b3:
iResults = (iResults * 10) + 3;
tvvresults.setText(iResults);
break;
case R.id.b4:
iResults = (iResults * 10) + 4;
tvvresults.setText(iResults);
break;
case R.id.b5:
iResults = (iResults * 10) + 5;
tvvresults.setText(iResults);
break;
case R.id.b6:
iResults = (iResults * 10) + 6;
tvvresults.setText(iResults);
break;
case R.id.b7:
iResults = (iResults * 10) + 7;
tvvresults.setText(iResults);
break;
case R.id.b8:
iResults = (iResults * 10) + 8;
tvvresults.setText(iResults);
break;
case R.id.b9:
iResults = (iResults * 10) + 9;
tvvresults.setText(iResults);
break;
}
}
private void Calculations() {
if (arithmaticCode == 1) {
finalCalculation = container / iResults;
tvvresults.setText(finalCalculation);
}
if (arithmaticCode == 2) {
finalCalculation = container * iResults;
tvvresults.setText(finalCalculation);
}
if (arithmaticCode == 3){
finalCalculation = container - iResults;
tvvresults.setText(finalCalculation);
}
if (arithmaticCode == 4){
finalCalculation = container + iResults;
tvvresults.setText(finalCalculation);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:textSize="20dp"
android:text="0"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/tvResults" />
<Button
android:text="<---"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bBackSpace"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"/>
<Button
android:text="C"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bClear"
android:layout_below="#id/tvResults"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="21dp"/>
<Button
android:text="+/-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bPlusMinus"
android:layout_below="#id/tvResults"
android:layout_toStartOf="#id/bRoot"
android:layout_marginEnd="20dp"
android:layout_marginTop="21dp" />
<Button
android:text="-v"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bRoot"
android:layout_marginEnd="21dp"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentEnd="true" />
<Button
android:text="7"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b7"
android:layout_below="#id/bBackSpace"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="8"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b8"
android:layout_below="#id/bBackSpace"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="9"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b9"
android:layout_below="#id/bBackSpace"
android:layout_toStartOf="#id/bDivision"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="/"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDivision"
android:layout_marginEnd="21dp"
android:layout_below="#id/bBackSpace"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="4"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b4"
android:layout_below="#id/b7"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="5"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b5"
android:layout_below="#id/b8"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="6"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b6"
android:layout_below="#id/b9"
android:layout_toStartOf="#id/bMultiply"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="*"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bMultiply"
android:layout_marginEnd="21dp"
android:layout_below="#id/bDivision"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="1"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b1"
android:layout_below="#id/b4"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="2"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b2"
android:layout_below="#id/b5"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="3"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b3"
android:layout_below="#id/b6"
android:layout_toStartOf="#id/bSubtract"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bSubtract"
android:layout_marginEnd="21dp"
android:layout_below="#id/bMultiply"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="0"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b0"
android:layout_below="#id/b1"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="="
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bEqual"
android:layout_below="#id/b3"
android:layout_toStartOf="#id/bAddition"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="+"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bAddition"
android:layout_marginEnd="21dp"
android:layout_below="#id/bSubtract"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="."
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDot"
android:layout_below="#id/b2"
android:layout_toEndOf="#id/b1"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout>

Try replacing android:text="<---" by android:text="<---", on line 21.

In Your Layout XML with one of the button you are using
android:text="<---"
This will cause error as "<" has special meaning for XMLs, you must perform escaping there are only five:
" "
' &apos;
< <
> >
& &
android:text="<--"
refer link:
What characters do I need to escape in XML documents?
https://www.liquid-technologies.com/XML/EscapingData.aspx

There are 2 errors in xml
1. use "<---" instead of "<---"
2. in the id multiply, add, subtract, root add the "#+id/" before id ..
Solved xml for your problem.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:gravity="center"
android:textSize="20dp"
android:text="0"
android:layout_width="match_parent"
android:layout_height="30dp"
android:id="#+id/tvResults" />
<Button
android:text="<---"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bBackSpace"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"/>
<Button
android:text="C"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bClear"
android:layout_below="#id/tvResults"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="21dp"/>
<Button
android:text="+/-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bPlusMinus"
android:layout_below="#id/tvResults"
android:layout_toStartOf="#+id/bRoot"
android:layout_marginEnd="20dp"
android:layout_marginTop="21dp" />
<Button
android:text="-v"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bRoot"
android:layout_marginEnd="21dp"
android:layout_marginTop="21dp"
android:layout_below="#id/tvResults"
android:layout_alignParentEnd="true" />
<Button
android:text="7"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b7"
android:layout_below="#id/bBackSpace"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="8"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b8"
android:layout_below="#id/bBackSpace"
android:layout_toEndOf="#id/bBackSpace"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="9"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b9"
android:layout_below="#id/bBackSpace"
android:layout_toStartOf="#+id/bDivision"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="/"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDivision"
android:layout_marginEnd="21dp"
android:layout_below="#+id/bBackSpace"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="4"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b4"
android:layout_below="#id/b7"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="5"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b5"
android:layout_below="#id/b8"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="6"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b6"
android:layout_below="#id/b9"
android:layout_toStartOf="#+id/bMultiply"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="*"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bMultiply"
android:layout_marginEnd="21dp"
android:layout_below="#id/bDivision"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="1"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b1"
android:layout_below="#id/b4"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="2"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b2"
android:layout_below="#id/b5"
android:layout_toEndOf="#id/b4"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="3"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b3"
android:layout_below="#id/b6"
android:layout_toStartOf="#+id/bSubtract"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="-"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bSubtract"
android:layout_marginEnd="21dp"
android:layout_below="#+id/bMultiply"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="0"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/b0"
android:layout_below="#id/b1"
android:layout_alignParentStart="true"
android:layout_marginStart="21dp"
android:layout_marginTop="20dp"/>
<Button
android:text="="
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bEqual"
android:layout_below="#id/b3"
android:layout_toStartOf="#+id/bAddition"
android:layout_marginEnd="20dp"
android:layout_marginTop="20dp" />
<Button
android:text="+"
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bAddition"
android:layout_marginEnd="21dp"
android:layout_below="#id/bSubtract"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"/>
<Button
android:text="."
android:textStyle="bold"
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/bDot"
android:layout_below="#id/b2"
android:layout_toEndOf="#id/b1"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"/>
</RelativeLayout>
</LinearLayout>

Related

How to add a history to calculator?

How do I add a history for a calculator in android studio/java? I need it to be only 3 lines of history before it disapears. How do I also show the entire calculation, and not just print out the result?
first one is where i store all my functions, and the 2nd one is primarily just to customize all the calculator buttons, etc.
import static com.example.calculator.R.string.display;
import androidx.appcompat.app.AppCompatActivity;
import org.mariuszgromada.math.mxparser.*;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = findViewById(R.id.input);
display.setShowSoftInputOnFocus(false);
display.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (getString(R.string.display).equals(display.getText().toString())){
display.setText("");
}
}
});
}
private void updateText(String strToAdd){
String oldStr = display.getText().toString();
int cursorPos = display.getSelectionStart();
String leftStr = oldStr.substring(0, cursorPos); //code that prints the numbers on the screen
String rightStr = oldStr.substring(cursorPos);
if (getString(R.string.display).equals(display.getText().toString())){
display.setText(strToAdd);
display.setSelection(cursorPos + 1);
}
else{
display.setText(String.format("%s%s%s", leftStr, strToAdd, rightStr));
display.setSelection(cursorPos + 1); //moves cursor position to one right
}
}
public void zeroBTN(View view){
updateText("0");
}
public void oneBTN(View view){
updateText("1");
}
public void twoBTN(View view){
updateText("2");
}
public void threeBTN(View view){
updateText("3");
}
public void fourBTN(View view){
updateText("4");
}
public void fiveBTN(View view){
updateText("5");
}
public void sixBTN(View view){
updateText("6");
}
public void sevenBTN(View view){
updateText("7");
}
public void eightBTN(View view){
updateText("8");
}
public void nineBTN(View view){
updateText("9");
}
public void multiplyBTN(View view){
updateText("×");
}
public void divideBTN(View view){
updateText("÷");
}
public void subtractBTN(View view){
updateText("-");
}
public void addBTN(View view){
updateText("+");
}
public void clearBTN(View view){
display.setText("");
}
public void expBTN(View view){
updateText("^");
}
public void parenthesesBTN(View view){
int cursorPos = display.getSelectionStart();
int openPar = 0;
int closedPar = 0;
int textLength = display.getText().length();
for (int i = 0; i < cursorPos; i++){
if (display.getText().toString().substring(i, i+1).equals("(")){
openPar += 1;
}
if (display.getText().toString().substring(i, i+1).equals(")")){
closedPar += 1;
}
}
if (openPar == closedPar || display.getText().toString().substring(textLength - 1, textLength).equals("(")){
updateText("(");
display.setSelection(cursorPos + 1);
}
else if (closedPar < openPar && !display.getText().toString().substring(textLength - 1, textLength).equals("(")){
updateText(")");
}
display.setSelection(cursorPos + 1);
}
public void plusMinusBTN(View view){
updateText("thx for using my calc, you found an easter egg :)");
}
public void decimalBTN(View view){
updateText(".");
}
public void equalBTN(View view){
String userExp = display.getText().toString();
userExp = userExp.replaceAll("÷", "/");
userExp = userExp.replaceAll("×", "*");
Expression exp = new Expression(userExp);
String result = String.valueOf(exp.calculate());
display.setText(result);
display.setSelection(result.length());
}
public void backspaceBTN(View view){
int cursorPos = display.getSelectionStart();
int textLen = display.getText().length();
if (cursorPos != 0 && textLen !=0){ //code to delete stuff
SpannableStringBuilder selection = (SpannableStringBuilder) display.getText();
selection.replace(cursorPos - 1, cursorPos, "");
display.setSelection(cursorPos - 1);
}
}
}
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/tableLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/input"
app:layout_constraintVertical_bias="0.95"
tools:layout_editor_absoluteX="1dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/clearBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="clearBTN"
android:text="#string/clear"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/parenthesesBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="parenthesesBTN"
android:text="#string/parentheses"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/exponentBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="expBTN"
android:text="#string/exponent"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/divideBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="divideBTN"
android:text="#string/divide"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/sevenBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="sevenBTN"
android:text="#string/seven"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/eightBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="eightBTN"
android:text="#string/eight"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/nineBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="nineBTN"
android:text="#string/nine"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/multiplyBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="multiplyBTN"
android:text="#string/multiply"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/fourBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="fourBTN"
android:text="#string/four"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/fiveBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="fiveBTN"
android:text="#string/five"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/sixBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="sixBTN"
android:text="#string/six"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/subtractBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="subtractBTN"
android:text="#string/subtract"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/one"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="oneBTN"
android:text="#string/one"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/two"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="twoBTN"
android:text="#string/two"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/three"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="threeBTN"
android:text="#string/three"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/add"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="addBTN"
android:text="#string/add"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/plusMinusBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="plusMinusBTN"
android:text="#string/plusMinus"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/zeroBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="zeroBTN"
android:text="#string/zero"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/pointBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="decimalBTN"
android:text="#string/point"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
<Button
android:id="#+id/equalsBTN"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#drawable/circle"
android:onClick="equalBTN"
android:text="#string/equals"
android:textColor="#000000"
android:textSize="24sp"
tools:ignore="UsingOnClickInXml" />
</TableRow>
</TableLayout>
<EditText
android:id="#+id/input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="#string/display"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:autofillHints="false"
android:inputType="none"
android:textSize="36sp"
android:textAlignment="textEnd"
tools:ignore="LabelFor" />
<ImageButton
android:id="#+id/backspace"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#drawable/circle"
android:contentDescription="#string/backspace"
android:onClick="backspaceBTN"
app:layout_constraintBottom_toTopOf="#+id/tableLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#drawable/backspace"
tools:ignore="UsingOnClickInXml" />
</androidx.constraintlayout.widget.ConstraintLayout>

Android studio checkbox gets unchecked when moved from one screen to next screen

I am new to android studio, I tried all possible options, but unable to understand why checkbox gets unchecked when moved from one screen to other when I come back to previous screen.
I am missing out something in onsavedInstanceState or onRestoreInstanceState
Below is my Java Code
package com.example.qualitycheckstation;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class Qc2_1_1 extends AppCompatActivity {
CheckBox qc2_1cb1a,qc2_1cb1b,qc2_1cb1c, qc2_1cb2a,qc2_1cb2b,qc2_1cb2c ,qc2_1cb3a,qc2_1cb3b, qc2_1cb3c;
TextView date2_1_1, time2_1_1;
Button next2_1_1,back2_1_1;
Boolean myBoolean1,myBoolean2,myBoolean3,myBoolean4,myBoolean5,myBoolean6,myBoolean7,myBoolean8,myBoolean9;
private static String pr2_1_1,pr2_1_2,pr2_1_3;
private boolean myBoolean = false;
public static String getPr2_1_1 (){
return pr2_1_1;
}
public static String getPr2_1_2 (){
return pr2_1_2;
}
public static String getPr2_1_3(){
return pr2_1_3;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qc2_1_1);
date2_1_1= findViewById(R.id.date2_1_1);
time2_1_1 = findViewById(R.id.time2_1_1);
Calendar calendar= Calendar.getInstance();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
String date = simpleDateFormat.format(calendar.getTime());
date2_1_1.setText(date);
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("hh:mm:ss a");
String time0 = simpleDateFormat1.format(calendar.getTime());
time2_1_1.setText(time0);
qc2_1cb1a = findViewById(R.id.qc2_1cb1a);
qc2_1cb1b = findViewById(R.id.qc2_1cb1b);
qc2_1cb1c = findViewById(R.id.qc2_1cb1c);
qc2_1cb2a = findViewById(R.id.qc2_1cb2a);
qc2_1cb2b = findViewById(R.id.qc2_1cb2b);
qc2_1cb2c = findViewById(R.id.qc2_1cb2c);
qc2_1cb3a = findViewById(R.id.qc2_1cb3a);
qc2_1cb3b = findViewById(R.id.qc2_1cb3b);
qc2_1cb3c = findViewById(R.id.qc2_1cb3c);
next2_1_1 = findViewById(R.id.next2_1_1);
back2_1_1 = findViewById(R.id.back2_1_1);
next2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if((qc2_1cb1a.isChecked())){
pr2_1_1 = "O";
}
else if((qc2_1cb1b.isChecked())){
pr2_1_1 = "X";
}
else if((qc2_1cb1c.isChecked())){
pr2_1_1 = "N/A";
}
if((qc2_1cb2a.isChecked())){
pr2_1_2 = "O";
}
else if((qc2_1cb2b.isChecked())){
pr2_1_2 = "X";
}
else if((qc2_1cb2c.isChecked())){
pr2_1_2 = "N/A";
}
if((qc2_1cb3a.isChecked())){
pr2_1_3 = "O";
}
else if((qc2_1cb3b.isChecked())){
pr2_1_3 = "X";
}
else if((qc2_1cb3c.isChecked())){
pr2_1_3 = "N/A";
}
if (
((qc2_1cb1a.isChecked()) && (qc2_1cb1b.isChecked())) || ((qc2_1cb1b.isChecked()) && (qc2_1cb1c.isChecked())) || ((qc2_1cb1a.isChecked()) && (qc2_1cb1c.isChecked())) ||
((qc2_1cb2a.isChecked()) && (qc2_1cb2b.isChecked())) || ((qc2_1cb2b.isChecked()) && (qc2_1cb2c.isChecked())) || ((qc2_1cb2a.isChecked()) && (qc2_1cb2c.isChecked())) ||
((qc2_1cb3a.isChecked()) && (qc2_1cb3b.isChecked())) || ((qc2_1cb3b.isChecked()) && (qc2_1cb3c.isChecked())) || ((qc2_1cb3a.isChecked()) && (qc2_1cb3c.isChecked()))
)
{ { Toast.makeText(getApplicationContext(),"Select Either OK, NG or N/A", Toast.LENGTH_SHORT).show(); }}
else if (
((qc2_1cb1a.isChecked()) || (qc2_1cb1b.isChecked()) || (qc2_1cb1c.isChecked())) &&
((qc2_1cb2a.isChecked()) || (qc2_1cb2b.isChecked()) || (qc2_1cb2c.isChecked()))&&
((qc2_1cb3a.isChecked()) || (qc2_1cb3b.isChecked()) || (qc2_1cb3c.isChecked()))
)
{ Intent intent = new Intent(Qc2_1_1.this, Qc2_1_2.class);startActivity(intent); }
else { Toast.makeText(getApplicationContext(),"Select all parameters", Toast.LENGTH_SHORT).show(); }
}
});
back2_1_1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Qc2_1_1.this, Qc2.class);
startActivity(intent);
}
});
}
#Override
protected void onSaveInstanceState(#NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("qc2_1cb1a", qc2_1cb1a.isChecked());
outState.putBoolean("qc2_1cb1b", qc2_1cb1b.isChecked());
outState.putBoolean("qc2_1cb1c", qc2_1cb1c.isChecked());
outState.putBoolean("qc2_1cb2a", qc2_1cb2a.isChecked());
outState.putBoolean("qc2_1cb2b", qc2_1cb2b.isChecked());
outState.putBoolean("qc2_1cb2c", qc2_1cb2c.isChecked());
outState.putBoolean("qc2_1cb3a", qc2_1cb3a.isChecked());
outState.putBoolean("qc2_1cb3b", qc2_1cb3b.isChecked());
outState.putBoolean("qc2_1cb3c", qc2_1cb3c.isChecked());
}
#Override
protected void onRestoreInstanceState(#NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
qc2_1cb1a.setChecked(myBoolean1);
qc2_1cb1b.setChecked(myBoolean2);
qc2_1cb1c.setChecked(myBoolean3);
qc2_1cb2a.setChecked(myBoolean4);
qc2_1cb2b.setChecked(myBoolean5);
qc2_1cb2c.setChecked(myBoolean6);
qc2_1cb3a.setChecked(myBoolean7);
qc2_1cb3b.setChecked(myBoolean8);
qc2_1cb3c.setChecked(myBoolean9);
}
}
xml Code
<TableLayout
android:layout_width="match_parent"
android:layout_height="462dp"
android:layout_marginStart="15dp"
android:layout_marginTop="125dp"
android:layout_marginEnd="15dp"
android:background="#color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="Sl No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Measured Item"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="199dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="OP No"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text="Gauge Used"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Parameter"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="91dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="OK"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="92dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="NG"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:layout_width="96dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="N/A"
android:textColor="#color/black"
android:textSize="25dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="1."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Groove Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="75dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="131dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Exist"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb1a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb1b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb1c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="2."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Serial Engraving Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="291dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="30"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="41dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="Numbers should be readable"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb2a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb2b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb2c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="125dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:gravity="center"
android:text="3."
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="Machining Surface"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="251dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.25"
android:background="#a3addb"
android:gravity="center"
android:text="25"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="31dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="2"
android:background="#a3addb"
android:gravity="center"
android:text=" Visual"
android:textColor="#color/black"
android:textSize="25dp" />
<TextView
android:layout_width="328dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight="1.5"
android:background="#a3addb"
android:gravity="center"
android:text="No rough surface, unwash surface,scratch, burr"
android:textColor="#color/black"
android:textSize="25dp" />
<CheckBox
android:id="#+id/qc2_1cb3a"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_green" />
<CheckBox
android:id="#+id/qc2_1cb3b"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
<CheckBox
android:id="#+id/qc2_1cb3c"
android:layout_width="64dp"
android:layout_height="match_parent"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:layout_weight=".25"
android:background="#a3addb"
android:button="#drawable/checkbox_red" />
</TableRow>
</TableLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="360dp"
android:layout_marginTop="32dp"
android:text="QC Station 2.1"
android:textColor="#color/black"
android:textSize="50dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="211dp"
android:layout_height="68dp"
android:layout_marginStart="25dp"
android:layout_marginTop="25dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<TextView
android:id="#+id/date2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="DD/MM/YYYY"
android:inputType="date"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/time2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:layout_marginEnd="50dp"
android:ems="6"
android:hint="HH.MM.SS"
android:inputType="time"
android:textSize="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="next"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="#+id/back2_1_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginBottom="30dp"
android:background="#drawable/button"
android:text="Back"
android:textColor="#color/white"
android:textSize="25dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
Any support to solve this would be of great help
You should get values from savedInstanceState in onCreate() method. Someting like:
if (savedInstanceState!=null){
myBoolean1 = savedInstanceState.getBoolean("qc2_1cb1a");
myBoolean2 = savedInstanceState.getBoolean("qc2_1cb1b");
myBoolean3 = savedInstanceState.getBoolean("qc2_1cb1c");
myBoolean4 = savedInstanceState.getBoolean("qc2_1cb2a");
myBoolean5 = savedInstanceState.getBoolean("qc2_1cb2b");
myBoolean6 = savedInstanceState.getBoolean("qc2_1cb2c");
myBoolean7 = savedInstanceState.getBoolean("qc2_1cb3a");
myBoolean8 = savedInstanceState.getBoolean("qc2_1cb3b");
myBoolean9 = savedInstanceState.getBoolean("qc2_1cb3c");
}
and then assign it to your check boxes

Error inflating class android.widget.GridLayout app crash

When I try to run my app in Android Studio emulator everything works fine, but when I try to run it on my phone (LG G4) the app crashes and the error on the logs is:
Error inflating class android.widget.GridLayout
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="368dp"
android:layout_height="360dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/board"
android:columnCount="3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:rowCount="3">
<ImageView
android:id="#+id/imageView"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="0"
app:layout_column="0"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="1"
app:layout_column="1"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:onClick="dropIn"
android:tag="2"
app:layout_column="2"
app:layout_row="0" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="3"
app:layout_column="0"
app:layout_row="1" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="4"
app:layout_column="1"
app:layout_row="1" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="5"
app:layout_column="2"
app:layout_row="1" />
<ImageView
android:id="#+id/imageView9"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="6"
app:layout_column="0"
app:layout_row="2" />
<ImageView
android:id="#+id/imageView10"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="7"
app:layout_column="1"
app:layout_row="2" />
<ImageView
android:id="#+id/imageView11"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="10dp"
android:layout_marginTop="25dp"
android:onClick="dropIn"
android:tag="8"
app:layout_column="2"
app:layout_row="2" />
</GridLayout>
<LinearLayout
android:id="#+id/playAgainLayout"
android:layout_width="189dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="100dp"
android:layout_marginRight="8dp"
android:layout_marginStart="100dp"
android:layout_marginTop="8dp"
android:background="#46d65e"
android:orientation="vertical"
android:padding="30dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/gridLayout"
app:layout_constraintHorizontal_bias="0.011"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView"
android:textSize="30sp" />
<Button
android:id="#+id/playAgainButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="playAgain"
android:text="Play Again" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Here is my java:
public class MainActivity extends AppCompatActivity {
// 0 = yellow, 1 = red;
int activePlayer = 0;
// 2 means unplayed
int [] gameState= {2, 2, 2, 2, 2, 2, 2, 2, 2};
int [] [] winningPositions = {{0,1,2}, {3,4,5}, {6,7,8}, {0,3,6}, {1,4,7}, {2,5,8}, {0,4,8}, {2,4,6}};
public void dropIn (View view){
ImageView counter = (ImageView) view;
System.out.println(counter.getTag().toString());
int tappedCounter = Integer.parseInt(counter.getTag().toString());
if (gameState[tappedCounter] == 2) {
gameState[tappedCounter] = activePlayer;
counter.setTranslationY(-1000f);
if (activePlayer == 0) {
counter.setImageResource(R.drawable.yellow);
activePlayer = 1;
} else {
counter.setImageResource(R.drawable.red);
activePlayer = 0;
}
counter.animate().translationYBy(1000f).rotation(360f).setDuration(500);
for (int [] winningPosition : winningPositions){
if (gameState[winningPosition[0]] == gameState[winningPosition[1]] &&
gameState[winningPosition[1]] == gameState[winningPosition[2]] &&
gameState[winningPosition[0]] != 2){
//Someone has won
String winner = "Red";
if (gameState[winningPosition[0]] == 0){
winner = "Yellow";
}
TextView winnerMeassage = (TextView) findViewById(R.id.winnerMessage);
winnerMeassage.setText(winner + " has won!");
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.VISIBLE);
}
}
}
}
public void playAgain (View view){
LinearLayout layout = (LinearLayout) findViewById(R.id.playAgainLayout);
layout.setVisibility(View.INVISIBLE);
// 0 = yellow, 1 = red;
activePlayer = 0;
// 2 means unplayed
for (int i = 0 ; i < gameState.length ; i++){
gameState[i] = 2;
}
// setting all the images source to nothing
GridLayout gridLayout = (GridLayout) findViewById(R.id.gridLayout);
for(int i = 0 ; i<gridLayout.getChildCount() ; i++){
((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I don't think the code will help but it is here if you want to see it.
Please check your drawable folder. Sometimes when your images are in v24 drawable folder this error happens move your images to drawable folder.
Try withe support library version.
add via gradle
implementation com.android.support:gridlayout-v7:27.1.1
replace <GridLayout> with <android.support.v7.widget.GridLayout>
Also change the import to support library

Cannot cast View to Switch

I need a really simple thing done, but I keep getting errors. I have multiple switches with each a distinct id. Everytime I use (Switch)findViewById(), it gives me the following error: Cannot cast View to Switch. s is defined as a Switch.
I did some research and I have tried to clean my project and delete my R file, but it still doesn't work.
protected void onCreate(Bundle savedInstanceState) {
...
switches = weekprogram.getSwitches(day); //returns an arraylist with switches
for (int j = 0; j < switches.size(); j++) {
switch (j) {
case 5:
b = (Button) findViewById(R.id.timeBOne);
s = (Switch) findViewById(R.id.switchOne);
break;
case 6:
b = (Button) findViewById(R.id.timeBTwo);
s = (Switch) findViewById(R.id.switchTwo);
break;
case 7:
b = (Button) findViewById(R.id.timeBThree);
s = (Switch) findViewById(R.id.switchThree);
break;
case 8:
b = (Button) findViewById(R.id.timeBFour);
s = (Switch) findViewById(R.id.switchFour);
break;
case 9:
b = (Button) findViewById(R.id.timeBFive);
s = (Switch) findViewById(R.id.switchFive);
break;
case 0:
b = (Button) findViewById(R.id.timeBSix);
s = (Switch) findViewById(R.id.switchSix);
break;
case 1:
b = (Button) findViewById(R.id.timeBseven);
s = (Switch) findViewById(R.id.switchSeven);
break;
case 2:
b = (Button) findViewById(R.id.timeBEight);
s = (Switch) findViewById(R.id.switchEight);
break;
case 3:
b = (Button) findViewById(R.id.timeBNine);
s = (Switch) findViewById(R.id.switchNine);
break;
case 4:
b = (Button) findViewById(R.id.timeBTen);
s = (Switch) findViewById(R.id.switchTen);
break;
}
...
}
XML File:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundforapp"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.hti_thermostat.SelectedDay$PlaceholderFragment" >
<Button
android:id="#+id/timeBNine"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBTen"
android:layout_alignLeft="#+id/timeBTen"
android:text="09:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBEight"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBNine"
android:layout_alignLeft="#+id/timeBFive"
android:text="08:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton"/>
<Button
android:id="#+id/timeBseven"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBEight"
android:layout_alignLeft="#+id/timeBEight"
android:text="07:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBSix"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBseven"
android:layout_alignLeft="#+id/timeBseven"
android:text="06:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBTen"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/timeBEight"
android:layout_alignParentBottom="true"
android:layout_marginBottom="26dp"
android:text="10:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBFive"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/timeBFour"
android:layout_centerVertical="true"
android:text="05:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBFour"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBFive"
android:layout_alignLeft="#+id/timeBThree"
android:text="04:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBThree"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBFour"
android:layout_alignLeft="#+id/DayofWeek"
android:text="03:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBTwo"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBThree"
android:layout_alignLeft="#+id/timeBThree"
android:text="02:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton" />
<Button
android:id="#+id/timeBOne"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="30dp"
android:layout_above="#+id/timeBTwo"
android:layout_alignLeft="#+id/DayofWeek"
android:text="01:30 AM"
android:textSize="13sp"
android:onClick="setTimeButton"/>
<TextView
android:id="#+id/DayofWeek"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/timeBOne"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:text="Monday"
android:textColor="#ECDFD5"
android:textSize="35sp" />
<ImageView
android:id="#+id/sunsD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/timeBOne"
android:src="#drawable/sunshine" />
<ImageView
android:id="#+id/moonsD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/sunsD"
android:layout_alignTop="#+id/timeBSix"
android:src="#drawable/moon" />
<Switch
android:id="#+id/switchOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/timeBOne" />
<Switch
android:id="#+id/switchTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/switchOne"
android:layout_alignTop="#+id/timeBTwo" />
<Switch
android:id="#+id/switchThree"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBThree"
android:layout_alignLeft="#+id/switchTwo" />
<Switch
android:id="#+id/switchFour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/switchThree"
android:layout_below="#+id/timeBThree" />
<Switch
android:id="#+id/switchSix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/switchFive"
android:layout_alignTop="#+id/timeBSix" />
<Switch
android:id="#+id/switchSeven"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/switchSix"
android:layout_below="#+id/timeBSix" />
<Switch
android:id="#+id/switchEight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBEight"
android:layout_alignLeft="#+id/switchSeven" />
<Switch
android:id="#+id/switchNine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBNine"
android:layout_alignLeft="#+id/switchEight" />
<Switch
android:id="#+id/switchTen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBTen"
android:layout_alignLeft="#+id/switchNine" />
<Switch
android:id="#+id/switchFive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/timeBFive"
android:layout_alignLeft="#+id/switchFour" />
You have another Switch class in your project and the code is using that instead of android.widget.Switch.
If the other Switch is in another package, just change the imports to import android.widget.Switch.
If it is in the same package, you can explicitly refer to the platform class by replacing Switch with android.widget.Switch in code.

Enter data in EditText without default keyboard [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How to enter data in my EditText using my own buttons instead of default keyboard (I mean like in calculator apps). Please write me some method or show suitable example. Thanks.
I initialize veriable, even tried to make some algorithm. But there is no reason to go further without knowing how to input data.
And of course I was looking for the answer.
public class MainActivity extends Activity implements OnClickListener{
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bPlus,bMin,bDiv,bMult,bEqual;
String sN1,sN2,func,result;
EditText etEnter;
SoundPool pool; int shot = 0;
TextView tvShow;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
initVars();
}
private void initVars(){
etEnter =(EditText)findViewById(R.id.etCalc);
tvShow = (TextView)findViewById(R.id.tvShow);
b0 = (Button)findViewById(R.id.button0);
b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
b5 = (Button)findViewById(R.id.button5);
b6 = (Button)findViewById(R.id.butt6);
b7 = (Button)findViewById(R.id.butt7);
b8 = (Button)findViewById(R.id.butt8);
b9 = (Button)findViewById(R.id.butt9);
bMin = (Button)findViewById(R.id.bMinus);
bPlus = (Button)findViewById(R.id.bPlus);
bDiv = (Button)findViewById(R.id.bDiv);
bMult = (Button)findViewById(R.id.bMult);
bEqual = (Button)findViewById(R.id.bEqual);
b0.setOnClickListener(this);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
bMin.setOnClickListener(this);
bPlus.setOnClickListener(this);
bDiv.setOnClickListener(this);
bMult.setOnClickListener(this);
bEqual.setOnClickListener(this);
pool = new SoundPool(5,AudioManager.STREAM_MUSIC,0);
shot = pool.load(this, R.raw.shot, 1);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button0:
break;
case R.id.button1:
break;
case R.id.button2:
break;
case R.id.button3:
break;
case R.id.button4:
break;
case R.id.button5:
break;
case R.id.butt6:
break;
case R.id.butt7:
break;
case R.id.butt8:
break;
case R.id.butt9:
break;
case R.id.bMinus:
func = "-";
break;
case R.id.bPlus:
func = "+";
break;
case R.id.bDiv:
func = "/";
break;
case R.id.bMult:
func = "*";
break;
case R.id.bEqual:
pool.play(shot, 1, 1, 0, 0, 1);
if(func.contentEquals("+")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1+l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("-")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1-l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("/")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1/l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}else if(func.contentEquals("*")){
if(sN1!=null && sN2!=null){
long l1 = Long.parseLong(sN1);
long l2 = Long.parseLong(sN2);
long lRes = l1*l2;
result = String.valueOf(lRes);
tvShow.setText(result);
}else if(sN1!=null && sN2==null){
tvShow.setText(sN1);
}
sN1=sN2=null;
}
break;
}
}
and XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rainbow"
>
<LinearLayout android:paddingTop="22dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<EditText
android:id="#+id/etCalc"
android:layout_width="260dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="right"
android:textSize="30dp"
android:inputType="number" />
</LinearLayout>
<LinearLayout android:paddingRight="33dp" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvShow"
android:layout_width="100dp"
android:layout_height="45dp"
android:text=":-)"
android:textSize="35dp"
android:paddingRight="10dp"
android:layout_gravity="right"
android:background="#drawable/rect_buttons"
android:gravity="right"
></TextView>"
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:paddingTop="20dp" >
<Button
android:id="#+id/bPlus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:gravity="center"
android:text="+" />
<Button
android:id="#+id/bMinus"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="-" />
<Button
android:layout_gravity="center"
android:id="#+id/bEqual"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="=" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="1"
android:textSize="30dp" />
<Button
android:id="#+id/button2"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="2"
android:textSize="30dp" />
<Button
android:id="#+id/button3"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="3"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/button4"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="4"
android:textSize="30dp" />
<Button
android:id="#+id/button5"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="5"
android:textSize="30dp" />
<Button
android:id="#+id/butt6"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="6"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/butt7"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="7"
android:textSize="30dp" />
<Button
android:id="#+id/butt8"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="8"
android:textSize="30dp" />
<Button
android:id="#+id/butt9"
android:layout_width="70dp"
android:layout_height="70dp"
android:text="9"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" >
<Button
android:id="#+id/bDiv"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="/" />
<Button
android:layout_gravity="center"
android:id="#+id/button0"
android:layout_width="70dp"
android:layout_height="50dp"
android:textSize="30dp"
android:text="0" />
<Button
android:id="#+id/bMult" android:layout_width="70dp" android:layout_height="50dp" android:textSize="30dp" android:text="*" />
</LinearLayout>
</LinearLayout>
You have to make your editText non focusable/editable and modify the text inside when you click on your buttons. Easy !

Categories