android - How do you get a view reference from a class that doesn't extend Activity? -


I have to be a class "utilities", which will have many ways to use all of my code. Close is a top bar with a textview and two ImageButton , which will display different texts and symbols on various activities.

I find myself writing stuff like this on every activity:

  (text view) topbartex = (textview) Find VVBID (R.D. TopBarText.setText (R.id.mytextForThisView);  

I will see once in my entire app, and call a method setupTopBar (string text, RID.ConformImaz Button1, RID.ConforyImageButton2) Pass the ID of current activity, or even the existing activity, and let the method understand what to show in text and images.

I have created the class appropriate , but it does not extend activity . The problem is that if it is not, findViewById is not accessible, so I can not use it

What is the pattern to do something like this in Android?

should look like your supporting methods

  (Activity Act, Ink TextID) {(TextView) topBarText = (TextView) act.findViewById (R.id.topBarText); TopBarText.setText (textId); }  

Then you can import a constant from activity and call

  setTopBarText (this, R.id.mytextForThisView);  

Comments