Android Button internal onClick actions - android

I'm making a custom button which is designed to handle onClick events internally. Here's a simplified version of my implementation:
public class CustomButton extends Button implements View.OnClickListener {
public CustomButton(Context context) {
super(context);
setOnClickListener(this);
}
#Override
public void onClick(View v) {
setText("Clicked!");
}
}
No surprises in the above code! The problem is that the activity/fragment/whatever containing the button could call setOnClickListener(View) which would prevent the onClick(View) method inside the button from receiving the onClick event, thus changing the function of my button in an undesirable way. I though about overriding setOnClickListener(View) to throw an exception if the argument is not this, but I don't want to prevent the button from having external listeners, I just want to add an internal feature.
Which method of Button if any can I override to do extra stuff when the button is clicked without taking away the ability of other classes to listen to onClick events? Basically, which method in Button dispatches OnClick events?

I thought of a solution. I made CustomButton as follows:
public class CustomButton extends Button implements View.OnClickListener {
private OnClickListener externalOnClickListener;
public CustomButton(Context context) {
super(context);
super.setOnClickListener(this);
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
super.setOnClickListener(this);
}
public CustomButton(Context context, AttributeSet attrs, int defStyleAttrs) {
super(context, attrs, defStyleAttrs);
super.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Do all of my stuff
setText("Clicked!");
setBackground(Color.GREEN);
// Pass onClick event to external listener
if (externalOnClickListener != null) {
externalOnClickListener.onClick(v);
}
}
#Override
public void setOnClickListener(OnClickListener l) {
externalOnClickListener = l;
}
}
Basically setOnClickListener() is overriden so that it just stores the passed listener as an instance variable. At construction, the button sets itself as its own on click listener, so that it can receive the on click events, do something, then pass the event on to the stored listener.

Related

Callback with extended EditText

I'm trying to create a custom EditText that provides an onLostFocus event. However, I can't get my head around how I tell the custom class what method to run when the focus is lost.
This is my extended EditText:
public class smtyEditText extends EditText {
public smtyEditText(Context context) {
super(context);
}
public smtyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public smtyEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setFocusChangeListener() {
this.setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// notify the relevant activity, probably passing it some parameters like what instance of smtyEditText triggered the event.
}
}
});
}
}
The intention of the setFocusChangeListener function was that from any given activity I could do something like:
public class AddMeal extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_meal);
EditText etMealName = (EditText) findViewById(R.id.txtmealName);
etMealName.setFocusChangeListener(this.fieldLostFocus)
}
// then
public void fieldLostFocus(eventSource) {
// run some kind of validation on the field text.
}
}
Clearly I'm "code paraphrasing" here. I also get that Interfaces, and some other "EventNotifier" class might be needed. These are the resources I've tried to decipher so far:
http://www.javaworld.com/article/2077462/learn-java/java-tip-10--implement-callback-routines-in-java.html
How to Define Callbacks in Android?
http://www.justinmccandless.com/blog/Setting+Up+a+Callback+Function+in+Android
But for whatever reason I can't crystallize what is needed. Do you have any suggestions on how I can achieve this?
You don't need the inheritance... it only adds an unnecessary layer of indirection. Just add the focus change handler in your activity.
public class AddMeal extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_meal);
EditText etMealName = (EditText) findViewById(R.id.txtmealName);
etMealName.setFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// call method to handle loss of focus
}
}
});
}
// then
public void fieldLostFocus(eventSource) {
// run some kind of validation on the field text.
}
}

android: calling a method each time a button is clicked

i would like to create a class, which extends Button and implement a method, which is alwasy called, when the Button is clicked. But i still want it's OnClickListener to be called.
My Idea is to save the OnClickListener into a private member when the constructor or setOnClickListener is called and then set the OnClickListener to my own OnClickListener. This one would then call my method and the saved OnClickListener.
But i don't see how i can get the OnClickListenr, i only see, how to set it.
Is there a way to acces it?
Or do you have a better idea? (it doesn't matter wheter my method is called before or after the OnClickListener)
I guess you could do this:
public class OnceClickedTwiceRunButton extends Button{
public OnceClickedTwiceRunButton(Context context) {
super(context);
}
public OnceClickedTwiceRunButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public OnceClickedTwiceRunButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private OnClickListener extraClickMethod;
#Override
public void setOnClickListener(OnClickListener newListener)
{
super.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
DefaultClickMethod(v);
if(extraClickMethod != null)
{
extraClickMethod.onClick(v);
}
}
});
extraClickMethod = newListener;
}
private void DefaultClickMethod(View v)
{
//TODO
}
}

Creating custom control with overriding onclick

I am working on an android app and I have a custom GUI component which extends a TextView.
I want to have my custom control do a task when clicked from my custom control class and my overridden onclick method.
For example my class that extends the TextView implements the OnClick listener and writes a log to the log cat.
Then in my activity, I set an onclick listener to my custom control, and this shows a toast notification.
What I want to happen, is when my custom control is clicked, my activities overridden onclick shows the toast and the custom control class on click method also is run to show the log. But I can only seem to get one working or the other, for example, if I don't run myCustom.setOnClickListener(myListener) then the classes onclick is used and does the log, if I set the onClick listener then I only get the toast not the log.
Below is my custom control class
public class NavTextView extends TextView implements View.OnClickListener
{
public NavTextView(Context context) {
super(context);
setOnClickListener(this);
}
public NavTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
public NavTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
setOnClickListener(this);
}
public NavTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.d("NavTextView", "This has been clicked");
}
}
Below is my activities onCreate method
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navTextView = (NavTextView)findViewById(R.id.navTextView);
navTextView.setOnClickListener(mClickListener);
}
Hope this makes sense
A View can only have one OnClickListener. In your NavTextView you are setting it there. If you later call setOnClickListener again, you are replacing the previous listener.
What you can do is override setOnClickListener in your custom View, then wrap the OnClickListener and call both.
public class MyTextView extends TextView implements View.OnClickListener
{
OnClickListener _wrappedOnClickListener;
public MyTextView(Context context) {
super(context);
super.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Log.d("NavTextView", "This has been clicked");
if (_wrappedOnClickListener != null)
_wrappedOnClickListener.onClick(view);
}
#Override
public void setOnClickListener(OnClickListener l) {
_wrappedOnClickListener = l;
}
}

Event for VideoView playback state or MediaController play/pause

I cant seem to find an event that listens for playback state. I am mostly interested in the play/pause state. I am using MediaController which has a Play/Pause button, but I have a secondary button that also controls Play/Pause. Using my custom button, I can play/pause, but if I play/pause using the MediaController play/pause button, I currently have no way to change the image on my custom play/pause button to either play or pause.
Is there an event that I do not know about so I can do some work during play/pause?
This is a very similar question: How to catch event when click pause/play button on MediaController
If you're using the MediaController in combination with a VideoView, it should be relatively easy to extend the latter and add your own listener to it.
The custom VideoView would then look something like this in its most basic form:
public class CustomVideoView extends VideoView {
private PlayPauseListener mListener;
public CustomVideoView(Context context) {
super(context);
}
public CustomVideoView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomVideoView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setPlayPauseListener(PlayPauseListener listener) {
mListener = listener;
}
#Override
public void pause() {
super.pause();
if (mListener != null) {
mListener.onPause();
}
}
#Override
public void start() {
super.start();
if (mListener != null) {
mListener.onPlay();
}
}
public static interface PlayPauseListener {
void onPlay();
void onPause();
}
}
Using it is identical to using a regular VideoView, with the only difference being that we can now hook up our own listener to it.
// Some other code above...
CustomVideoView cVideoView = (CustomVideoView) findViewById(R.id.custom_videoview);
cVideoView.setPlayPauseListener(new CustomVideoView.PlayPauseListener() {
#Override
public void onPlay() {
System.out.println("Play!");
}
#Override
public void onPause() {
System.out.println("Pause!");
}
});
cVideoView.setMediaController(new MediaController(this));
cVideoView.setVideoURI(...);
// or
cVideoView.setVideoPath(...);
// Some other code below...
Finally, you may also declare it in your xml layout and inflate it (as shown above) - just make sure your use <package_name>.CustomVideoView. Example:
<mh.so.CustomVideoView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/custom_videoview" />
You should be able to set your own MediaController.MediaPlayerControl and override pause and start

Android and making `this` a listener for one of its buttons

I have a custom view MyView inflated from my_view.xml (with a corresponding Java class MyView.java).
I override MyView(Context context, AttributeSet attrs) to connect subviews up to members of the class.
Now one of these subviews is a Button, and I'd like for my view to listen for a click on its button before passing this event on to a delegate. However if I declare
this.myButton.setOnClickListener(this);
in the constructor MyView(Context context, AttributeSet attrs) (where this is an instance of MyView) I get a NullPointerException.
Where is an appropriate place in MyClass.java to call this.myButton.setOnClickListener(this);?
%%
Edit. Some code:
public MyView(Context ctx, AttributeSet attrs)
{
super(context, attrs);
this.myButton = (Button) this.findViewById(R.id.my_button);
this.myButton.setOnClickListener(this); // Raises null pointer;'id' is correct.
}
Instead of trying to do the setOnClickListener(this) call in the constructor, do it after the button has been fully initialized. Try moving setOnClickListener(this) so that it's called (indirectly) from the parent activity's onResume method like this:
public class MainMenuActivity extends Activity {
#Override
public void onResume() {
super.onResume();
new MyView(this, attrs).onResume();
}
...
}
public class MyView {
public void onResume() {
myButton.setOnClickListener(this);
}
...
}
I use Roboguice all the time for this sort of thing, and highly recommend it. My button handler code looks like this:
class ButtonHandler implements OnClickListener {
#InjectView(R.id.MainMenuButton)
private Button button;
public void onResumeEvent( #Observes OnResumeEvent onResume ) {
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
doSomethingUseful();
}
}
The problem is I've missed off an inflate in the constructor:
LayoutInflater i = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
i.inflate(R.layout.my_view, this);
This is confusing for me as I'd thought the constructor MyView(Context ctx, AttributeSet attrs) would be called in inflating the view, not the other way around.

Categories

Resources