1

I have extended the Button class and I want to run methods like isInternetConnected and isUserLoggedIn when user click to this button and after perform onclick if all condition satisfied.

For Example if I created simple form where user name, email, phone no. and Submit Button placed. When user click on submit first it checks isInternetConnected and isUserLoggedIn if this satisfies then perform final operation saves user data to server or anywhere.

public class CustomButton extends Button {
private static final String TAG = "CustomButton";

public CustomButton(Context context) {
    super(context);
}

public CustomButton(Context context, AttributeSet attrs) {
    super(context, attrs);
}

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

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

If this is possible then I don't need to check every time

  `mButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isInternetConnected) {
                //show dialog or snackbar
            } else if (!isUserLoggedIn) {
                //show dialog or snackbar
            }else {
                // save data
            }
        }
    });`
  • 1
    Can you make clear what is the issue? – Thirumalai Apr 17 '20 at 09:20
  • 1
    I think this answers your question. https://stackoverflow.com/questions/8575959/android-button-onclick-override – Samir Spahic Apr 17 '20 at 10:22
  • 2
    Please don't tag questions with the android-studio tag just because you use it: the Android Studio tag should **only** be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561), [How do I avoid misusing tags?](https://meta.stackoverflow.com/q/354427/6296561), and [the tagging guide](/help/tagging). Use [android] or other relevant tags instead. – Zoe Apr 17 '20 at 11:50
  • @Thirumalai I want to create custom button so that where I use that button, when a user clicks, first it checks predefined methods then go to on click event. – Android Debugger Apr 18 '20 at 13:45
  • @SamirSpahic I checked the link u provided but its not what I am looking for. I want to create custom button so that where I use that button, when a user clicks, first it checks predefined methods then go to on click event. please tell me if any how its possible – Android Debugger Apr 18 '20 at 13:47

1 Answers1

2

Now i understand what you are asking. Ideal solution for your problem would be AOP (aspect oriented programming). These should help you:

https://medium.com/@jdvp/aspect-oriented-programming-in-android-159054d52757 https://fernandocejas.com/2014/08/03/aspect-oriented-programming-in-android/

This lib should help you:

https://www.eclipse.org/aspectj/

Samir Spahic
  • 542
  • 2
  • 10