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;
}
}
Related
I am working on a simple login form with 4 kinds of users. The onclicklisteners view parameter always shows the same v.getId() value which was clicked first time. Eg if the student view is clicked,the id value if updated but if i click the admin layout, the id still stays same as student.This happens with all the 4 sections.
Listener:
#Override
public void onClick(View v) {
loginActivityBinding.etPassword.setText("");
loginActivityBinding.etUsername.setText("");
loginActivityBinding.etPassword.setOnFocusChangeListener((v1, hasFocus) -> {
if (!Utils.isValidPassword(loginActivityBinding.etPassword.toString())) {
showToast("Please enter a valid password");
}
});
alertDialog.setPositiveButton("Login", (dialog, which) -> {
if (StringUtils.isEmpty(loginActivityBinding.etUsername.getText().toString(), loginActivityBinding.etPassword.getText().toString())) {
showToast("Error .Please fill all fields");
} else {
Timber.d(String.valueOf(v.getId()));
switch (v.getId()) {
case R.id.ll_Admin:
mainLoginViewModel.login(loginActivityBinding.etUsername.getText().toString(), loginActivityBinding.etPassword.getText().toString(), 1);
break;
case R.id.ll_faculty:
mainLoginViewModel.login(loginActivityBinding.etUsername.getText().toString(), loginActivityBinding.etPassword.getText().toString(), 2);
break;
case R.id.ll_student:
mainLoginViewModel.login(loginActivityBinding.etUsername.getText().toString(), loginActivityBinding.etPassword.getText().toString(), 3);
break;
case R.id.counsellorlayout:
mainLoginViewModel.login(loginActivityBinding.etUsername.getText().toString(), loginActivityBinding.etPassword.getText().toString(), 4);
break;
}
}
});
}
The flag value passed to the viewmodel stays the same after the first click.
Fixed it by creating seperate click listeners using the setOnClickListener approach. bad approach surely but couldnt see any other way out.
I have 50 Buttons. I use the switch statement as shown below. Each case has the same On-Click code. But I make many changes to the code for each Button, and it's really tedious work and very repetitive to change the code the same way 50 times. My code currently looks like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button1:
if(button_list.get(1).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(1).getText().toString());
is_clicked= (String)button_list.get(1).getTag();
if ("0".equals(is_clicked)){
button_list.get(1).setBackgroundResource(R.drawable.button_pressed);
button_list.get(1).setTag("1");
}else{
button_list.get(1).setBackgroundResource(R.drawable.button_normal);
button_list.get(1).setTag("0");
}
break;
case button2:
if(button_list.get(2).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(2).getText().toString());
is_clicked= (String)button_list.get(2).getTag();
if ("0".equals(is_clicked)){
button_list.get(2).setBackgroundResource(R.drawable.button_pressed);
button_list.get(2).setTag("1");
}else{
button_list.get(2).setBackgroundResource(R.drawable.button_normal);
button_list.get(2).setTag("0");
}
break;
case button3:
if(button_list.get(3).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(3).getText().toString());
is_clicked= (String)button_list.get(3).getTag();
if ("0".equals(is_clicked)){
button_list.get(3).setBackgroundResource(R.drawable.button_pressed);
button_list.get(3).setTag("1");
}else{
button_list.get(3).setBackgroundResource(R.drawable.button_normal);
button_list.get(3).setTag("0");
}
break;
case button4:
if(button_list.get(4).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(4).getText().toString());
is_clicked= (String)button_list.get(4).getTag();
if ("0".equals(is_clicked)){
button_list.get(4).setBackgroundResource(R.drawable.button_pressed);
button_list.get(4).setTag("1");
}else{
button_list.get(4).setBackgroundResource(R.drawable.button_normal);
button_list.get(4).setTag("0");
}
break;
case button5:
if(button_list.get(5).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(5).getText().toString());
is_clicked= (String)button_list.get(5).getTag();
if ("0".equals(is_clicked)){
button_list.get(5).setBackgroundResource(R.drawable.button_pressed);
button_list.get(5).setTag("1");
}else{
button_list.get(5).setBackgroundResource(R.drawable.button_normal);
button_list.get(5).setTag("0");
}
break;
case button6:
if(button_list.get(6).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(6).getText().toString());
is_clicked= (String)button_list.get(6).getTag();
if ("0".equals(is_clicked)){
button_list.get(6).setBackgroundResource(R.drawable.button_pressed);
button_list.get(6).setTag("1");
}else{
button_list.get(6).setBackgroundResource(R.drawable.button_normal);
button_list.get(6).setTag("0");
}
break;
case button7:
if(button_list.get(7).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(7).getText().toString());
is_clicked= (String)button_list.get(7).getTag();
if ("0".equals(is_clicked)){
button_list.get(7).setBackgroundResource(R.drawable.button_pressed);
button_list.get(7).setTag("1");
}else{
button_list.get(7).setBackgroundResource(R.drawable.button_normal);
button_list.get(7).setTag("0");
}
break;
case button8:
if(button_list.get(8).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(8).getText().toString());
is_clicked= (String)button_list.get(8).getTag();
if ("0".equals(is_clicked)){
button_list.get(8).setBackgroundResource(R.drawable.button_pressed);
button_list.get(8).setTag("1");
}else{
button_list.get(8).setBackgroundResource(R.drawable.button_normal);
button_list.get(8).setTag("0");
}
break;
case button9:
if(button_list.get(9).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(9).getText().toString());
is_clicked= (String)button_list.get(9).getTag();
if ("0".equals(is_clicked)){
button_list.get(9).setBackgroundResource(R.drawable.button_pressed);
button_list.get(9).setTag("1");
}else{
button_list.get(9).setBackgroundResource(R.drawable.button_normal);
button_list.get(9).setTag("0");
}
break;
case button10:
if(button_list.get(10).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(10).getText().toString());
is_clicked= (String)button_list.get(10).getTag();
if ("0".equals(is_clicked)){
button_list.get(10).setBackgroundResource(R.drawable.button_pressed);
button_list.get(10).setTag("1");
}else{
button_list.get(10).setBackgroundResource(R.drawable.button_normal);
button_list.get(10).setTag("0");
}
break;
Is there a way I can use a general variable to access each case so if I make a change I won't need to make it 50 times for each button? I thought about doing something like this:
public void ButtonOnClick(View v) {
switch (v.getId()) {
case button[i]
if(button_list.get(i).getTag().equals("0"))
enter_txt.setText(enter_txt.getText()+button_list.get(i).getText().toString());
is_clicked= (String)button_list.get(i).getTag();
if ("0".equals(is_clicked)){
button_list.get(i).setBackgroundResource(R.drawable.button_pressed);
button_list.get(i).setTag("1");
}else{
button_list.get(i).setBackgroundResource(R.drawable.button_normal);
button_list.get(i).setTag("0");
}
break;
Would this work? Or is there something else I need to do so I don't need to make 50 changes to my code every time I decide I want to change the OnClick method?
I'm going to assume that ButtonOnClick is the click handler for the buttons. I'm going to further assume that for any i, the contents of button_list.get(i) is the button. If that's right, then the argument v is the same Button object stored in the list. You can then reduce your entire handler to:
public void ButtonOnClick(View v) {
Button btn = (Button) v;
if (btn.getTag().equals("0")) {
enter_txt.setText(enter_txt.getText()+btn.getText().toString());
}
is_clicked= (String) btn.getTag();
if ("0".equals(is_clicked)){
btn.setBackgroundResource(R.drawable.button_pressed);
btn.setTag("1");
}else{
btn.setBackgroundResource(R.drawable.button_normal);
btn.setTag("0");
}
}
As an aside, you might consider using Boolean objects instead of String objects as the button tags.
First you should never copy paste code, try to extract your code in another method.
If you are using a arraylist where you store all your buttons, you can probably use the ArrayList.indexOf(Object o)
So just replace your ButtonOnClick method with this one, you can handle other buttons behavior easily too in the future :
public void ButtonOnClick(View v) {
if(button_list.contains(v))
{
setButtonBackground(button_list.indexOf(v));
}
}
private setButtonBackground(int buttonNumber)
{
Button myButton = (Button) button_list.get(buttonNumber);
if(myButton.getTag().equals("0"))
{
enter_txt.setText(enter_txt.getText()+myButton.getText().toString());
}
is_clicked = (String) myButton.getTag();
if ("0".equals(is_clicked))
{
myButton.setBackgroundResource(R.drawable.button_pressed);
myButton.setTag("1");
}
else
{
myButton.setBackgroundResource(R.drawable.button_normal);
myButton.setTag("0");
}
}
When you are developping instead of copy and paste code, just use the shortcut extract to a method so you will get something like this and will save yourself time.
[...]
case button1:
setButtonBackground(1);
break;
case button2:
setButtonBackground(2);
break;
[...]
Let me know if it works for you
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");
}
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.
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
}