How to use repeated methods?

4

I have some methods that I use at various points of the applications and I end up having to copy in each class.

For example:

String getStringOrEmpty(EditText editText) {
    String mString = editText.getText().toString();
    mString = (mString.isEmpty() ? "" : mString);
    return mString;
}

Just to inform: this method returns a String with the text that the EditText, if any, or an empty String.

I did not want to have to copy it in every class that will use it, but I would like to access this and other methods throughout the project.

This would be similar to a library of functions in other languages. For example, in php, I would make a file with a list of functions and would use an include command to import the list into the page.

How do you do this in Java (with emphasis for Android)?

    
asked by anonymous 02.02.2017 / 03:31

3 answers

1

You can create a class with static methods . Just by the name, you can suspect that it is something related to constant, something 'stopped' (static).

When we define a class and create several objects of it, we already know that each object will be a faithful copy of the class, but with its own variables and methods in distinct places of memory. That is, the 'fusca' object has its own variables, different from the 'ferrari' object, although both have the same 'model', which is the 'Carro' class.

When we define variables with the word static in a class it will have a special behavior: it will be the same for all objects in that class. That is, there will not be a type of it in each object. All objects, when accessing and modifying this variable, will access the same variable, the same memory space , and the change can be seen on all objects.

You can create a class called Utils containing all the methods you wanted to use in several classes. See:

public classe Utils{

   public Utils(){
   }

    public static String returnDate(){
        // Aqui o código para retornar data atual
        return date;
    }

    public void String returnHour(){
        // Aqui o código para retornar a hora atual
        return hour;
    }

    public static String getStringOrEmpty1(EditText editText){ 
        String mString = editText.getText().toString();
        mString = (mString.isEmpty() ? "" : mString); 
        return mString; 
    }

    public String getStringOrEmpty2(EditText editText){ 
        String mString = editText.getText().toString();
        mString = (mString.isEmpty() ? "" : mString); 
        return mString; 
    }
}

Access to these methods is done as follows:

Static

String date = Utils.returnDate();
String value = Utils.getStringOrEmpty1(editText);

Not static

String hour = new Utils().returnDate();
String value = new Utils().getStringOrEmpty2(editText);

When to use static variables?

Mainly when you want to have control over objects or when all objects should share information (avoid having to do Composition or call methods of other objects).

    
02.02.2017 / 05:01
2

I think the best way to do this is to create a utility package. Inside the package you create a class that can be imported whenever you need and use this method. In the case of this method that is presenting, could be static, in this way nor would it be necessary to instantiate the object of the class. It would look something like:

Create a useful name package. Ex: br.com.seuprojeto.util

Next creates the utility class. Call it what you think is best. Here I call StringUtil

//imports aqui
public class StringUtil {

    public static String getStringOrEmpty(EditText editText) {
        String mString = editText.getText().toString();
        mString = (mString.isEmpty() ? "" : mString);
        return mString; 
    }

}

To use it in your project, just call:

//código da classe
String mNome = StringUtil.getStringOrEmpty(mEditText);
    
02.02.2017 / 04:13
2

Another option, other than the ones already mentioned, is to use a practice called custom view .

Basically, you create a class that inherits from the type of View that you want to modify and make your own implementations.

In your case, it would look like this:

public class CustomEditText extends EditText {

    public CustomEditText(Context context) {
        this(context, null, 0);
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public String getStringOrEmpty() {
        String mString = getText().toString();
        mString = (mString.isEmpty() ? "" : mString);
        return mString;
    }
}

To use this custom view , you need to change in your xml (layout):

Before:

<EditText
    android:id="@+id/seu_id"
    style="@style/AlgumStyle" />

After:

<br.com.seupacote.view.CustomEditText
    android:id="@+id/seu_id"
    style="@style/AlgumStyle" />

Now to access the getStringOrEmpty() method is quite easy:

public class AlgumaActivity extends Activity {

    private CustomEditText customEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        customEditText = (customEditText) findViewById(R.id.seu_id);
        String algumTexto = customEditText.getStringOrEmpty();
    }

}

This practice is interesting because you avoid creating static methods and classes without need.

    
02.02.2017 / 12:39