0

I have the following Java code that allows me to dynamically change the text in my Android app header bar - retrieving the name from Strings.xml.

    // Display header title
    headertitle = findViewById(R.id.header_title);
    int id = getResources().getIdentifier(getLocalClassName(), "string", getPackageName());
    String header = getResources().getString(id);
    headertitle.setText(header);

I don't want to include this in every Activity so how do I create a file that includes this and include / import it on the respective Activities?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
fraggley
  • 1,215
  • 2
  • 9
  • 19
  • 1
    Put it in a (static) method, invoke that method. – Andy Turner Feb 15 '19 at 07:50
  • You can create a class with a static method with the code you have and then just call that method.. – Amit Jangid Feb 15 '19 at 07:50
  • 4
    Make it a utility method, or make it an instance method of another class and inject an instance of that class, or put it in a base class and inherit from that base class. – PPartisan Feb 15 '19 at 07:52
  • 1
    You can't "include" a file like you can in C or C++: because Java deliberately does *NOT* have a "preprocessor". Nor do any of the .Net languages, for that matter. What you *CAN* do is put whatever "common definitions/functionality" you want in a class or interface, then "import" that into whatever .java file needs it. PS: "headerTitle" is much nice looking than "headertitle". – paulsm4 Feb 15 '19 at 07:57
  • Thank you all. Looks like i need to do more reading – fraggley Feb 15 '19 at 11:42

0 Answers0