0

currently I am writing a Class in Java for Android. Basically, I am using this piece of code, R.id.content, a ton. Now, I was wondering, sense the content part of it may change at any time, how could I write this so I would only have to change it in one place? I was thinking I could do something like the following:

public {unknown data-type} varName = R.id.content;

But then I realized that this is actually a class, so then I was wondering if I could write it like this:

public {unknown data-type} varName = content;
.... R.id.varName;

Problem is I am not exactly sure on how I would do that either. So simply, my question is, how could I write R.id.content so I can easily change the content part of it (like put the content in to a variable)?

Michael Jones
  • 2,222
  • 18
  • 32

1 Answers1

2

Basically your R.id.content has an integer value(In the R file under id subclass). So you could store the whole thing in an int variable. Like this :

int your_variable=R.id.content;

OR

int[] your_array=new int[]{R.id.c1,R.drawable.p1};

and use it like you would normally use a variable. Eg :

EditText edt=(EditText)findViewbyId(your_variable);

Is this what you were looking for..?

Akhil Soman
  • 2,077
  • 2
  • 17
  • 30