whats wrong with the android code ? I want this code to give Incorrect when the
entered answer is wrong and correct when the answer is correct but every time
get is incorrect
and are my casting variables correct
public class MainActivity extends Activity {
TextView Jlabel1;
TextView Jlabel2;
TextView Jlabel3;
EditText Jtextbox1;
Button b1;
int m;
int ans;
String ans1;
String k;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Jlabel1 = (TextView) findViewById(R.id.textView1);
Jlabel2 = (TextView) findViewById(R.id.textView2);
Jlabel3 = (TextView) findViewById(R.id.textView3);
Jtextbox1 = (EditText) findViewById(R.id.editText1);
b1 = (Button) findViewById(R.id.button1);
double q = Math.random();
double w = Math.random();
int e = (int) (q * 10);
int z = (int) (w * 10);
ans = e + z;
Jlabel1.setText(Integer.toString(e));
Jlabel2.setText(Integer.toString(z));
ans1 = String.valueOf(ans);
k = Jtextbox1.getText().toString();
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (ans1.equals(k)) {
Jlabel3.setText("Correct");
} else {
Jlabel3.setText("Incorrect");
}
}
});
}
}'
You're getting a copy of the contents of the edittext too early. Move the
k = Jtextbox1.getText().toString();
inside the onClick().
Related
I am facing difficulties in showing data in textView after going to the next page
I am storing the return value in EMI variable but I am not able to print that value in the next page textview.
public class EmiCalculator extends AppCompatActivity {
public static double emical(double p,double r, double t)
{
double emi;
r = r / (12 * 100); // one month interest
t = t * 12; // one month period
emi = (p * r * (double)Math.pow(1 + r, t)) / (double)(Math.pow(1 + r, t) - 1);
return (emi);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emi_calculator);
EditText getText_1, getText_2, getText_3;
getText_1 = (EditText) findViewById(R.id.emi_editText_1);
getText_2 = (EditText) findViewById(R.id.emi_editText_2);
getText_3 = (EditText) findViewById(R.id.emi_editText_3);
Button calculateButton = (Button) findViewById(R.id.emi_calculate);
calculateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
double emi_edit_Text_01 = Double.parseDouble(getText_1.getText().toString());
double emi_edit_Text_02 = Double.parseDouble(getText_2.getText().toString());
double emi_edit_Text_03 = Double.parseDouble(getText_3.getText().toString());
double emi = emical(emi_edit_Text_01, emi_edit_Text_02, emi_edit_Text_03);
String str = String.valueOf(emi);
TextView result = (TextView) findViewById(R.id.result_textView_3);
result.setText(""+emi);
Intent nextPage = new Intent(EmiCalculator.this,Result.class);
startActivity(nextPage);
}
});
}
}
you must use
Intent.putExtra
for send data to another activity.
in first activity:
Intent nextPage = new Intent(EmiCalculator.this,Result.class);
nextPage.putExtra("result",""+emi);
startActivity(nextPage);
in second activity:
Intent intentResult=this.getIntent;
if(intentResult.hasExtra("result")){
textview.setText(intent.getStringExtra("result"));
}
If there is any way to change program when the "a" is below 0
I mean when the A is larger than 0 that the program will be like this and when below 0 than the program will be like B-A
private Button Button;
private EditText Num1;
private EditText Num2;
private EditText Num3;
private EditText Num4;
private EditText Num5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zisk);
Button = findViewById(R.id.btnBtn);
Num1 = findViewById(R.id.etNum1);
Num2 = findViewById(R.id.etNum2);
Num3 = findViewById(R.id.etNum3);
Num4 = findViewById(R.id.etNum4);
Num5 = findViewById(R.id.etNum5);
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double a,b,c,d,e;
a = Double.parseDouble(Num1.getText() . toString());
b = Double.parseDouble(Num2.getText() . toString());
d = b-a;
e = d*0.80;
c = e*0.79;
Num3.setText(Double.toString(c));
Num4.setText(Double.toString(d));
Num5.setText(Double.toString(e));
}
});
}
I am working on Indoor positioning system with wifi fingerprinting in which I want to store level of specifics BSSID in arrryList I tried this code but it seems not to work
public class calibrate2 extends AppCompatActivity {
WifiManager wifimanager2;
Button cali;
Button reset;
//EditText Tx;
//EditText Ty;
TextView textView;
TextView textView2;
List<ScanResult> list;
Integer j,x,i=0;
ArrayList<String> Ap=new ArrayList<>();;
ArrayList<Integer> si=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calibrate2);
Ap = getIntent().getStringArrayListExtra("ap");
cali = (Button) findViewById(R.id.button);
reset = (Button) findViewById(R.id.button2);
textView= (TextView)findViewById(R.id.textView);
textView2= (TextView)findViewById(R.id.textView2);
// Tx = (EditText) findViewById(R.id.editText);
// Ty = (EditText) findViewById(R.id.editText2);
wifimanager2 = (WifiManager) getSystemService(Context.WIFI_SERVICE);
cali.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
wifimanager2.startScan();
list = wifimanager2.getScanResults();
for (j = 0; j < Ap.size(); j++) {
for (i = 0; i < list.size(); i++) {
if (list.get(i).BSSID == Ap.get(j)) {
si.add(list.get(i).level);
break;
}
}
for (j = 0; j < Ap.size(); j++) {
textView2.append(si.get(j).toString());
textView2.append(" /n");
}
}
}
}
);
}}
I guess the problem is in the for loop because activity is working perfectly but whenever I click button it goes to parent activity
I got the answer now with this changes it is working
` for (i = 0; i < list.size(); i++) {
if (Ap.get(j).contains(list.get(i).BSSID) {
si.add(list.get(i).level);
break;}}
for (j = 0; j <si.size(); j++) {
//change Ap.size()to si.size()
textView2.append(si.get(j).toString());
textView2.append(" /n");}
I have 2 textviews, 1 editbox and 1 button in my xml.
In my code, this line is not working:
if(c.equals(str)){
Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
}
But next line (else statement) is working and In both cases (if & else statement) it shows else function toast ("Wrong").
Here is my code:
public class GameActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView tv = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
Random r = new Random();
int i = r.nextInt(10 - 2) + 2;
tv.setText(i +"");
Random r1 = new Random();
int j = r1.nextInt((9 - 2) + 1) + 2;
tv2.setText(j +"");
int a = Integer.parseInt(tv.getText().toString());
int b = Integer.parseInt(tv2.getText().toString());
int result = a*b;
String str = String.valueOf(result);
EditText txt4 = (EditText) findViewById(R.id.editText1);
String c = txt4.getText().toString();
if(TextUtils.isEmpty(txt4.getText().toString())) {
txt4.setError("Please enter your answer");
return;
}
if(c.equals(str)){
Toast.makeText(GameActivity.this, "Alright",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(GameActivity.this, "wrong",Toast.LENGTH_LONG).show();
}
txt4.setText("");
}
});
}
Try this,
I got result
public class Test extends AppCompatActivity {
Button b1;
TextView t1,t2;
EditText e1;
int a,b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
b1 = (Button) findViewById(R.id.button2);
t1=(TextView) findViewById(R.id.textView4);
t2=(TextView) findViewById(R.id.textView5);
e1=(EditText) findViewById(R.id.editText);
Random r = new Random();
int i = r.nextInt(10 - 2) + 2;
t1.setText(i +"");
Random r1 = new Random();
int j = r1.nextInt((9 - 2) + 1) + 2;
t2.setText(j +"");
a = Integer.parseInt(t1.getText().toString());
b = Integer.parseInt(t2.getText().toString());
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int result = a*b;
String str = String.valueOf(result);
String c = e1.getText().toString();
if(TextUtils.isEmpty(c)) {
e1.setError("Please enter your answer");
return;
}
if(c.equals(str)){
Toast.makeText(Test.this, "Alright",Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Test.this, "wrong",Toast.LENGTH_LONG).show();
}
e1.setText("");
}
});
}}
Instead of
String str = String.valueOf(result);
you should use
String str = Integer.toString(result);
I am trying to display a double from this class in another class..
So here is my code:
public class Calculator extends AppCompatActivity {
Button next;
TextView pPrice;
TextView renovations;
TextView misc2;
TextView util;
TextView rep;
TextView mortage;
TextView misc1;
TextView rent;
public double getStartingCostsResult() {
return startingCostsResult;
}
double startingCostsResult;
double monthlyMinus;
double monthlyPlus;
double monthlyROI;
double yearlyROI;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
// Setting these textviews to those in the xml.
pPrice = (TextView) findViewById(R.id.pPrice);
renovations = (TextView) findViewById(R.id.renovations);
misc2 = (TextView) findViewById(R.id.misc2);
util = (TextView) findViewById(R.id.util);
rep = (TextView) findViewById(R.id.rep);
mortage = (TextView) findViewById(R.id.mortage);
misc1 = (TextView) findViewById(R.id.misc);
rent = (TextView) findViewById(R.id.rent);
next = (Button) findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent expense = new Intent(getApplicationContext(), Results.class);
if ((pPrice.getText().length() > 0) && (renovations.getText().length() > 0) && (misc2.getText().length() > 0)) {
double price = Double.parseDouble(pPrice.getText().toString());
// double costs = Double.parseDouble(cCosts.getText().toString());
double reno = Double.parseDouble(renovations.getText().toString());
double misc = Double.parseDouble(misc2.getText().toString());
startingCostsResult = price + reno + misc;
if((util.getText().length()>0) && (rep.getText().length()>0) && (mortage.getText().length()>0) && (misc1.getText().length()>0)){
double utilities = Double.parseDouble(util.getText().toString());
double repairs = Double.parseDouble(rep.getText().toString());
double mort = Double.parseDouble(mortage.getText().toString());
double miscsell = Double.parseDouble(misc1.getText().toString());
monthlyMinus = utilities + repairs + mort + miscsell;
if (rent.getText().length()>0){
double monthlyRent = Double.parseDouble(rent.getText().toString());
monthlyPlus = monthlyRent;
monthlyROI = monthlyPlus - monthlyMinus;
yearlyROI = monthlyROI *12;
startActivity(expense);
}else{
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(Calculator.this, "Please enter '0' in all boxes that don't apply.", Toast.LENGTH_SHORT).show();
}
}
});
}
}
So I am trying to display the yearlyROI double in another class.
I have tried this:
Calculator calc = new Calculator();
otherClass.setText((int) calc.yearlyROI);
But my app crashes when I click next.
you should put an extra in the expense intent like this.
expense.putExtra("yearlyRoi",yearlyRoi);
then in the nexet activity you can get it like this.
Intent recievedIntent = this.getIntent();
double yearlyRoi = recievedIntent.getDoubleExtra("yearlyRoi", defaultValue);
default value can be 0.0 or anything you want.
as for the crash i think its another problem,you need to give us error log of your app.
If you want to access variables from a different Activity you need to add them to your intent.
In your case:
expense.putExtra("yearlyROI", yearlyROI);
startActivity(expense);
Then in your new Activity:
double yearlyROI = getIntent().getDoubleExtra("yearlyROI");
Hope it helps!