user2832024
user2832024

Reputation: 3

macros possible in android java

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.

Upvotes: 0

Views: 237

Answers (1)

kedark
kedark

Reputation: 246

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()

Upvotes: 1

Related Questions