Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Helper classes

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.

like image 907
Paddy1990 Avatar asked Apr 06 '14 08:04

Paddy1990


People also ask

What is the use of helper class in Android?

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.

What is helper in Android?

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.

What is the use of helper classes?

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

How do you create a helper class?

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.


2 Answers

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.

Update:

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
}
like image 136
Simon Dorociak Avatar answered Oct 09 '22 09:10

Simon Dorociak


Why not use abstract BaseActivity with all these methods? And extend this class in all your other Activities?

like image 36
agamov Avatar answered Oct 09 '22 09:10

agamov