So I have been working on a Calculator app off a tutorial I got online. Everything's fine so far but I can't seem work to out what's causing this "NaN" error in my application every time I try to use the operator buttons. I'll attach screenshots of the code and app. Sorry if I'm vague I'm new to coding and just trying to learn.
My MainActivity.java
package com.example.calculator_prjct;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.app.Activity;
import com.example.calculator_prjct.databinding.ActivityMainBinding;
import android.os.Bundle;
import android.view.View;
import java.text.DecimalFormat;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
private char CURRENT_ACTION;
private double valueOne = Double.NaN;
private double valueTwo;
private DecimalFormat decimalFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
decimalFormat = new DecimalFormat("#.##########");
final ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.buttonDot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + ".");
}
});
binding.buttonZero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "0");
}
});
binding.buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "1");
}
});
binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "2");
}
});
binding.buttonThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "3");
}
});
binding.buttonFour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "4");
}
});
binding.buttonFive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "5");
}
});
binding.buttonSix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "6");
}
});
binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "7");
}
});
binding.buttonEight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "8");
}
});
binding.buttonNine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "9");
}
});
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
binding.buttonEquals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length() - 1));
} else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}
private void computeCalculation() {
if (!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);
if (CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if (CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if (CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
} else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
} catch (Exception e) {
}
}
}
}
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<RelativeLayout
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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/infoTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:textSize="30sp" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/infoTextView"
android:enabled="false"
android:gravity="bottom"
android:lines="2"
android:maxLines="2"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSeven"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:text="7"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFour"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="1dp"
android:layout_marginLeft="-87dp"
android:layout_marginTop="47dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="4"
android:textSize="20sp" />
<Button
android:id="#+id/buttonFive"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="1dp"
android:layout_marginTop="47dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="5"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSix"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="89dp"
android:layout_marginTop="46dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="6"
android:textSize="20sp" />
<Button
android:id="#+id/buttonOne"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="-1dp"
android:layout_marginLeft="-89dp"
android:layout_marginTop="96dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="1"
android:textSize="20sp" />
<Button
android:id="#+id/buttonTwo"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="-1dp"
android:layout_marginTop="95dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="2"
android:textSize="20sp" />
<Button
android:id="#+id/buttonThree"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_marginLeft="88dp"
android:layout_marginTop="97dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="3"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDot"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="-86dp"
android:layout_marginTop="144dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="."
android:textSize="20sp" />
<Button
android:id="#+id/buttonZero"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="90dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="144dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="0"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEquals"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_marginStart="179dp"
android:layout_marginLeft="91dp"
android:layout_marginTop="145dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="="
android:textSize="20sp" />
<Button
android:id="#+id/buttonClear"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="265dp"
android:layout_marginLeft="177dp"
android:layout_marginTop="197dp"
android:layout_toRightOf="#id/buttonSeven"
android:text="C"
android:textSize="20sp" />
<Button
android:id="#+id/buttonEight"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_toRightOf="#id/buttonSeven"
android:text="8"
android:textSize="20sp" />
<Button
android:id="#+id/buttonNine"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editText"
android:layout_toRightOf="#id/buttonEight"
android:text="9"
android:textSize="20sp" />
<Button
android:id="#+id/buttonDivide"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonNine"
android:layout_toRightOf="#id/buttonNine"
android:text="/"
android:textSize="20sp" />
<Button
android:id="#+id/buttonMultiply"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonDivide"
android:layout_marginLeft="-88dp"
android:layout_marginTop="48dp"
android:layout_toRightOf="#id/buttonDivide"
android:text="*"
android:textSize="20sp" />
<Button
android:id="#+id/buttonSubtract"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonMultiply"
android:layout_marginLeft="-86dp"
android:layout_marginTop="53dp"
android:layout_toRightOf="#id/buttonMultiply"
android:text="-"
android:textSize="20sp" />
<Button
android:id="#+id/buttonAdd"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/buttonSubtract"
android:layout_marginLeft="-89dp"
android:layout_marginTop="49dp"
android:layout_toRightOf="#id/buttonSubtract"
android:text="+"
android:textSize="20sp" />
</RelativeLayout>
</layout>
Screenshot of the actual error
"NaN Error on all operator buttons"
This is because you didn't check whether or not the EditText is empty in the respective onClick method of all the operator buttons. Add code like this to all of the operator buttons :
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = ADDITION;
//if editText was empty then valueOne would still be NaN
//and infoTextView will show NaN+ with this line of code
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
Full Activity Code :
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private static final char ADDITION = '+';
private static final char SUBTRACTION = '-';
private static final char MULTIPLICATION = '*';
private static final char DIVISION = '/';
private char CURRENT_ACTION;
private double valueOne = Double.NaN;
private double valueTwo;
private DecimalFormat decimalFormat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
decimalFormat = new DecimalFormat("#.##########");
binding.buttonDot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + ".");
}
});
binding.buttonZero.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "0");
}
});
binding.buttonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "1");
}
});
binding.buttonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "2");
}
});
binding.buttonThree.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "3");
}
});
binding.buttonFour.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "4");
}
});
binding.buttonFive.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "5");
}
});
binding.buttonSix.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "6");
}
});
binding.buttonSeven.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "7");
}
});
binding.buttonEight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "8");
}
});
binding.buttonNine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
binding.editText.setText(binding.editText.getText() + "9");
}
});
binding.buttonAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = ADDITION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "+");
binding.editText.setText(null);
}
});
binding.buttonSubtract.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = SUBTRACTION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "-");
binding.editText.setText(null);
}
});
binding.buttonMultiply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = MULTIPLICATION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "*");
binding.editText.setText(null);
}
});
binding.buttonDivide.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
CURRENT_ACTION = DIVISION;
binding.infoTextView.setText(decimalFormat.format(valueOne) + "/");
binding.editText.setText(null);
}
});
binding.buttonEquals.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(binding.editText.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this, "Enter number before performing operation", Toast.LENGTH_LONG).show();
return;
}
computeCalculation();
binding.infoTextView.setText(binding.infoTextView.getText().toString() +
decimalFormat.format(valueTwo) + " = " + decimalFormat.format(valueOne));
valueOne = Double.NaN;
CURRENT_ACTION = '0';
}
});
binding.buttonClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (binding.editText.getText().length() > 0) {
CharSequence currentText = binding.editText.getText();
binding.editText.setText(currentText.subSequence(0, currentText.length() - 1));
} else {
valueOne = Double.NaN;
valueTwo = Double.NaN;
binding.editText.setText("");
binding.infoTextView.setText("");
}
}
});
}
private void computeCalculation() {
if (!Double.isNaN(valueOne)) {
valueTwo = Double.parseDouble(binding.editText.getText().toString());
binding.editText.setText(null);
if (CURRENT_ACTION == ADDITION)
valueOne = this.valueOne + valueTwo;
else if (CURRENT_ACTION == SUBTRACTION)
valueOne = this.valueOne - valueTwo;
else if (CURRENT_ACTION == MULTIPLICATION)
valueOne = this.valueOne * valueTwo;
else if (CURRENT_ACTION == DIVISION)
valueOne = this.valueOne / valueTwo;
} else {
try {
valueOne = Double.parseDouble(binding.editText.getText().toString());
} catch (Exception e) {
}
}
}
}
Related
I think is happening in the switch case or there is any small error in my code.
Please help me out
Here is my code:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/edText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="180sp"
/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="230sp"
android:text="1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="230sp"
android:text="2"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="230sp"
android:text="3"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="280sp"
android:text="4" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="280sp"
android:text="5"/>
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="280sp"
android:text="6"/>
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="330sp"
android:text="7" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="330sp"
android:text="8"/>
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="330sp"
android:text="9"/>
<Button
android:id="#+id/buttonplus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="380sp"
android:text="+" />
<Button
android:id="#+id/button0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="380sp"
android:text="0"/>
<Button
android:id="#+id/buttoneq"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="380sp"
android:text="="/>
<Button
android:id="#+id/buttondiv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="430sp"
android:text="/" />
<Button
android:id="#+id/buttonmin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="170sp"
android:layout_marginTop="430sp"
android:text="-"/>
<Button
android:id="#+id/buttonmul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="280sp"
android:layout_marginTop="430sp"
android:text="*"/>
</RelativeLayout>
MainActivity.java:
package com.example.calculator;
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;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int operator = 0;
int s1 = 0,s2 = 0,add = 0,min = 0,mul = 0,div = 0;
EditText tv;
Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,beq,bplus,bmin,bmul,bdiv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
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.button6);
b7 = (Button)findViewById(R.id.button7);
b8 = (Button)findViewById(R.id.button8);
b9 = (Button)findViewById(R.id.button9);
b0 = (Button)findViewById(R.id.button0);
beq = (Button)findViewById(R.id.buttoneq);
bplus = (Button)findViewById(R.id.buttonplus);
bmin = (Button)findViewById(R.id.buttonmin);
bmul = (Button)findViewById(R.id.buttonmul);
bdiv = (Button)findViewById(R.id.buttondiv);
tv = (EditText) findViewById(R.id.edText);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "1");
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "2");
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "3");
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "4");
}
});
b5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "5");
}
});
b6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "6");
}
});
b7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "7");
}
});
b8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "8");
}
});
b9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(tv.getText() + "9");
}
});
b0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText( tv.getText() + "0");
}
});
bplus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 1;
}
});
bmin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 2;
}
});
bmul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 3;
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s1 = Integer.parseInt(tv.getText().toString());
tv.setText(null);
operator = 4;
}
});
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s2 = Integer.parseInt(tv.getText().toString());
add = s1 + s2;
min = s1 - s2;
mul = s1 * s2;
div = s1 / s2;
switch (operator){
case 1:
tv.setText(add);
break;
case 2:
tv.setText(min);
break;
case 3:
tv.setText(mul);
break;
case 4:
tv.setText(div);
break;
}
}
});
}
}
I have added 15 buttons to my calculator and everything works properly except when I click on equal to the button. Every button does works properly and prints the proper number into the textView when clicked but still when I click on the equal to button, my app just shuts down itself whenever I click on the equal to button on my calculator and nothing happens.
It seems like you need to parse integers to strings before you set the text. Try using String.valueOf() to parse add, min, mul, and div to strings.
Also, if you post your logcat, it'll be much easier to figure out what the problem is as it tells you what and where the error is.
You can not set integer value on Textview, only String will be acceptable. Try with the below code.
switch (operator){
case 1:
tv.setText(""+add);
break;
case 2:
tv.setText(""+min);
break;
case 3:
tv.setText(""+mul);
break;
case 4:
tv.setText(""+div);
break;
}
if you give an int value to setText(), it will be seen as a resId,so ,if system can not find this id, will throw new NotFoundException("String resource ID #0x"
+ Integer.toHexString(id));
Change eq listener to
beq.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(tv.getText().toString().length()==0)
return;
s2 = Integer.parseInt(tv.getText().toString());
add = s1 + s2;
min = s1 - s2;
mul = s1 * s2;
div = s1 / s2;
switch (operator) {
case 1:
tv.setText(String.valueOf(add));
break;
case 2:
tv.setText(String.valueOf(min));
break;
case 3:
tv.setText(String.valueOf(mul));
break;
case 4:
tv.setText(String.valueOf(div));
break;
}
}
});
Do not set integer value to text view directly. First cast it to string.
Try replacing this in your code.
I am using Android Studio 3.2 and My app is showing up but the buttons are not triggering actions please suggest any code changes so that they would trigger events on the app
The MainActivity code is :
package com.example.myfalconcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView expression;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button num1 = findViewById(R.id.num1);
expression = findViewById(R.id.expression);
Button num2 = findViewById(R.id.num2);
Button num3 = findViewById(R.id.num3);
Button num4 = findViewById(R.id.num4);
Button num5 = findViewById(R.id.num5);
Button num6 = findViewById(R.id.num6);
Button num7 = findViewById(R.id.num7);
Button num8 = findViewById(R.id.num8);
Button num9 = findViewById(R.id.num9);
Button num0 = findViewById(R.id.num0);
Button plus = findViewById(R.id.plus);
Button minus = findViewById(R.id.minus);
Button equalsTo = findViewById(R.id.equalsTo);
Button closeBrace = findViewById(R.id.closeBrace);
Button openBrace = findViewById(R.id.openBrace);
Button clear = findViewById(R.id.clear);
Button delete = findViewById(R.id.delete);
Button exponential = findViewById(R.id.exponential);
Button divideBy = findViewById(R.id.divideBy);
num1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "1");
}
});
num2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "2");
}
});
num3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "3");
}
});
num4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "4");
}
});
num5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "5");
}
});
num6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "6");
}
});
num7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "7");
}
});
num8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "8");
}
});
num9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "9");
}
});
num0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "0");
}
});
plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "+");
}
});
minus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "-");
}
});
exponential.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "*");
}
});
divideBy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "/");
}
});
equalsTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SimpleMathSolver mathSolver = new SimpleMathSolver(expression.getText().toString());
try {
expression.setText(String.valueOf(mathSolver.solve()));
} catch (Exception e) {
expression.setText("Input Not Valid");
}
}
});
closeBrace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + ")");
}
});
openBrace.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText(expression.getText().toString() + "(");
}
});
clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expression.setText("");
}
});
delete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String tempExpression = expression.getText().toString().substring(0, expression.getText().toString().length()-1);
expression.setText(tempExpression);
}
});
}
}
The Main Activity file is depending on another file:
package com.example.myfalconcalculator;
import java.util.Scanner;
import java.util.Stack;
public class SimpleMathSolver {
private String inputExp;
private final char PLUS = '+';
private final char MINUS = '-';
private final char MULTIPLY = '*';
private final char DIVIDE = '/';
private final char OPENBRACE = '(';
private final char CLOSEBRACE = ')';
public SimpleMathSolver(String inputExp) {
this.inputExp = inputExp;
}
public int solve() throws Exception {
String withoutBraceInputExp = solveBraces(this.inputExp);
return solveMath(withoutBraceInputExp);
}
private String solveBraces(String inputExp) throws Exception {
String output = inputExp;
int closeBraceIndex = output.indexOf(CLOSEBRACE);
while (closeBraceIndex >= 0) {
int openBraceIndex = output.substring(0, closeBraceIndex).lastIndexOf(OPENBRACE);
String solveStr = output.substring(openBraceIndex + 1, closeBraceIndex);
int value = solveMath(solveStr);
output = output.substring(0, openBraceIndex) + value
+ output.substring(closeBraceIndex + 1, output.length());
closeBraceIndex = output.indexOf(CLOSEBRACE);
}
return output;
}
private int solveMath(String inputExp) throws Exception {
Stack<Integer> numberStack = new Stack<Integer>();
Stack<Character> symbolStack = new Stack<Character>();
updateStacks(inputExp, numberStack, symbolStack);
solveMathSymbol(numberStack, symbolStack, DIVIDE);
solveMathSymbol(numberStack, symbolStack, MULTIPLY);
solveMathSymbol(numberStack, symbolStack, MINUS);
solveMathSymbol(numberStack, symbolStack, PLUS);
return numberStack.pop();
}
private void solveMathSymbol(Stack<Integer> numberStack, Stack<Character> symbolStack, char symbol) {
Stack<Integer> tempNumberStack = new Stack<Integer>();
Stack<Character> tempSymbolStack = new Stack<Character>();
while (symbolStack.size() > 0) {
char ch = symbolStack.pop();
if (ch == symbol) {
int pop1 = numberStack.pop();
int pop2 = numberStack.pop();
int out = 0;
switch (symbol) {
case PLUS:
out = pop2 + pop1;
break;
case MINUS:
out = pop2 - pop1;
break;
case MULTIPLY:
out = pop2 * pop1;
break;
case DIVIDE:
out = pop2 / pop1;
break;
}
numberStack.push(out);
} else {
tempSymbolStack.push(ch);
int numpop = numberStack.pop();
tempNumberStack.push(numpop);
}
}
while (tempSymbolStack.size() > 0) {
char charpop = tempSymbolStack.pop();
symbolStack.push(charpop);
int numpop = tempNumberStack.pop();
numberStack.push(numpop);
}
}
private void updateStacks(String inputExp, Stack<Integer> numberStack, Stack<Character> symbolStack)
throws Exception {
int number = 0;
for (int i = 0; i < inputExp.length(); i++) {
char ch = inputExp.charAt(i);
if (Character.isDigit(ch)) {
number = number * 10 + Integer.parseInt(String.valueOf(ch));
} else {
numberStack.push(number);
number = 0;
if (ch == PLUS || ch == MINUS || ch == MULTIPLY || ch == DIVIDE) {
symbolStack.push(ch);
} else {
throw new Exception("Unknown Math Symbol " + ch);
}
}
}
numberStack.push(number);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String retryOp = "N";
do {
System.out.println("Enter Math Expression");
String inputExp = scanner.nextLine();
SimpleMathSolver solver = new SimpleMathSolver(inputExp);
try {
System.out.println("Result: " + solver.solve());
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("Do you want to try this again");
retryOp = scanner.nextLine();
} while (retryOp.equalsIgnoreCase("Y"));
scanner.close();
System.exit(0);
}
}
this is the activity main.xml file:
<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">
<Button
android:id="#+id/closeBrace"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="1dp"
android:text=")"
app:layout_constraintBottom_toTopOf="#+id/equalsTo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/openBrace"
app:layout_constraintTop_toBottomOf="#+id/divideBy" />
<Button
android:id="#+id/openBrace"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginBottom="1dp"
android:text="("
app:layout_constraintBottom_toTopOf="#+id/equalsTo"
app:layout_constraintEnd_toStartOf="#+id/closeBrace"
app:layout_constraintStart_toEndOf="#+id/num9"
app:layout_constraintTop_toBottomOf="#+id/exponential" />
<Button
android:id="#+id/divideBy"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="/"
app:layout_constraintBottom_toTopOf="#+id/closeBrace"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/exponential"
app:layout_constraintTop_toBottomOf="#+id/minus" />
<Button
android:id="#+id/exponential"
android:layout_width="74dp"
android:layout_height="76dp"
android:text="*"
app:layout_constraintBottom_toTopOf="#+id/openBrace"
app:layout_constraintEnd_toStartOf="#+id/divideBy"
app:layout_constraintStart_toEndOf="#+id/num6"
app:layout_constraintTop_toBottomOf="#+id/plus" />
<Button
android:id="#+id/plus"
android:layout_width="74dp"
android:layout_height="76dp"
android:text="+"
app:layout_constraintBottom_toTopOf="#+id/exponential"
app:layout_constraintEnd_toStartOf="#+id/minus"
app:layout_constraintStart_toEndOf="#+id/num3"
app:layout_constraintTop_toBottomOf="#+id/delete" />
<Button
android:id="#+id/minus"
android:layout_width="74dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="-"
app:layout_constraintBottom_toTopOf="#+id/divideBy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/plus"
app:layout_constraintTop_toBottomOf="#+id/delete" />
<Button
android:id="#+id/num0"
android:layout_width="252dp"
android:layout_height="76dp"
android:layout_marginStart="3dp"
android:layout_marginLeft="3dp"
android:layout_marginBottom="5dp"
android:text="0"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/equalsTo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num8" />
<Button
android:id="#+id/num5"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginBottom="2dp"
android:text="5"
app:layout_constraintBottom_toTopOf="#+id/num8"
app:layout_constraintEnd_toStartOf="#+id/num6"
app:layout_constraintStart_toEndOf="#+id/num4"
app:layout_constraintTop_toBottomOf="#+id/num2" />
<Button
android:id="#+id/num4"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginBottom="1dp"
android:text="4"
app:layout_constraintBottom_toTopOf="#+id/num7"
app:layout_constraintEnd_toStartOf="#+id/num5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num1" />
<Button
android:id="#+id/num6"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="2dp"
android:text="6"
app:layout_constraintBottom_toTopOf="#+id/num9"
app:layout_constraintEnd_toStartOf="#+id/exponential"
app:layout_constraintStart_toEndOf="#+id/num5"
app:layout_constraintTop_toBottomOf="#+id/num3" />
<Button
android:id="#+id/num8"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginTop="1dp"
android:text="8"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/num9"
app:layout_constraintStart_toEndOf="#+id/num7"
app:layout_constraintTop_toBottomOf="#+id/num5" />
<Button
android:id="#+id/num7"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp"
android:text="7"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/num8"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/num4" />
<Button
android:id="#+id/num9"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:text="9"
app:layout_constraintBottom_toTopOf="#+id/num0"
app:layout_constraintEnd_toStartOf="#+id/openBrace"
app:layout_constraintStart_toEndOf="#+id/num8"
app:layout_constraintTop_toBottomOf="#+id/num6" />
<Button
android:id="#+id/num3"
android:layout_width="84dp"
android:layout_height="76dp"
android:text="3"
app:layout_constraintBottom_toTopOf="#+id/num6"
app:layout_constraintEnd_toStartOf="#+id/plus"
app:layout_constraintStart_toEndOf="#+id/num2"
app:layout_constraintTop_toBottomOf="#+id/clear" />
<Button
android:id="#+id/num2"
android:layout_width="84dp"
android:layout_height="76dp"
android:text="2"
app:layout_constraintBottom_toTopOf="#+id/num5"
app:layout_constraintEnd_toStartOf="#+id/num3"
app:layout_constraintStart_toEndOf="#+id/num1"
app:layout_constraintTop_toBottomOf="#+id/clear" />
<Button
android:id="#+id/equalsTo"
android:layout_width="150dp"
android:layout_height="76dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="5dp"
android:text="="
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/num0"
app:layout_constraintTop_toBottomOf="#+id/num9" />
<Button
android:id="#+id/clear"
android:layout_width="252dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="8dp"
android:text="Clear"
app:layout_constraintEnd_toStartOf="#+id/delete"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/expression" />
<TextView
android:id="#+id/expression"
android:layout_width="400dp"
android:layout_height="331dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="6dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:layout_marginBottom="8dp"
android:text=""
app:layout_constraintBottom_toTopOf="#+id/clear"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/delete"
android:layout_width="148dp"
android:layout_height="76dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="6dp"
android:layout_marginRight="6dp"
android:text="Delete"
app:layout_constraintBottom_toTopOf="#+id/plus"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/clear"
app:layout_constraintTop_toBottomOf="#+id/expression" />
<Button
android:id="#+id/num1"
android:layout_width="84dp"
android:layout_height="76dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:text="1"
app:layout_constraintBottom_toTopOf="#+id/num4"
app:layout_constraintEnd_toStartOf="#+id/num2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/clear" />
</android.support.constraint.ConstraintLayout>
please suggest how to solve this problem. The
you can't do final Button num1 = findViewById(R.id.num1); outside of oncreate, all your findViewById(R.id.foo); statements have to be inside onCreate (or at least after onCreate has been called)
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById<Button>(R.id.foo)
}
My problem is that when i try to take data from the edittext fields(uidreg, passwordreg) it fails.
and this if(!uid.equals("") || !password.equals("")) becomes false and prints the toast Toast.makeText(getApplicationContext(),"All fields are required",Toast.LENGTH_LONG).show();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg);
Button studentbtn = (Button)findViewById(R.id.studentbtn);
EditText textuidreg = (EditText)findViewById(R.id.uidreg);
EditText textpasswordreg = (EditText)findViewById(R.id.passwordreg);
String uid = textuidreg.getText().toString();
String password = textpasswordreg.getText().toString();
Toast.makeText(getApplicationContext(),uid,Toast.LENGTH_LONG).show();
studentbtn.setMovementMethod(LinkMovementMethod.getInstance());
studentbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!uid.equals("") || !password.equals("")) {
}
else {
Toast.makeText(getApplicationContext(),"All fields are required",Toast.LENGTH_LONG).show();
}
}
});
teacherbtn.setMovementMethod(LinkMovementMethod.getInstance());
teacherbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/uidreg"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:drawableRight="#drawable/ic_uid"
android:ems="10"
android:hint="uid"
android:importantForAutofill="no"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.482"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.327" />
<EditText
android:id="#+id/passwordreg"
android:layout_width="214dp"
android:layout_height="40dp"
android:drawableRight="#drawable/ic_password"
android:ems="20"
android:hint="password"
android:importantForAutofill="no"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.492"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.528" />
</androidx.constraintlayout.widget.ConstraintLayout>
You need to put
String uid = textuidreg.getText().toString();
String password = textpasswordreg.getText().toString();
inside onClick function
full code should be like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reg);
Button studentbtn = (Button)findViewById(R.id.studentbtn);
EditText textuidreg = (EditText)findViewById(R.id.uidreg);
EditText textpasswordreg = (EditText)findViewById(R.id.passwordreg);
Toast.makeText(getApplicationContext(),uid,Toast.LENGTH_LONG).show();
studentbtn.setMovementMethod(LinkMovementMethod.getInstance());
studentbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String uid = textuidreg.getText().toString();
String password = textpasswordreg.getText().toString();
if(!uid.equals("") || !password.equals("")) {
}
else {
Toast.makeText(getApplicationContext(),"All fields are required",Toast.LENGTH_LONG).show();
}
}
});
teacherbtn.setMovementMethod(LinkMovementMethod.getInstance());
teacherbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
}
My EditText are being covered by my keyboard when trying to input into it.
Here's a screenshot without the Keyboard
Here's a screenshot with the Keyboard
When it comes to the code, I tried everything I could find here I think it comes from the fact that I use a custom animations. Here's the XML part of the code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
android:paddingTop="25dp">
<!--French Flag !-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:orientation="vertical"
android:id="#+id/linearLayout">
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:background="#color/flagBlue">
</View>
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:background="#color/flagWhite">
</View>
<View
android:layout_width="match_parent"
android:layout_height="12dp"
android:background="#color/flagRed">
</View>
</LinearLayout>
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/logo"
android:src="#drawable/logo_text"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<com.facebook.login.widget.LoginButton
android:id="#+id/button_facebook_sign_in"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_marginTop="100dp"
android:layout_below="#+id/logo"
android:layout_centerHorizontal="true" />
<com.petitchef.petitchef.views.customviews.CustomButton
android:id="#+id/button_sign_in"
android:layout_marginTop="20dp"
android:layout_width="180dp"
android:layout_height="35dp"
android:text="#string/sign_in_petitchef"
android:textColor="#color/flagWhite"
android:background="#drawable/petitchef_background_button"
android:layout_below="#+id/button_facebook_sign_in"
android:textSize="15sp"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="75dp"
android:src="#drawable/fork_and_knife"
android:layout_centerHorizontal="true" />
<include layout="#layout/signup_form"
android:id="#+id/sign_up_form"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="800dp"/>
<include layout="#layout/signin_form"
android:id="#+id/sign_in_form"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="800dp"/>
</RelativeLayout>
</RelativeLayout>
And the Java part
public class SignUpActivity extends AppCompatActivity {
private static final String TAG = "SignUpActivity";
View sliderView;
TextView switchToSignIn;
TextView switchToSignUp;
CallbackManager callbackManager;
EditText usernameSignIn;
EditText passwordSignIn;
EditText usernameSignUp;
EditText mailSignUp;
EditText passwordSignUp;
Button buttonSignIn;
Button buttonFacebook;
Button buttonConfirmSignIn;
Button buttonConfirmSignUp;
LoginButton loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_login_signup);
//Testing if user already logged in
callbackManager = CallbackManager.Factory.create();
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
if (sharedPref.contains(this.getString(R.string.token_shared_string))) {
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
sliderView = findViewById(R.id.slider_layout);
loginButton = (LoginButton) findViewById(R.id.button_facebook_sign_in);
loginButton.setReadPermissions("email");
buttonSignIn = (Button) findViewById(R.id.button_sign_in);
buttonSignIn.setTransformationMethod(null);
buttonConfirmSignIn = (Button) findViewById(R.id.button_confirm_sign_in);
buttonConfirmSignIn.setTransformationMethod(null);
buttonConfirmSignUp = (Button) findViewById(R.id.button_confirm_sign_up);
buttonConfirmSignUp.setTransformationMethod(null);
switchToSignIn = (TextView) findViewById(R.id.switch_to_login_text);
switchToSignIn.setText(Html.fromHtml(getString(R.string.signup_switch_to_signin)));
switchToSignUp = (TextView) findViewById(R.id.switch_to_signup_text);
switchToSignUp.setText(Html.fromHtml(getString(R.string.signin_switch_to_signup)));
switchToSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.sign_up_form).setVisibility(View.INVISIBLE);
findViewById(R.id.sign_in_form).setVisibility(View.VISIBLE);
}
});
switchToSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
findViewById(R.id.sign_up_form).setVisibility(View.VISIBLE);
findViewById(R.id.sign_in_form).setVisibility(View.INVISIBLE);
}
});
usernameSignIn = (EditText) findViewById(R.id.username_sign_in);
passwordSignIn = (EditText) findViewById(R.id.password_sign_in);
usernameSignUp = (EditText) findViewById(R.id.username_sign_up);
mailSignUp = (EditText) findViewById(R.id.mail_address_sign_up);
passwordSignUp = (EditText) findViewById(R.id.password_sign_up);
buttonSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Resources resources = SignUpActivity.this.getResources();
DisplayMetrics displayMetrics = resources.getDisplayMetrics();
sliderView.animate()
.translationY(-0.8f * displayMetrics.heightPixels / 2)
.setDuration(600)
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
findViewById(R.id.sign_up_form).setVisibility(View.INVISIBLE);
findViewById(R.id.sign_in_form).setVisibility(View.VISIBLE);
buttonSignIn.animate().alpha(0.0f)
.setDuration(300);
buttonSignIn.setEnabled(false);
loginButton.animate().alpha(0.0f)
.setDuration(300);
loginButton.setEnabled(false);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
});
buttonConfirmSignIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (TextUtils.isEmpty(usernameSignIn.getText()) || TextUtils.isEmpty(passwordSignIn.getText()))
Toast.makeText(SignUpActivity.this, getResources().getString(R.string.error_field_empty), Toast.LENGTH_SHORT).show();
String username = usernameSignIn.getText().toString();
String password = passwordSignIn.getText().toString();
APIManager.getInstance().login(username, password, new APIListener<Boolean>() {
#Override
public void onResult(Boolean hasSignUpSucceeded) {
if (hasSignUpSucceeded) {
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
else {
Toast.makeText(SignUpActivity.this,
getResources().getString(R.string.error_password_or_username),
Toast.LENGTH_LONG)
.show();
}
}
});
}
});
buttonConfirmSignUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (TextUtils.isEmpty(usernameSignUp.getText()) || TextUtils.isEmpty(passwordSignUp.getText()) || TextUtils.isEmpty(mailSignUp.getText()))
Toast.makeText(SignUpActivity.this, getResources().getString(R.string.error_field_empty), Toast.LENGTH_SHORT).show();
String username = usernameSignUp.getText().toString();
String password = passwordSignUp.getText().toString();
String mail = mailSignUp.getText().toString();
APIManager.getInstance().register(username, password, mail, new APIListener<Boolean>() {
#Override
public void onResult(Boolean hasSignUpSucceeded) {
if (hasSignUpSucceeded) {
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
else {
Toast.makeText(SignUpActivity.this,
getResources().getString(R.string.error_password_or_username),
Toast.LENGTH_LONG)
.show();
}
}
});
}
});
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
#Override
public void onBackPressed() {
usernameSignIn.setText("");
usernameSignIn.clearFocus();
passwordSignIn.setText("");
passwordSignIn.clearFocus();
usernameSignUp.setText("");
usernameSignUp.clearFocus();
mailSignUp.setText("");
mailSignUp.clearFocus();
passwordSignUp.setText("");
passwordSignUp.clearFocus();
sliderView.animate()
.translationY(0)
.setDuration(600)
.setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
buttonSignIn.animate().alpha(1.0f)
.setDuration(300);
buttonSignIn.setEnabled(true);
loginButton.animate().alpha(1.0f)
.setDuration(300);
loginButton.setEnabled(true);
}
#Override
public void onAnimationEnd(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
I tried android:windowSoftInputMode="stateAlwaysHidden|adjustResize", I treid to put everything in a ScrollView (It just breaks the whole view)
I have this error in one activity a bout out of memory :
Process: com.moments.hamza.babytalk, PID: 16463
java.lang.OutOfMemoryError: Failed to allocate a 18485292 byte allocation with 15230424 free bytes and 14MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:747)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:568)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1014)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:3747)
at android.content.res.Resources.loadDrawable(Resources.java:3620)
at android.content.res.TypedArray.getDrawable(TypedArray.java:762)
at android.widget.ImageView.(ImageView.java:151)
at android.widget.ImageView.(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:57)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:53)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:102)
at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:972)
at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:1030)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:276)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:136)
at com.moments.hamza.babytalk.Level_2.onCreate(Level_2.java:49)
at android.app.Activity.performCreate(Activity.java:6283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
the xml layout file :
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.moments.hamza.babytalk.Level_2"
android:padding="16dp">
<ViewFlipper
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/v_f_level2"
android:layout_marginBottom="50dp"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_aid"
android:src="#drawable/letter_a"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_bid"
android:src="#drawable/letter_b"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_cid"
android:src="#drawable/letter_c"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_did"
android:src="#drawable/letter_d"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_eid"
android:src="#drawable/letter_e"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_fid"
android:src="#drawable/letter_f"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_gid"
android:src="#drawable/letter_g"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_hid"
android:src="#drawable/letter_h"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_iid"
android:src="#drawable/letter_i"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_jid"
android:src="#drawable/letter_j"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_kid"
android:src="#drawable/letter_k"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_lid"
android:src="#drawable/letter_l"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_mid"
android:src="#drawable/letter_m"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_nid"
android:src="#drawable/letter_n"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_oid"
android:src="#drawable/letter_o"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_pid"
android:src="#drawable/letter_p"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_qid"
android:src="#drawable/letter_q"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_rid"
android:src="#drawable/letter_r"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_sid"
android:src="#drawable/letter_s"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_tid"
android:src="#drawable/letter_t"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_uid"
android:src="#drawable/letter_u"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_vid"
android:src="#drawable/letter_v"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_wid"
android:src="#drawable/letter_w"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_xid"
android:src="#drawable/letter_x"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_yid"
android:src="#drawable/letter_y"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/letter_zid"
android:src="#drawable/letter_z"
android:scaleType="centerCrop"
android:contentDescription="#string/a_letter" />
</ViewFlipper>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/next_level_2"
android:src="#drawable/next"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/back_level_2"
android:src="#drawable/back"
android:layout_alignTop="#+id/next_level_2"
android:layout_alignParentStart="true" />
</RelativeLayout>
the main code for level_2 is :
public class Level_2 extends AppCompatActivity {
private ImageView back_level_2;
private ImageView next_level_2;
private ImageView letteraj;
private ImageView letterbj;
private ImageView lettercj;
private ImageView letterdj;
private ImageView letterej;
private ImageView letterfj;
private ImageView lettergj;
private ImageView letterhj;
private ImageView letterij;
private ImageView letterjj;
private ImageView letterkj;
private ImageView letterlj;
private ImageView lettermj;
private ImageView letternj;
private ImageView letteroj;
private ImageView letterpj;
private ImageView letterqj;
private ImageView letterrj;
private ImageView lettersj;
private ImageView lettertj;
private ImageView letteruj;
private ImageView lettervj;
private ImageView letterwj;
private ImageView letterxj;
private ImageView letteryj;
private ImageView letterzj;
private ViewFlipper v_f_level2j;
private TextToSpeech t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_2);
v_f_level2j = (ViewFlipper) findViewById(R.id.v_f_level2);
back_level_2 = (ImageView) findViewById(R.id.next_level_2);
next_level_2 = (ImageView) findViewById(R.id.back_level_2);
letteraj=(ImageView) findViewById(R.id.letter_aid);
letterbj=(ImageView) findViewById(R.id.letter_bid);
lettercj=(ImageView) findViewById(R.id.letter_cid);
letterdj=(ImageView) findViewById(R.id.letter_did);
letterej=(ImageView) findViewById(R.id.letter_eid);
letterfj=(ImageView) findViewById(R.id.letter_fid);
lettergj=(ImageView) findViewById(R.id.letter_gid);
letterhj=(ImageView) findViewById(R.id.letter_hid);
letterij=(ImageView) findViewById(R.id.letter_iid);
letterjj=(ImageView) findViewById(R.id.letter_jid);
letterkj=(ImageView) findViewById(R.id.letter_kid);
letterlj=(ImageView) findViewById(R.id.letter_lid);
lettermj=(ImageView) findViewById(R.id.letter_mid);
letternj=(ImageView) findViewById(R.id.letter_nid);
letteroj=(ImageView) findViewById(R.id.letter_oid);
letterpj=(ImageView) findViewById(R.id.letter_pid);
letterqj=(ImageView) findViewById(R.id.letter_qid);
letterrj=(ImageView) findViewById(R.id.letter_rid);
lettersj=(ImageView) findViewById(R.id.letter_sid);
lettertj=(ImageView) findViewById(R.id.letter_tid);
letteruj=(ImageView) findViewById(R.id.letter_uid);
lettervj=(ImageView) findViewById(R.id.letter_vid);
letterwj=(ImageView) findViewById(R.id.letter_wid);
letterxj=(ImageView) findViewById(R.id.letter_xid);
letteryj=(ImageView) findViewById(R.id.letter_yid);
letterzj=(ImageView) findViewById(R.id.letter_zid);
t1=new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.UK);
}
}
});
letteraj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="A";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterbj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="B";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
lettercj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="C";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterdj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="D";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterej.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="E";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterfj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="F";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
lettergj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="G";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterhj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="H";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterij.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="I";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterjj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="J";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterkj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="K";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterlj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="L";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
lettermj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="M";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letternj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="N";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letteroj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="O";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterpj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="P";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterqj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="Q";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterrj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="R";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
lettersj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="S";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
lettertj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="T";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letteruj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="U";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
lettervj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="V";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterwj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="W";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak, TextToSpeech.QUEUE_ADD, null);}});
letterxj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="X";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letteryj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="Y";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
letterzj.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak ="Z";
t1.setSpeechRate((float) 1);
t1.speak(toSpeak,TextToSpeech.QUEUE_ADD, null);}});
next_level_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v_f_level2j.showNext();
}
});
back_level_2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v_f_level2j.showPrevious();
}
});
}
}
can you please help me ?
One or more of your ImageView widgets is pointing to a resource that is approximately 2150 x 2150 pixels. This is much too large. Use smaller images.
the largest size is : 28KB
That is the size on disk, which is not that useful.
the largest dimension is 660X778
Then my guess is that you put the images in res/drawable/, and you are testing on a device or emulator with -xxhdpi density. res/drawable/ is a synonym for res/drawable-mdpi/, and the drawables there are for medium-density screens. When you load them on an -xxhdpi density environment, those images will be scaled by a factor of 3 on each axis, to keep the image the same physical size as it would be on an -mdpi device.