Changing button text and ID by clicking on it - android

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");
}

Related

one Button for multiple thing

I just want to implement one button to do multiple actions like on first click make Textview1 visible and on second click make Textview2 visible and so on.
here is my code it works but for 2 actions only i want to set more visible component in one button i hope its clear and Thanks for any help
final TextView textView_r4 = findViewById(R.id.tv_r4);
final EditText editText_r4 = findViewById(R.id.input_R4);
final TextView textView_r5 = findViewById(R.id.tv_r5);
final EditText editText_r5 = findViewById(R.id.input_R5);
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView_r4.setVisibility(View.VISIBLE);
editText_r4.setVisibility(View.VISIBLE);
}
});
findViewById(R.id.Addbtn).setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
textView_r5.setVisibility(View.VISIBLE);
editText_r5.setVisibility(View.VISIBLE);
return true;
}
});
You can add an enum State to keep track of which state your button is in. Create a class field in the same class (activity) that these methods are in, and change the state every time you click. Then in the .setOnClickListener method you can check which state the button is in, and depending on that do different actions.
private State state = INITIAL;
findViewById(R.id.Addbtn).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (state) {
case INITIAL:
// do first action
state = State.CLICKED_ONCE;
break;
case CLICKED_ONCE:
// do second action
state = State.CLICKED_TWICE;
break;
default:
// clicked too many times, no action taken
break;
}
}
});
private enum State { INITIAL, CLICKED_ONCE, CLICKED_TWICE }

how to lock method and release it after test

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;

retrieving the id of the view that was clicked [duplicate]

This question already has answers here:
android: How to get the ID of a textview when clicked?
(2 answers)
Closed 9 years ago.
I'm using MyActivity extends Activity implements OnClickListener{
This activity references around 10+ buttons and has setOnClicklistener(this) method called on every button.
#Override
public void onClick(View v){
//here I need to get the id of the view that was clicked...
//Depending on the button that was clicked different actions need to be called...
//How do I get the ID of the button that was clicked...
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.btnCancel:
//your code for onclick of that button
break;
}
you can use following method to get id.
v.getId()
#Override
public void onClick(View v){
int id = v.getId();
if(id == R.id.button_ok){
}
}
The View parameter that is sent to your onClick method is the actual button that was clicked, therefore you can check which one it is, for example:
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.button_1: ...; break;
case R.id.button_2: ...; break;
case R.id.button_3: ...; break;
...
default: //unknown button clicked
}
}
This is only one option, there are other. Search google for more info.
use :
if(v.getId()==R.id.whatever)
{
// do something
}
or you can even use :
Button btn = (Button)findViewById(R.id.btn);
if(v==btn)
{
// do something
}
but the second one is not recommended.

One method for several buttons?

Instead of have to call separate method for each button, would it be possible to just use one method and check which button it was that was clicked by it's id?
I'm using the simple way like android:onClick="activateButtons"
public void activateButtons(View v)
{
if(?? == btn_1)
{
Code for button with ID = btn_1
}
if(?? == btn_1)
{
Code for button with ID = btn_2
}
}
You can do that if you tag the buttons with an Id. Set the android:id="#+id/yourbuttonid" in the xml file. Then do if(v.getId() == R.id.yourbuttonid) in the onClick method.
Yes it is possible. You can assign specific ids(eg: button1) for buttons and then call v.getId() in activateButtons(onclick method) to check if it is equal to R.id.button1 and then do your operation there.
Cheers,
Richie
public void onClickKeyPad(View view)
{
EditText text = (EditText)findViewById(R.id.text_password);
switch(view.getId())
{
case R.id.button_0:
text.setText(text.getText() + "0");
break;
case R.id.button_1:
text.setText(text.getText() + "1");
break;
case R.id.button_cancel:
finish();
break;
default:
break;
}
}

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
}

Categories

Resources