There probably is a really simple answer to this. My goal is to divide the the inputed information x and y and put that into z.
Here is my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public int x = Integer.parseInt("");
public int y = Integer.parseInt("");
public int z = (y/x);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Button
Button btn = (Button) findViewById(R.id.button);
//EditText
EditText nop = (EditText) findViewById(R.id.editText);
EditText cob = (EditText) findViewById(R.id.editText2);
x = Integer.parseInt(nop);
y = Integer.parseInt(cob);
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv.setText(z);
}
});
}
}
Declare z inside your onclick function. Or assign it a value inside your function.
Also nop and cob are editText's get the data in it, use getText().
Because Integer.parseInt() accepts only a String but not an editText, so you must use getText() to get the String value in it.
x = Integer.parseInt(nop.getText().toString());
y = Integer.parseInt(cob.getText().toString());
final TextView tv = (TextView) findViewById(R.id.textView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int z = x / y;
tv.setText(z);
}
});
Also z is declared globally and assigned nothing. But it needs to be assigned a value when ever you click the button, so that your editText changes every time you click the button.
Convert first as a string from Edittext then make it as Integer.
String Val1 = nop.getText().toString();
String Val2 =cob.getText().toString();
x = Integer.parseInt(val1);
y = Integer.parseInt(val2);
Related
I'm new to Android. I'm developing an app which gives all the possible combination number based on the user input and separated it by comma.
For example the user input is: 1234
The output should be list all the possible number combination from "1234" like
"1234,1243,1324,1342,1423,1432,2134,etc.."
And there's also another EditText that limit the output by setting it to 1 or 2 or 3 or 4(max).
For example the user input is: 2
The output should be list all the possible number combination from "1234" by "2" digits
"12,13,14,21,23,24,31,32,34,41,42,43"
This is my MainActivity.java
package com.example.androidtest1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
EditText et_input1, et_output1;
Button b_generate, b_clear;
CheckBox checkBox1;
Random r;
int min,output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText mEdit = (EditText) findViewById(R.id.et_output1);
mEdit.setEnabled(false);
r = new Random();
et_input1 = (EditText) findViewById(R.id.et_input1);
et_output1 = (EditText) findViewById(R.id.et_output1);
b_generate = (Button) findViewById(R.id.b_generate);
b_clear = (Button) findViewById(R.id.b_clear);
checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
b_generate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (et_input1.length()==0){
et_input1.setError("Insert numbers");
}else if(checkBox1.isChecked()){
min = Integer.parseInt(et_input1.getText().toString());
output = (min);
et_output1.setText("" + output + "*");
et_input1.setError(null);
}
else{
et_input1.setError("Check the checkbox");
}
}
});
b_clear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
et_output1.getText().clear();
et_input1.getText().clear();
return;
}
});
}
}
My app keeps crashing and gives me a:
java.lang.NumberFormatException: empty String
Also says this
FATAL EXCEPTION: main
Process: com.example.ayyan.jellybeanestimator, PID: 2966
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ayyan.jellybeanestimator/com.example.ayyan.jellybeanestimator.MainActivity}: java.lang.NumberFormatException: empty String
package com.example.ayyan.jellybeanestimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
EditText jellyBeanLength, jellyBeanDiameter, jarSizeVolume;
double jellybeantall, jellybeanfat, jellybeanspace, volumeOfOneJellyBean, volumeofBeans;
final double loadFactor = .698;
TextView answer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jellyBeanLength = (EditText) findViewById (R.id.length);
jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
jarSizeVolume = (EditText) findViewById (R.id.jarsize);
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
answer = (TextView) findViewById (R.id.answer);
Button calculate = (Button) findViewById (R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
int jellyguess = (int) (volumeofBeans);
answer.setText("You have this amount of beans in your jar" + jellyguess);
}
});
}
}
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
All your EditTexts start as empty. You can't getText() in onCreate, you have to do it in onClick
calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
volumeofBeans = (jellybeanspace*loadFactor)/volumeOfOneJellyBean;
int jellyguess = (int) (volumeofBeans);
answer.setText("You have this amount of beans in your jar" + jellyguess);
}
});
Note: when volumeOfOneJellyBean == 0, you'll get a division by zero error
I am trying to work with two buttons on one screen and I want to have two different actions occur when each button is clicked. This is my code and I am getting the error message onClick view is already defined on the method titles of both onClick methods. Any help is greatly appreciated.
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
private Button button3;
private Button button2;
private EditText editText;
private EditText editText9;
private EditText editText10;
private EditText editText11;
private EditText editText12;
private EditText editText13;
private EditText editText14;
private TextView textView;
private TextView textView22;
private View view;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2_main);
Intent intent = getIntent();
button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(this);
button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(this);
editText = (EditText) findViewById(R.id.editText);
editText9 = (EditText) findViewById(R.id.editText9);
editText10 = (EditText) findViewById(R.id.editText10);
editText11 = (EditText) findViewById(R.id.editText11);
editText12 = (EditText) findViewById(R.id.editText12);
editText13 = (EditText) findViewById(R.id.editText13);
editText14 = (EditText) findViewById(R.id.editText14);
textView = (TextView) findViewById(R.id.textView);
textView22 = (TextView) findViewById(R.id.textView22);
}
public void onClick (View view) {
this.view = view;
if (view.getId() == R.id.button3) {
Intent intent = new Intent(MainActivity2.this, MainActivity.class);
MainActivity2.this.startActivity(intent);
}
}
public void onClick (View view){
if (view.getId() == R.id.button2) {
String value8 = editText.getText().toString();
String value9 = editText9.getText().toString();
String value10 = editText10.getText().toString();
String value11 = editText11.getText().toString();
String value12 = editText12.getText().toString();
String value13 = editText13.getText().toString();
String value14 = editText14.getText().toString();
}
}
}
You have two onClick methods with the same parameters so it doesn't know which to use. Combine the two and it should work.
I'm in the very early stages of developing this android app, and I'm trying to setText to buttons from within a method. Doing it not in a method is fine, but I want to call the method over and over. However when I try to create the method to do this, I get a "Non-static method findViewById(int) cannot be referenced from a static context" error.
eg: Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
The findViewById will be red highlighted
package com.example.lp1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button button2;
private Button button3;
private Button button4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int score = 0;
int count = 3;
static int getQuestion(){
ArrayList set = addingThree();
final int a = (int) set.get(0);
final int b = (int) set.get(1);
final int c = (int) set.get(2);
final int d = (int) set.get(3);
final int ans = (int) set.get(4);
String qText = (String) set.get(5);
String[] options = new String[] {Integer.toString(a),
Integer.toString(b),Integer.toString(c),Integer.toString(d)};
Button button = (Button) findViewById(R.id.button);
button.setText(options[0]);
Button button2 = (Button) findViewById(R.id.button2);
button2.setText(options[1]);
Button button3 = (Button) findViewById(R.id.button3);
button3.setText(options[2]);
Button button4 = (Button) findViewById(R.id.button4);
button4.setText(options[3]);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(qText);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
static ArrayList addingThree(){
Random rand = new Random();
int numa = rand.nextInt(99);
int numb = rand.nextInt(99);
int numc = rand.nextInt(99);
int ans = numa + numb + numc;
int fake1 = rand.nextInt(200) + 50;
int fake2 = rand.nextInt(200) +50;
int fake3 = rand.nextInt(200) +50;
int[ ] options = {fake1, fake2, fake3, ans};
String stringQuestion = ("What is the sum of " + numa + ", " + numb + ",
and " + numc + "? ");
Arrays.sort(options);
ArrayList parcel = new ArrayList();
parcel.add(options[0]);
parcel.add(options[1]);
parcel.add(options[2]);
parcel.add(options[3]);
parcel.add(ans);
parcel.add(stringQuestion);
return parcel;
}
}
Remove the
static
from your functions.
Also remove the functions from the onCreate callback, they don't belong there.
When you're invoking the functions in onCreate, the functions/methods cannot be static
If you want to use findViewById() in static method then make your rootview Global variable
I'm using onClick() method by implementing OnClickListener class. Using below code but it does not work! There are no errors but it doesn't work. When I click on the button nothing happens. What is wrong?
package com.behnam.PhoneCh.main;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id. Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
}
You never set your listener on your Buttons
mainBtn.setOnClickListener(this);
You will need to add this for all of the Buttons that you want to use the onClick().
See this answer for different ways of handling onClick()
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2.setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
#Override
public void onClick(View aaaaa) {
switch (aaaaa.getId()) {
case R.id.button1;
Log.i("mylog", "start button1");
vaje = mainEt.getText().toString();
mainTv.setText(javab.startJ(vaje));
break;
case R.id.button2:
int c = 3;
break;
}
}
Change your onCreate and onClick with the above code.
You need to change your code to :
public class MainActivity extends Activity implements OnClickListener {
EditText mainEt;
TextView mainTv;
Button mainBtn;
Button mainBtn2;
String vaje;
Javab javab;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainEt = (EditText) findViewById(R.id.editText1);
mainTv = (TextView) findViewById(R.id.textView1);
mainBtn = (Button) findViewById(R.id.button1);
mainBtn2 = (Button) findViewById(R.id.button2);
mainBtn.setOnClickListener(this);
mainBtn2 .setOnClickListener(this);
vaje = new String();
javab = new Javab();
}
// And then you can use
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Dome some stuff on click of button 1
break;
case R.id.button2:
// Dome some stuff on click of button 2
break;
}
}
}
Try this instead:
Button btMain = (Button)findViewById(R.id.button1);
btMain.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
doStuff();
}
});
This is the best way to start understanding how the listeners work.