Android - How to call a function from one simple class to another with activity?

0

I know that a very repetitive question sounds, however I still can not find what I'm looking for and I would like to ask here ...

My interest is to create a class "mother" or "manager" where I have all the functions and methods that I occupy for my app.

My interest is this:

  • Create a simple Java class and put all my methods

  • Inside this class initialize buttons, textviews, edittexts, (the views / widgets) of android

  • Send methods that contain these elements to other classes that are activity

My question is:

  • How can I do this?

  • Is it possible? How?

asked by NAYIR55 20.10.2016 в 00:57
source

2 answers

1

It is not a good practice to do it, but you could quickly create a class that has static methods where you pass an activity, and it is in charge of initializing variables and views that you want, something like}:

public class Init {

    public static void initMainActivity(MainActivity ac) {
        ac.edit1 = ac.findViewById(R.id.edit1);
        ac.edit2 = ac.findViewById(R.id.edit2);
        ac.edit3 = ac.findViewById(R.id.edit3);
    }

}

But I repeat, it is not a good practice because you take away the meaning of everything, but if you are looking for something that makes life easier, you can use ButtterKnife which makes your life easier when searching for android views

    
answered by 20.10.2016 в 01:30
0
  

Create a simple Java class and put all my methods

You can create a parent class from which your other classes extend, these methods would obviously be public.

  

Within this class initialize buttons, textviews, edittexts, (the   views / widgets) of android

If they are views (views / widgets) these references can only be used in the layout that contains them and therefore in the Activity that loads this layout.

In the parent class you can initialize the variables:

public Button myButton;
public TextView myTextView;
public EditText myEditText;

but references only when inflating the container layout.

(I do not recommend ButterKnife, it's to make something simple just as simple, if you use it it's because you do not understand how to get references of views on Android.)

  

Send calling methods containing these elements to other classes that   they are activity

To call them these methods must have the public modifier.

    
answered by 20.10.2016 в 14:04