0

I'm trying to keep my code more developer friendly. Originally I have set up a button with a standard on click listener which simply increase the value of a edit text by 1 each time its pressed.

Now in order to try keep it more organised I'm trying to move the on click listener to its own class which will host a range of switch statements. The only problem I'm experiencing is the ability to set up a getter and setter for my method.

This is my current on click listener

 testButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int time = Integer.valueOf(editHours.getText().toString());
                    time++;

                   editHours.setText(Integer.toString(time));
                }
            });

This is my separate case statement which can be linked in at a later point.

public class MyClickListener implements View.OnClickListener {



@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.testingButton:

            Log.i("Test", "Complete");


            break;

I'm only stuck on the ability to set up getter and setter. Which takes the current value with the getter and then finally use the setter to set the new value.

Also as a side note you have noticed that I haven't demonstrated my own getter and setter that is because I'm slightly confused about how to do this for this method.

Thank you

Steven
  • 49
  • 9
  • You want to setup getter and setter methods for which class? – Prerak Sola Feb 23 '17 at 16:48
  • It would be setup for my OnclickListener class as that's where I need it to get value when the button is pressed. Then have it set on my Main Class – Steven Feb 23 '17 at 16:55
  • It is still unclear to me that what is your final goal for this setup. – Prerak Sola Feb 23 '17 at 16:59
  • Basically I don't want all these methods crowding up my activity so instead I want a View.OnclickListener to store the onclick and then I reference it later. e.g Btn.setOnclickListener(New methodname) – Steven Feb 23 '17 at 17:02

4 Answers4

0

I don't know if you know this, but you do not have to use anonymous classes, or separate classes at all, but can set android:onclick="onClickMethod" on your button in xml and then you simply have to define the method in the activity itself. This will work with any method name as long as the method is of the form public void methodName(View v) In fact this is recommended in the official Android Developers training and reference. Therefore I would have in my activity class:

public void onTestClick(View v) {
    int time = Integer.valueOf(editHours.getText().toString());
    time++;

    editHours.setText(Integer.toString(time));
}

and in xml:

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onTestClick"
    />

I usually use this method as a developer can look at the xml and see the method name, and then go to the activity and see exactly what it is doing. It is also less messy to my eye than an anonymous class, and in any case where I might want to programmatically run the same code I have access to the method. Also AndroidStudio knows automatically that the two are linked, so all of its functionality like findusages and such work with this method.

You can see more about this here: https://developer.android.com/reference/android/widget/Button.html

drawinfinity
  • 315
  • 4
  • 21
  • I understand I've used this method multiple times. I just want to separate my methods completely from the Main Activity and put them into there own View.OnclickListener – Steven Feb 23 '17 at 17:00
  • To do so I believe the easiest way would be to use an observer pattern, similar to how you would have a fragment change information in an activity. This is kind of redundant since by definition an OnClickListener is implementing the same pattern, so the two classes will be observing each other. Either way I think no matter how you tried to implement, you will need another method in the activity. Personally I would stick with the recommended methods, as it seems you will just be created more complicated code that would actually be harder for another developer to read. – drawinfinity Feb 23 '17 at 17:11
  • I understand where you are coming from but the amount of methods I'm going to implement will over crowd my entire activity – Steven Feb 23 '17 at 17:20
0

Based on your comments to my answer above I would suggest doing something like this answer: https://stackoverflow.com/a/9977370/4888701 , except the interface is defined in your listener class, and you feed the activity to the listeners constructer when you create a new Listener. Otherwise it would work the same. Depending on how many values you might be editing, it could potentially save you lines of code in the activity or not. This would also make the listener reusable for other activities if you are doing similar things, as you could just implement the method in each activity.

If this adds too much code you could try defining the Listener class as a private inner class in Activity. That way you could access the values but at least have the option of hiding the class contents in while viewing code.

Community
  • 1
  • 1
drawinfinity
  • 315
  • 4
  • 21
  • Is there just a way off moving my current Onclick listener which is represent in the first code section, onto the a View.Onclick listener? – Steven Feb 23 '17 at 17:42
  • To my knowledge not unless it is an inner class. Alternatively you could try storing the data in a singleton class that both the activity and listener access. This way the data is decoupled from both and you can edit it at will, and you can have the activity listen for a data change. – drawinfinity Feb 23 '17 at 17:52
0

If I understand your use case, you don't necessarily need getter/setter. Why not use something like:

public class EditTextIncrementor implements View.OnClickListener {
        private final EditText editText;

        public EditTextIncrementor(EditText editText){
            this.editText = editText;
        }


       @Override
       public void onClick(View v) {
           String counter = editText.getText().toString();
           int time = Integer.valueOf(counter);
           time++;
           editText.setText(Integer.toString(time));
       }
 }
Yoni Gross
  • 816
  • 8
  • 16
0
public class MyClickListener extends AppCompatActivity implements View.OnClickListener {

EditText editHours;
Button testButton;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    editHours = (EditText)findViewById(R.id.editHours);
    testButton = (Button)findViewById(R.id.testingButton);



     testButton.setOnClickListener(new MyClickListener());
}

@Override
public void onClick(View v) {



    switch (v.getId()){


        case R.id.testingButton:

            Log.i("Test", "Complete");

            int time = Integer.valueOf(editHours.getText().toString());
            time++;

            editHours.setText(Integer.toString(time));




            break;

        default:
            Log.i("Test", "Complete");
    }
Steven
  • 49
  • 9