how to lock method and release it after test - android

I have tried to lock onclick method after 2 clicks and release it after test/compare betweeen these clicks, I have recyclerview and listener to these click
is there a way to do it?

When you want to lock onClick method, write this code:
int counter=0; //definite it as an instance variable
public void onClick(View view)
{
if(counter<2)
{
swtich(view.getId())
{
case R.id.firstcard:
//do something
break;
case R.id.secondcard:
//do something
break;
...
counter++;
}
}
}
Of course you have to set this in the layout for each card:
android:onClick="onClick"
And when you want to reset the clicks, you have to set:
counter=0;

Related

Android single button multiple functions

I am beginner to Android development. I have 3 edit boxes and one "Edit" button. When I launch the activity all the edit boxes should be disabled. When I click on the Edit button all the 3 edit boxes should get enabled and button text should change to "Save". After updating the data in the edit boxes, when I click on the "Save" button, I should be able to send the updated data to the backend.
My problem is how can I make use of a single button for two function "Edit" and "Save".
Please help me.
You can do it this way:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String ButtonText = button.getText().toString();
if(ButtonText.equals("Save"){
//code for save
button.setText("Edit");
}
else{
//code for edit
button.setText("Save");
}
}
});
If I were you I would actually use two buttons one for edit, and one for save. Make them the same size and in the same position, when you want to switch between them make one invisible, and the other visible. Doing it that way would let you keep your onClickListeners separate which would make your code more understandable in my mind.
That being said you could technically achieve it with a single button as well. Just change the text on the button when you want to switch between them, and add an if statement into your click listener to check which "mode" your button is currently in to determine which action it should take.
I am not sure there is an easy way to do this or not. but you can sure use different behaviors of button clicks like
// When you press it for long time.
dummyButton.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
return true; // Can do lot more stuff here I am just returning boolean
}
});
// Normal click of button
dummyButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//do lot more stuff here.
}
});
Do it this way :
Make a public boolean variable
public boolean isClickedFirstTime = true;
make your 3 editTexts enabled false in xml and
onClick of your button
#Override
public void onClick(View v) {
if (v.getId() == R.id.edit_button_id) { //whatever your id of button
Button button = (Button) findViewById(R.id.edit_button_id);
if(isClickedFirstTime)
{
edit1.setEnabled(true);
edit2.setEnabled(true);
edit3.setEnabled(true);
butt.setText("Save");
isClickedFirstTime = false;
}
else
{
....//Get your values from editText and update your database
isClickedFirstTime = true;
}
}

Changing button text and ID by clicking on it

In part of my program, I change the id and text of a button when it is clicked. When the button is clicked again, the id and text are reverted to the previous values. However, I am getting error. (I have added a comment to the line where I am getting the error).
I just need to change text by clicking a button. Then change it back to the old values by clicking it again. Does anyone have a solution or some better idea on how to accomplish this?
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
mPlayer.stop();
start.setText("Pause");
start.setId(R.id.pause);
break;
case R.id.pause:
start.setText("Pause"); //here it is not accepting pause
start.setId(R.id.btn_start_again);
break;
}
}
I don't think you want to change the id of the Button. What it looks like you are doing is having a Button that starts play then turns to pause while playing. Simply have the two Buttons in the same space in your xml. Start with the visibility of the pause Button as gone then check the visibility in the function.
Something like
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
mPlayer.stop();
btnPause.setVisibility(View.VISIBLE);
btnPlay.setVisibility(View.GONE);
break;
case R.id.pause:
btnPause.setVisibility(View.GONE);
btnPlay.setVisibility(View.VISIBLE);
break;
}
Button Docs
You don't need to change the ID of the button, use something like a boolean flag to keep track of the state of the button:
boolean isPlaying = false;
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_start_again:
if(isPlaying){
mPlayer.stop();
start.setText("Play");
isPlaying = !isPlaying;
}else{
//mPlayer.start() <--- you don't start it anywhere?
start.setText("Pause");
isPlaying = !isPlaying;
}
break;
}
}
There a better way you can do it.
Set different tag to the button according to the status.
changing ID programmatically is a bad idea, better solution is to define a global boolean value:
private boolean isSelected = false;
and then in Your onClick:
public void onClick(View v) {
if(isSelected==false){
mPlayer.stop();
start.setText("Pause");
isSelected=true;
}else{
start.setText("Pause");
isSelected=false;
}
you could just have 1 button which can handle different events and change its behaviour based upon the current button text.
public void onClick(View v) {
switch(v.getText()){
case "Pause":
mPlayer.stop();
start.setText("Play");
break;
case "Play":
start.setText("Pause");
mPlayer.play();
break;
}
}
or you could have the button initally setup to fire 1 click event which once fired then sets the button click event to the second click event and vice versa
public void onClickOne()
{
// do stuff
btn.OnClick = onClickTwo();
}
public void onClickTwo()
{
// do stuff
btn.OnClick = onClickOne
}
I ended up doing it in this way:
String value=start.getText().toString();
if(value.equals("Start")){
start.setText("Pause");
}
else if(value.equals("Pause")){
start.setText("Start");
}

Android: long click on a button -> perform actions

I want to use the same button to perform 2 different methods.
One method when user single clicks it and a second method (different) when the user LONG clicks it.
I use this for the single short click (which works great):
Button downSelected = (Button) findViewById(R.id.downSelected);
downSelected.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
method();
}
}
});
I've tried to add a longClickListener but it didn't work.
Appreciate any ideas on how to solve this.
Thanks!
I've done it before, I just used:
down.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Per documentation:
public void setOnLongClickListener
(View.OnLongClickListener l)
Since: API Level 1 Register a callback
to be invoked when this view is
clicked and held. If this view is not
long clickable, it becomes long
clickable.
Notice that it requires to return a boolean, this should work.
To get both functions working for a clickable image that will respond to both short and long clicks, I tried the following that seems to work perfectly:
image = (ImageView) findViewById(R.id.imageViewCompass);
image.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
shortclick();
}
});
image.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
longclick();
return true;
}
});
//Then the functions that are called:
public void shortclick()
{
Toast.makeText(this, "Why did you do that? That hurts!!!", Toast.LENGTH_LONG).show();
}
public void longclick()
{
Toast.makeText(this, "Why did you do that? That REALLY hurts!!!", Toast.LENGTH_LONG).show();
}
It seems that the easy way of declaring the item in XML as clickable and then defining a function to call on the click only applies to short clicks - you must have a listener to differentiate between short and long clicks.
Initially when i implemented a longClick and a click to perform two separate events the problem i face was that when i had a longclick , the application also performed the action to be performed for a simple click . The solution i realized was to change the return type of the longClick to true which is normally false by default . Change it and it works perfectly .
Change return false; to return true; in longClickListener
You long click the button, if it returns true then it does the work. If it returns false then it does it's work and also calls the short click and then the onClick also works.
Try using an ontouch listener instead of a clicklistener.
http://developer.android.com/reference/android/view/View.OnTouchListener.html
The simplest and updated method is using a long click listener like
someView.setOnLongClickListener {
//do your work
true
}

How to determine which button pressed on android

i need to know, how to recognize, which button is pressed.
Like if i have two buttons ,say button 1 and button2,and both of them performing the same method, say method(),how to determine which button pressed ?
Regards
Most ellegant pattern to follow:
public void onClick(View v) {
switch(v.getId())
{
case R.id.button_a_id:
// handle button A click;
break;
case R.id.button_b_id:
// handle button B click;
break;
default:
throw new RuntimeException("Unknow button ID");
}
This way it's much simplier to debug it and makes sure you don't miss to handle any click.
OR... you can just put a android:onClick="foo" in the xml code of the
button, and define a method on java with the signature. Inside the
method foo, get the id and compare it with the one you need
public void foo(View v){
if (v.getId() == R.id.yourButton){
}
else if (v.getId() == R.id.nextButton){
}
}
I have 10 buttons performing the same method updateText(), I used this code to get the clicked button's text:
public void updateText(View v){
Button btn = (Button) findViewById(v.getId());
String text = btn.getText().toString();
}
If by "performing the same method" you mean theirs OnClickListener then you have to reference the parameter being passed to it.
public void onClick(View v) {
if(v==btnA) {
doA();
} else if(v==btnB) {
doB();
}
}
Ok got the solution
if (yesButton.getId() == ((Button) v).getId()){
// remainingNumber
}
else if (noButton.getId() == ((Button) v).getId())
{
// it was the second button
}

Android: What's the difference between a boolean for a click and button.isPressed()?

There are a lot of options on how to define a click/tap on the touchscreen. One of them for example is setting a boolean.
Example for boolean:
boolean buttonClicked = true;
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (buttonClicked) {
//do that and this
}
}
});
And there's a isPressed() method:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (button.isPressed()) {
//do that and this
}
}
});
What exactly is the difference between them? And when and why do I use boolean and the method isPressed()?
Because you are referring to a button in both of your examples, I assume that you are referring to the user tapping on a button, not just a random touch on the screen.
That being said, both of the examples you provided are not good.
In your first example, the boolean is useless because it is always true, so //do that and this will always be reached.
In your second example, your if statement is useless, because the onClick method by its nature is only reached when the button is tapped.
A good way to listen for a button press is using a click listener like this:
Button button = (Button) findViewById(R.id.buttonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Code placed here will run every time the button is tapped
}
});
...where R.id.buttonId is the ID of your button in the layout.
If you need to define click event for a View you can use onClickListener, onTouchListener.
For more information check for Android official Documentation.
onTouchListener
onTouchListener
When considering your first code snippet, You can use boolean to perform another operation on button click event. as example something like this ,
boolean buttonClicked = false;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//true after button clicked
buttonClicked = true;
}
});
//if buttonClicked equals true
if (buttonClicked){
//perform operation only after button clicked
}
when considering your second code snippet, no need of button.isPressed() inside
button's onClick() callback. Because what you want to do by checking button.isPressed() is done without it inside button's onClick() callback.
Keep in mind these things.
isPressed() is a public method of View Class
Button is a subclass of View Class
isPressed() is a public method of Button Class as well.
About isPressed() from Android official documentation.
Indicates whether the view is currently in pressed state. Unless
setPressed(boolean) is explicitly called, only clickable views can
enter the pressed state.
Returns true if the view is currently pressed, false otherwise.

Categories

Resources