I have little application where you can tell your fortune (it's actualy just random), there are four edittextfields that you can type in and one button that choose one of the edittextfields, and remove it. Pretty simple. But i have problem, if someone presses the button multiple times too fast then all of the code in the onClick() method doesn't execute (probably because it is called again). Is there some way that I can prevent this from happening (I want all of the code in the onClick() method to execute before it can be called again)?
Here is the code:
package com.foretell.lukas.spamedprick;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
ArrayList<EditText> tfa = new ArrayList<EditText>();
int x = 0;
boolean tf = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// References to XML widgets
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.background);
final Button daButton = (Button) findViewById(R.id.button);
Button restartButton = (Button) findViewById(R.id.restartButton);
final EditText ruta1 = (EditText) findViewById(R.id.editText);
final EditText ruta2 = (EditText) findViewById(R.id.editText2);
final EditText ruta3 = (EditText) findViewById(R.id.editText3);
final EditText ruta4 = (EditText) findViewById(R.id.editText4);
final TextView outputText = (TextView) findViewById(R.id.outputText);
tfa.add(ruta1);
tfa.add(ruta2);
tfa.add(ruta3);
tfa.add(ruta4);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.spar);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.hurra);
daButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if(tf) {
daButton.setEnabled(false);
mp.start();
try {
tf = false;
Thread.sleep(2000);
} catch (InterruptedException e) {
e.getStackTrace();
outputText.setText("Nått gick åt h***ete!");
}
Collections.shuffle(tfa);
x++;
if (x <= 2) {
outputText.setText("Det är inte " + tfa.get(0).getText() + "...");
tfa.get(0).setText("");
tfa.remove(0);
} else if (x == 3) {
outputText.setText("Det är...");
} else if (x == 4) {
tfa.get(1).setText("");
tfa.remove(1);
outputText.setText("Det är " + tfa.get(0).getText() + "!");
mp2.start();
}
tf = true;
daButton.setEnabled(true);
}else{
System.out.println("YO!");
}
}
}
);
restartButton.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
tfa.add(ruta1);
tfa.add(ruta2);
tfa.add(ruta3);
tfa.add(ruta4);
ruta1.requestFocus();
outputText.setText("");
x = 0;
ruta1.setText("");
ruta2.setText("");
ruta3.setText("");
ruta4.setText("");
}
}
);
}
}
Thanks.
I dont think that an "onClick" can be started before the last "onClick" is finished.
Here is the qoute from the android documentation:
The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.
http://developer.android.com/guide/components/processes-and-threads.html
Maybe you do something in the onClick method, that makes trouble, but without the code it is hard to tell:)
You have to do something like this
Button button= (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setEnabled(false);
// do your work here
// make it true after your work is done
button.setEnabled(true);
}
});
Related
The android studio code works as intended. However, points are made in the background since it's an idle game. An on click method only shows the points that were made as of the last click. I need the points to be shown at all times.
I have been testing this new added bit of code but the points are not updating when the button is clicked. Example in photo. Evolution Points = 0; The on click method is updating but there are also points made in the background that need to be shown without a button click. Any suggestions?
visibletotals.setText("Evolutions Points: " + clicks);
full code for reference
package com.example.idleclicker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
TextView points;
TextView visibletotals;
Button click;
Button upgradebtn;
TextView Leveltext;
int clicks = 0;
int clickcost = 10;
int upgradelevel = 1;
Timer myTimer = new Timer();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = (Button) findViewById(R.id.click);
points = (TextView) findViewById(R.id.points);
upgradebtn = (Button) findViewById(R.id.upgradebtn);
Leveltext = (TextView) findViewById(R.id.leveltext);
visibletotals = (TextView) findViewById(R.id.visibletotals);
visibletotals.setText("Evolutions Points: " + clicks);
click.setEnabled(true);
upgradebtn.setEnabled(true);
myTimer.schedule(new TimerTask() {
#Override
public void run() {
clicks+= upgradelevel;
}
}, 0, 1000);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
clicks++;
points.setText(getResources().getString(R.string.evol) + clicks);
}
});
upgradebtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (clicks >= clickcost) {
clicks -= clickcost;
upgradelevel += 1;
clickcost *= 2;
upgradebtn.setText(getResources().getString(R.string.Upgrade) +
clickcost +
getString(R.string.LevelText)
+ upgradelevel);
};
}
});
}}
I can see that your clicks is changing(dynamic) while your set text is static that means it is only replying at the start and it is not triggered from the next time you do it. one way to tackle this is put the settext value in the trigger block(i.e onclicklistener) and make it set the fresh text as per triggered.
I'm relatively new to app development and very confused about what to do here. Please be very clear and very detailed in your responses, much appreciated!
I'm getting a host of errors from the following code block:
//Blank out the appropriate blanks
chkRect.setOnClickListener(new onClickListener() {
#Override
public void onClick(View v) {
if (chkRect.isChecked()){
chkCyl.setEnabled(false);
txtDiameter.setText("0");
txtDiameter.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtDiameter.setText("");
txtDiameter.setEnabled(true);
}
}});
chkCyl.setOnClickListener(new onClickListener(){
#Override
public void onClick(View v) {
if (chkCyl.isChecked()){
chkRect.setEnabled(false);
txtHeight.setText("0");
txtWidth.setText("0");
txtHeight.setEnabled(false);
txtWidth.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtHeight.setText("");
txtWidth.setText("");
txtHeight.setEnabled(true);
txtWidth.setEnabled(true);
}
}});
On both "onClickListener" lines and the 'public void' lines, the following shows up:
-onClickListener cannot be resolved as a type. (multiple lines)
-Method setOnClickListener(View.onClickListener) in the type View is not applicable for the arguments new onClickListener.
-The Method "onClick(View v)" must override or implement a supertype method.
Here are my imports and variable declarations:
import java.text.DecimalFormat;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
public class MaterialCalculator extends ActionBarActivity implements View.OnClickListener{
public Spinner materialDD;
public CheckBox chkRect;
public CheckBox chkCyl;
//public Spinner shapeDD;
DecimalFormat d = new DecimalFormat("#");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_material_calculator);
materialDD = (Spinner) findViewById(R.id.materialDD);
final CheckBox chkRect = (CheckBox) findViewById(R.id.chkRect);
final CheckBox chkCyl = (CheckBox) findViewById(R.id.chkCyl);
//shapeDD = (Spinner) findViewById(R.id.shapeDD);
Button btnCalculate = (Button) findViewById(R.id.btnCalculate);
Button btnClearAll = (Button) findViewById(R.id.btnClearAll);
final EditText txtDiameter = (EditText) findViewById(R.id.txtDiameter);
final EditText txtLength = (EditText) findViewById(R.id.txtLength);
final EditText txtWidth = (EditText) findViewById(R.id.txtWidth);
final EditText txtHeight = (EditText) findViewById(R.id.txtHeight);
final EditText edtTxtTotprice = (EditText) findViewById(R.id.edtTxtTotprice);
final EditText edtTxtWeight = (EditText) findViewById(R.id.edtTxtWeight);
I tried other solutions on this problem, including deleting the imports for view and onClickListener and using the ctrl+shift+O import, but it didn't work. Like I said, please be very specific with your answers since this is my first app and I'm not at all familiar with Eclipse to any great extent.
change
chkCyl.setOnClickListener(new onClickListener(){
with
chkCyl.setOnClickListener(new OnClickListener(){
and
chkRect.setOnClickListener(new onClickListener(){
with
chkRect.setOnClickListener(new OnClickListener(){
setOnClickListener takes as paramter an instance of a class that implements the OnClickListener interface
Maybe it can't get the variable from within the saved instance state? Try to define the variable right before the onClick Listener like this:
final CheckBox chkRect = (CheckBox) findViewById(R.id.chkRect);
chkRect.setOnClickListener(new onClickListener() {
#Override
public void onClick(View v) {
if (chkRect.isChecked()){
chkCyl.setEnabled(false);
txtDiameter.setText("0");
txtDiameter.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtDiameter.setText("");
txtDiameter.setEnabled(true);
}
}});
final CheckBox chkCyl = (CheckBox) findViewById(R.id.chkCyl);
chkCyl.setOnClickListener(new onClickListener(){
#Override`enter code here`
public void onClick(View v) {
if (chkCyl.isChecked()){
chkRect.setEnabled(false);
txtHeight.setText("0");
txtWidth.setText("0");
txtHeight.setEnabled(false);
txtWidth.setEnabled(false);
}
else {
chkRect.setEnabled(true);
txtHeight.setText("");
txtWidth.setText("");
txtHeight.setEnabled(true);
txtWidth.setEnabled(true);
}
}});
I am new in the android programming. I see many ways to do the event handling, but when I try to do it by calling the handler class it give error on handling class name:
package com.example.test;
import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//attach an instance of HandleClick to the Button
findViewById(R.id.button1).setOnClickListener(new HandleClick());
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Button btn = (Button)arg0; //cast view to a button
// get a reference to the TextView
TextView tv = (TextView) findViewById(R.id.textview1);
// update the TextView text
tv.setText("You pressed " + btn.getText());
}
}
}
"HandleClick" error come on this it say class should be abstract type?
I do not understand why it is giving this error can any one help me?
That's the wrong OnClickListener class. You have
import android.content.DialogInterface.OnClickListener;
You need:
import android.view.View.OnClickListener;
For future reference, the error you get is "The type must implement the inherited abstract method...". This is because you need to implement the DialogInterface's onClick, which should have led you to notice that it was the wrong import (since you have onClick(View))
you imported the wrong OnClickListener, you should import the one from android.view.View
Make it simple & use this,
b1 = (Button) findViewById(R.id.button1);
TextView tv = (TextView) findViewById(R.id.textview1);
b1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast msg = Toast.makeText(getBaseContext(),
"You have clicked Button 1", Toast.LENGTH_LONG);
msg.show();
tv.setText("You pressed " + btn.getText());
}
});
So i have this code which solves the quadratic equation in java (android development) and it isnt doing anything!!!! The button when i press it does not give the answer at all... i cant even check if it is doing it correctly.
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class QuadraticEquationSolver extends Activity {
public void main(String[] args){
Button calc = (Button) findViewById(R.id.Calculate);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText X2 = (EditText)findViewById(R.id.X2);
EditText X = (EditText)findViewById(R.id.X);
EditText Num = (EditText)findViewById(R.id.Num);
TextView ans= (TextView) findViewById(R.id.finalans);
double x2 = Integer.parseInt(X2.getText().toString());
double x = Integer.parseInt(X.getText().toString());
double num = Integer.parseInt(Num.getText().toString());
double finalNum = ((x*-1) + (Math.sqrt((x*x)-(4*x*num))))/(2*x2);
ans.setText("answer: " + finalNum);
}
});
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quadratic_equation_solver);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_quadratic_equation_solver, menu);
return true;
}
First of all, welcome to Android development. I would highly recommend as a starting point you read the App Fundamentals and related guides on the SDK documentation site, as they will help you greatly in your new endeavour.
Android does not use a single entry point (i.e. main method) so your code will not be called. You will want to move all that code into onCreate().
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quadratic_equation_solver);
Button calc = (Button) findViewById(R.id.Calculate);
calc.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
EditText X2 = (EditText)findViewById(R.id.X2);
EditText X = (EditText)findViewById(R.id.X);
EditText Num = (EditText)findViewById(R.id.Num);
TextView ans= (TextView) findViewById(R.id.finalans);
double x2 = Integer.parseInt(X2.getText().toString());
double x = Integer.parseInt(X.getText().toString());
double num = Integer.parseInt(Num.getText().toString());
double finalNum = ((x*-1) + (Math.sqrt((x*x)-(4*x*num))))/(2*x2);
ans.setText("answer: " + finalNum);
}
});
}
I don't do any android programming, but I doubt Android will ever call your main method. The content of this main method must probably be in the onCreate method.
onCreate() is your main() equivalent in Android. Your main() function will never be called. The contents of main() should go in onCreate().
I have a MainClass which one is the full app, when I click one button I go to another class (PopupValores) which one I make it looks like a popup. In this class I have a EditText where you type a integer and a button to close this class. My question is how to get that int typed in PopupClass and use it in MainClass. Heres the code for the PopupValores.
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 PopupValores extends Activity implements OnClickListener {
TextView texto;
String mensaje;
EditText editable;
Button ok;
public static int cantidad;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popupvalores);
ok = (Button) findViewById (R.id.Bok);
texto = (TextView) findViewById (R.id.textView1);
editable = (EditText) findViewById (R.id.editText1);
mensaje = editable.getText().toString();
ok.setOnClickListener(this);
ok.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View arg0) {
finish();
return true;
}
});
}
public void onClick(View v){
switch(v.getId()){
case R.id.Bok:
String mensaje;
mensaje = editable.getText().toString();
cantidad = Integer.parseInt(mensaje);
texto.setText("New value " + cantidad + ".");
}
}
}
Then in my MainClass I click a button and it shows the int
int id, vaas = PopupValores.cantidad;
public void onClick (View v)
{
posicion = (ImageCell) v;
seleccion = posicion.mCellNumber;
if (seleccion == -1){
....
toast (id + " " + vaas);
....
}
}
But instead of showing the value declared in PopupValores it shows 0. What I'm doing wrong here?
You need to call the popup Activity with Activity.startActivityForResult()
Once finishing the popup Activity, set the requested result via Activity.setResult() (you can save your data in the intent's bundle)
In your main Activity, override onActivityResult and retrieve the data
Its called startActivityforResult
and has been answered soo many times here on stackoverflow Here is one