Catch all clicks of android application - android

I need to catch all clicks(buttons only) from all activities of App and put only
Log.v("","") method.
Target: instrument for small group testing of devices, and actions of users.
I have different variants of them:
1)
extends activity implements OnClickListener {
....
#Override
public void onClick(View arg0) {
....
2)
button1.setOnClickListener() {
....
3) Inside of xml
android:onClick="method"
Need to find at least for 1) and 2) solution.
What solutions I need to use:
1) Create some kind of global activity class with OnClickListener and extend all my activities.
2) Or create handler with some broadcast messages.
3) Any advices ...

I think the only way to do this would be to create your own subclass of the Button class which would do your required logging before calling the OnClickListener instances for the buttons.
You would then have to use this subclass implementation instead of the standard Button instances in your xml layouts and/or programmatically created Button instances.

Related

Android, should all code be written in an Activity

I am more familiar with iOS development than Android and I am wondering if all code should be written in an Activity rather than having a "model" class.
I have a couple screens each with a few checkboxes and I want them all to behave the same on click, I am trying to figure out how I would do this without writing repeating code in each activity. Thanks!
No you should not. If you are familiar with java, think of an activity as a extension of main with OO added.
In your particular example you can create a class with a method like:
<MethodName>(View <checkboxClickedName>){ //your code here }. and then add this to the checkbox in the XML android:onClick="<MethodName>", you may need the full package path (e.g. com.example.app.)
Note: if some of the commands/objects you need are only available within an activity you should create this in an calss that extends Activity or preferably within the running activity.
You could have a base class that extends activity that contains the methods that you want executed on click (either implemented or abstract). Use this new base class instead of activity when making new activities. In the layout xml, you can set the onclick of each checkbox to be the method in the base activity you want executed.
The best practice would be to use a single activity and switch fragments as if they were your screens. Then, the activity could simply implement the listener interface that the fragments would re-use.
Since you have multiple activities this becomes a little bit harder. To really re-use a single listener, I can think of a single (not so beautiful) option. Create a static listener and lazy load it:
public class MainActivity extends Activity {
private static View.OnClickListener sCheckboxClickListener;
public static View.OnClickListener getCheckboxClickListener() {
if (sCheckboxClickListener == null) {
sCheckboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Work with v
}
};
}
return sCheckboxClickListener;
}
}
And in each of your activities call:
findViewById(R.id.checkbox1)
.setOnClickListener(MainActivity.getCheckboxClickListener());

View OnClickListner at Application level 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.

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.

how to apply master page concept in android application?

I want common logout action to all pages in android application.i have common template that contain logout option. but i repeat the logout function to all activity. how to resolve this problem.
The simplest thing would be to extend a common Activity, as someone else suggested. You are able to extends only from one class, that's why you would do something like this:
public class CommonActivity extends Activity {
// Here you implement log out methods
}
public class ParticularActivity extends CommonActivity {
// Here you put your particular class variables and methods
}
This way, you have the logout functionality in every activity, all you have to do is extends the common one.
Create a Base activity which extends Activity. Write the logic for logout in it. Then extend this class in all activities of the app.
You can either:
extend Activity class and implement the functionality there, your activities would extend this class to provide same functionality
use delegation pattern - create some helper class which contains needed functionality
With first approach you will run into problems when you'll need to extend existing Activities in Android (e.g. ListActivity, TabActivity, etc.)
Check this answer.
I have done this using XML file.
I am just creating runtime view from XML file , and add it to the Activity layout.
I have created method for that
public static void setLoginview(Context ctx, RelativeLayout layout) {
LayoutInflater linflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View myView = linflater.inflate(R.layout.loginheader, null);
layout.addView(myView);
try {
layout.getChildAt(0).setPadding(0, 50, 0, 0);
} catch (Exception e) {
}
}
ctx is the application contetx and layout is the layout in which i want to add that view.

How do I create common code for parts of Android activities?

In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?
The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}
Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers

Categories

Resources