Algorithm for my project in android - java

I have a android project that generates random numbers as the button's text. If you click a button the value of the corresponding button should be displayed in the edittext.
I am already getting the value of the buttons and also able to display it in the edittext. I have 12 buttons and 2 edittexts. What I want is, if I will do the first click then first value will display in the first edittext and in the second click the value will display in the second edittext.
My problem is that in the first click the value is getting displayed in the 2 edittexts simultaneously. Hope you can help me, here is my code:
b1=(Button)findViewById(R.id.b1);
et1=(EditText)findViewById(R.id.first);
et2=(EditText)findViewById(R.id.second);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
str=((Button)v).getText().toString();
et1.setText(str);
et2.setText(str);
}
});
b1 = one of the buttons.
et1 and et2= the two edittexts.
str = empty string

That doesn't surprise me. You don't distinguish your cases within your onClickListener.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
str=((Button)v).getText().toString();
// Check which edit text
if(isFirstClick()) {
et1.setText(str);
setFirstClick(false);
} else {
et2.setText(str);
}
}
});
See the 'isFirstClick()' like some kind of pseudo code, maybe you can also check if your first editbox is still empty or something like that.

Both #Averroes and #Sambuca have provided a smiliar solution which should work:
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
str=((Button)v).getText().toString();
if((et1.getText().toString()).equals(""))
et1.setText(str);
else
et2.setText(str);
}
});
In case you can do more than 2 clicks and your requirement is to output to textBox1 on odd clisk and Textbox2 on even click - use a boolean variable.

Related

2 OnClickListeners for one Button cannot resolve the OnClick Listener

I added the following lines of Code into my OnCreate method.My goal is to assign a button to two functions and to call them up alternately. With the first click the text of the button should be changed and the EditText should be editable. At the second click, the fields should no longer be editable and the button text should change to the first alternative. I have implemented two OnClickListeners and the program structure seems logical to me. Nevertheless, I get an error message; "Cannot resolve symbol onClickListener". What can I do to get the setup described above up and running? Thanks for all responses!
private Button ProfilUpdate;
ProfilUpdate=findViewById(R.id.buttonProfilUpdate);
.
.
.
.
final ProfilUpdate.OnClickListener listener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Profil bearbeiten");
profilVorname.setFocusable(false);
}
};
ProfilUpdate.OnClickListener listener1 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Ă„nderungen speichern");
profilVorname.setFocusable(true);
v.setOnClickListener(listener2);
}
};
ProfilUpdate.setOnClickListener(listener1);
why don't you create a boolean isFirstClick = true , and then check it in the same listener
ProfilUpdate.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFirstClick){
//Do the job for the first click process
isFirstClick= false;
}else {
//Do the job for the second click process
isFirstClick= true;
}
}
};
ProfilUpdate.setOnClickListener(listener);
There can only be one click listener on one view at a time. Use ProfileUpdate.setOnClickListener(listener object). To get the alternate functionality, you can define a Boolean to keep track of the state, for the example, define a class variable at the top Boolean shouldChangeText = true, and in the onClick body in the listener, do something like:
If (shouldChangeText) { // change the text
}
else { // clear the text
}
shouldChangeText = !shouldChangeText

Having trouble understanding this line of code

If anyone wants to break down this code and explain it to me. I'd be thankful.
I run into an error on view(cannot resolve symbol), not sure if I'm supposed to replace that with a specific view ?
Btw, this is an onClick method.
"else if(view.getId()==R.id.Button9){}"
What I understand from this code is it says when "if" "view" whatever the viewid is goes in here ()?? == <--is related to R.id.button9
then run this block of code. Am i even close? Thanks.
A little backstory, I have created an ImageButton and when it's clicked, I'd like to to clear the screen. I've built on onclicklistener and implemented view.OnClickListener on my user public class.
CLEARCANVAS = (ImageButton) findViewById(R.id.button9);
CLEARCANVAS.setOnClickListener(this);
#Override
public void onClick(View v) {
if (view.getId()==R.id.button9);
}
Your View parameter is v, not view.
Change it to v and it will compile:
#Override
public void onClick(View v) {
//if (view.getId()==R.id.button9){
if (v.getId() == R.id.button9){
//handle button9 click
}
}
Another common way to do it is to define a separate click listener for each clickable element, for example:
clearCanvas = (ImageButton) findViewById(R.id.button9);
clearCanvas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Handle button9 click
}
});
If the error is related with class R, then you need to rebuild the project or else if its still not working for you, you will have to restart the IDE you are using. Basically if you are using Eclipse, I suggest to change to Android Studio as google has deprecated android support to eclipse. Cannot resolve symbol error basically means that the project does not have particular class reference or whatever you are trying to use.
It is like this.
You have 10 buttons from button1 to button10. Everything has an id stored in R.java file to uniquely identify it.
Suppose you want to perform a onClick event for button9. You create a generic onClick event for all the buttons.
#Override
public void onClick(View v) {
//now all the 10 buttons have the same onclick event.
}
Now to differentiate between which button was clicked, you need to use it's id.
#Override
public void onClick(View v) {
if (v.getId()==R.id.button9){
Toast.makeText(context, "Button 9 Clicked!", Toast.LENGTH_SHORT).show();
}
if (v.getId()==R.id.button8){
Toast.makeText(context, "Button 8 Clicked!", Toast.LENGTH_SHORT).show();
}
...
}
So, R.java is a link between your UI objects in the XML and the java code.
You'll need the R.java to identify each object from the UI.
This is automatically done. If you get an error saying cannot resolve R. You might want to re-build or clean your project.
you just need to change your view.getId() with the v.getId().
The view which is passed in the OnClick method is the view which is clicked, so It will check which Id is clicked.

Android: Button On Click listener: Single and constant when held down

I am unsure how to word this right but I want a button / on click listener to do this
1 touch = 1 tap = generate 1 row
Hold button down = generate 1 row per seconds or x per second
You know in games like clash of clans when u recruit soldier, u can touch it for 1 soldier or hold it down for more. I want a similar implementation but for a button
Thank you
For one single tap to generate 1 row, use:-
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your code here for single tap event
}
});
For long press use the following:-
b.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//your code here for long press event
}
});

how to get the value of the button and display it on edittext?

my application generate random numbers in the buttons. and if you click a button i want to display the value of the button in the edittext but i dont have idea how to do it?:( can someone help me with this? ps: the numbers in the buttons are random so everytime the app is run, the number in the buttons change. i have tried this but nothing happens...
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
hold=b1.getText().toString();
et1.setText(hold);
}
});
hold is a empty String.
b1 is the button.
et1 is the edittext.
To get the text of the button, you need to use the getText() method.
You can get more details here: http://developer.android.com/reference/android/widget/Button.html
To set random number on your button:-
int random = (int)Math.ceil(Math.random()*100);
b1.setText(""+random);
and then your button clicklistener is fine for getting text from button.
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
hold=b1.getText().toString();
et1.setText(hold);
}
});

Button click change textview

I want to make a simple counter program where I have a Textview and two buttons, one with + another with - whichever I press I get 1 incremented or decremented.
My problem is that I want the Textview to update a variable and not text. (By that I mean I get the app to work with .setText("counter:"+ var_counter) but doesn't work with .setText(var_counter).
var_counter is an int.
final TextView tvContador = (TextView)findViewById(R.id.tvContador);
Button add = (Button)findViewById(R.id.bMais);
Button sub = (Button)findViewById(R.id.bMenos);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
contador++;
tvContador.setText(getText(contador));
}
});
p.s: my string is a resource string.
Since var_counter is an int, setText(var_counter) tries to set the text of the widget to the string resource with such value as its id. What you want is:
setText(Integer.toString(var_counter));
use
.setText(Integer.toString(var_counter));
Simply:
.setText(""+var_counter);

Categories