P
P
PK_D2018-03-21 01:30:57
Android
PK_D, 2018-03-21 01:30:57

How to reuse code for android apps?

Hello!
I am now understanding Android programming and the question arose of how to write the code correctly so that it can also be used in other applications?
I have an activity that displays several items to the user:

  1. EditText1
  2. EditText2
  3. Spinner1
  4. EditText3
  5. EditText4
  6. Button1
  7. Button2

When certain values ​​are selected in Spinner1, EditText3/4 elements can appear and disappear.
Button1/2 have their own handlers, which are located in the same activity.
1. Is it possible to make Spinner1, EditText3, EditText4, Button1, Button2 reusable in applications? Those. write several lines in the layout and get the same structure and behavior: when you select a dropdown list item, the text fields disappear / appear, etc.
2. Is it possible to put a number of auxiliary functions into a common "package", for example: setting / getting values ​​for these fields in SystemPreference, etc.
Now all the code is in one Activity and it becomes more and more difficult to work with it.
Please tell me which way to look
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Vitaliev, 2018-03-21
@PK_D

1. Perhaps, for this you need to create your own class of a button, spinner, EditText, whatever

public class CustomEditText extends EditText {
// Твоя логика, можно написать тут свои листенеры а в Activity передавать Spinner чтобы подписываться на его изменение
}

And further in layout'e
<your.package.CustomEditText
...
/>

If you need to completely repeat the entire structure, then it might be worth using Fragments
2. I don’t quite understand what SystemPreference is, perhaps SharedPreference was meant.
You can, for this, for example, you can create a SharedPreferenceUtil class in which to declare
class SharedPreferenceUtil {
  private final SharedPreferences preferences;
private SharedPreferenceUtil (Context context) {
    preferences = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);
}

public int getValue() {
    return preferences.getInt(PREFERENCE_VALUE, 0);
}

Something like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question