I have a couple of methods in my android application that I would like to reuse across multiple Activities, as it's a Java helper class I'm not extending from anything. I have two problems, the first is how to pass the context into the java class and the second is how to use methods that extend from activity in the helper class, below is my helper class:
Java Class
public class ScreenHelper {
Context context;
public ScreenHelper(Context ctx) {
this.context = ctx;
}
public int getStatusBarHeight(layout layout) {
Rect r = new Rect();
Window w = (Window) getWindow();
w.getDecorView().getWindowVisibleDisplayFrame(r);
return r.top;
}
public int getScreenHeight() {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int ScreenHeight = metrics.heightPixels;
return ScreenHeight;
}
public int getTitleBarHeight() {
RelativeLayout relaLayout = (RelativeLayout) findViewById(R.id.title_bar);
int titleHeight = relaLayout.getHeight();
Log.d("Title Height","Title Height:" + titleHeight);
return titleHeight;
}
}
Any help would be much appreciated.
Helper Class is a Java class which includes basic error handling, some helper functions etc. Helper class contains functions that help in assisting the program. This Class intends to give quick implementation of basic functions such that programmers do not have to implement again and again.
Helper classes are the utility entities. They are better used just like utility so prevent the instantiation and extension by marking the default constructor as private. Expose the ' static ' methods.
In object-oriented programming, a helper class is used to assist in providing some functionality, which isn't the main goal of the application or class in which it is used. An instance of a helper class is called a helper object (for example, in the delegation pattern).
You can create helper class by making all its methods static and its constructor private, and optionally you can also make the class final. Thus it can not be instantiated and one can easily access to all its methods directly.
the first is how to pass the context into the java class
You can pass it as parameter of each method. Also for easier access you can make those methods as final static.
how to use methods that extend from activity in the helper class, below is my helper class.
This sounds like bad design of application logic. Methods inherited from Activity class should be visible and used only from Activity subclass(es).
But if you really need it, also you can pass Activity as method parameter but i recommend you to find another way.
how do I pass the context as a marameter of each method?
Example of method:
public static final void foo(Context c, int value, String message) {
// do your stuff
}
Why not use abstract BaseActivity
with all these methods? And extend this class in all your other Activities
?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With