0

I am new to android so please bear with me. I am developing an application using android. I am using a lot of buttons which gets enabled and disabled based on the values in the database. Each time the button id is to be specified while enabling and disabling but for each button there requires a separate function. However the functionality remains the same. I thought of using macros like that of C language so that a concatenation can be done like

#define fun(str,i) str##i

so if i pass value as fun(R.id.button,12) then button with id 12 gets selected. Can macros be used or is there any other possible way to accomplish this?? Thanks in advance.

1
  • 1
    That's impossible. Even in a setting where you use C because you're trying to use runtime data for a compile time feature. Commented Nov 19, 2013 at 14:55

1 Answer 1

1

You just need to pass the id of the button.

public void fun(int id) {
    findViewById(id).setEnabled(false);
}

you can get the id as per below or from the view reference

Button btn1;
btn1.getId()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.