View OnClickListner at Application level Android - android

I am developing an android application where I stuck at some point. The problem is that I want to track the last action performed in app by tracking onClickListner. So is there any way to set OnclickListner for whole application and then track the last event time. Please Suggest me the way to do that. My assumption is to have a class which extends View class and this class should implement onClickListner. Then all of my buttons should set OnClickListner to Object of this class. but my application have more than 100 buttons so it will increase the complexity of the class. one more problem is that all buttons are performing their activity specific operations.doing all operations from one class will increase complexity.
I am looking to capture onclicklistner throughout the app and then log the event time then transfer the event to the activity where onclickListner was implemented.

An idea might be to create your own OnClickListener, say
class LoggingOnClickListener implements OnClickListener {
public void onClick(View view) {
doLog();
}
}
Now you just have to add super.onClick(view) to add logging to the Button clicks
button.setOnClickListener(new LoggingOnClickListener() {
public void onClick(Button button) {
//handle the click
super.onClick(button);
}
}
The code might very well be flawed as I do not have my IDE open, but should just show the general idea.

Related

How can a widely used custom button track which class its onclick is called from?

In my application, I have a custom button that is used in many different custom views.
The behavior differs very slightly depending on which view the button was clicked in, I want to have one #OnClick implementation in my custom button class that takes care of every case in order to avoid having similar code in every custom view class.
Is there a way I can determine in the #OnClick block where the button was clicked from?
It looks something like
public class customButton extends appcompactimagebutton {
//stuff
#OnClick
public void onbuttonclick(){
//handles general behavior
//if (clicked from customViewA) {
//do A stuff
//}
}
}
and then I have customViewA that has a customButton within it, I'm not sure what to put in the if statement, if that's even a proper way to handle something like this
You can use the tag attribute. In your XML layouts, add a line
android:tag="viewname"
to the XML for the button (in code you can do .setTag("viewname"). You can then just modify your onClick to check the tag using this.getTag() and use that to identify where the click came from.

Event Handling in Android (setOnClickListener)

I'm new to Java and Android Development in General. I found a sample which uses the following statement.
sButton.setOnClickListener(this);
in the onCreate() method of the Activity which implements View.OnClickListener where sButton is a Button variable. As far as I understand this register the on click event handler
Later in the sample
public void onClick(View v) {
if(v.getId() == R.id.button_s)
{
//some work
}
this happens.
My question is if it uses the 'this' keyword from inside the activity shouldn't it pass an object of class Activity? If that happens then the Button ID would never match.
I know there are other methods to implement the Button click.
I have a bit of experience with C# and Windows Phone. The procedure there was that the methods were called for the respective buttons without any need for registering them.
Also what is the difference between event handlers and listeners?
Any help would be appreciated! Thanks
The OnClickListener (in this case the Activity instance) is used to declare the behaviour of your application when it consumes click event. However, your application's Main (UI) Thread registers all (UI) events and it will dispatch the appropriate View object as a parameter to your onClick(View v) function.
The snippet you provided - sButton.setOnClickListener(this) simply instructs your application to use the implemented OnClickListener inside your Activity implementation to respond to user clicks. It doesn't forward the this instance as the parameter to the onClick() function, Android OS does that.
In conclusion: The View v parameter in your onClick(View v) function will correspond to the View that the user clicked, regardless of the OnClickListener that has been attached to that View
EDIT: This is (perhaps outdated but) Android's source-code for the performClick() method of the View class. As you can see, inside that method it calls the mOnClickListener.onClick(this) if there's a listener attached, and that's how the view that has been clicked is forwarded to the onClick() method of the appropriate onClickListener object.
One is below
Button btn_stop=(Button) findViewById(R.id.btn_stop);
btn_stop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
}
});
Second is through XML android:onClick="doClick"
below is code for activity
public void doClick(View v)
{
}
When you do this
sButton.setOnClickListener(this);
this is indeed an instance of Activity. But it is declared as
MyActivity extends Activity implements View.OnClickListener
The important part is that it implements the interface that setOnClickListener takes as argument. This is why it works, this is treated as an OnClickListener, regardless of being an Activity or not.
As for the difference between event handlers and listeners, see this question.

android common OnItemClickListener and separate button listeners

In my app I have around 12 activities. The first layout of my activity is a list view. By clicking each list it gets redirected to other activities. I have implemented the Listview with OnItemClickListener and is working good.
In all the other 11 activities, I have a common title bar with a logo and a button named as S. When the user clicks on the S button, I am showing the same list of items in the First activity of my app, within the current activities by splitting the page. Now instead of writing a separate OnItemClickListener for each activity, how to write it once and use in multiple activities?
In the same way I have placed 3 button in include layout and I am using this in various activities, how to write a separate common button action, it can be used in multiple activities.
While #SamirMangroliya's method works, this is an alternate method you can use, and the one I've been using for a while.
Create your listener in another class file (say, MyClickListener.java):
public class MyClickListener implements OnItemClickListener {
// This can be OnClickListener, OnTouchListener, whatever you like.
// Implement your method here:
public onItemClick(...) {
// Your selection process, etc.
}
}
Then in each of your Activity objects, all you have to do is:
myObject.setOnItemClickListener(new MyClickListener());
how to write a separate common button action, it can be used in multiple activities.
you should create one Activity(called BaseActivity),
class BaseActivity extends Activity{
#Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
Button btn1 = (Button)findViewById(r.id.btn1);
.
.
.
//now setonclickListner here...
}
}
then extends it ...class MyActivity extends BaseActivity
It sounds like you should implement ActionBar navigation. For a more detailed description about how to do it the Android way, see the Design guide. To support versions of Android between 1.6 and 2.3, link in the ActionBarSherlock library.

Why is there no getOnClickListener in the Button class? (Android)

Why is there no getOnClickListener in the Button class? I think this is really strange considering there is a getOnFocusChangeListener function. Why make it for the FocusChangeListener and not for the ClickListener?
Added comment:
For those below that are wondering why I need this: We are developing a large application with a lot of viewgroups on the screen. I want to add some code to a button on the screen but not replace the complete OnClickListener. I want to implement a new OnClickListener that will run some code and call the old OnClickListener. But for that I need to retrieve the old one.
I don't know why there is not, but you can do what you want to do by extending the button class:
public class Button extends android.widget.Button implements OnClickListener {
public void onClick(View v) {
/* Your code here...*/
super().onClick(v);
}
}
I think it's a question to Google :D
Why do you need to get a onClickListener back? If you are so desperate, store it in a tag (Views.setTag(...));

Which is better in implementing click listener?

Which is a good practice in implementing click listener and why? Or is there a better way other than the two? Thanks.
First :
sampleButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
// do something
}
});
Second : implement OnClickListener then override onClick method?
The third option is to set the listener directly in your XML layout:
android:onClick="myClickHandler"
and then implement it in your Activity:
public void myClickHandler(View v){
// do something
}
You're technically doing the 2nd thing with the 1st one. The 1st case uses whats called an anonymous class which implements OnClickListener, but since is anonymous, doesn't have a class name and isn't editable from external classes. Explicitably implementing OnClickListener is useful if you expect to use the same onClick functionality in multiple different locations, or if the click code is long
The first approach is used when you want to perform the action only for a particular case, if many click events require the same action then use the second one.

Categories

Resources