Getting a view's listener - java

Is it possible to get the listener that is attached to a view if it exists ? for example if i have a view i would like to get the listener that is listening to that view if there exists one

The only method I could find is View.getOnFocusChangeListener(). So a standard-library View will only tell you about its OnFocusChangeListener; for other listeners, no such getters are part of the class, so if you want to introduce those, you may have to subclass View.
Also, there are some methods that tell you whether a specific type of listener is attached to the View without returning the listener, for example whether a drag event listener, an on click listener or an on long click listener is attached to the View. Note: some of these methods may have side effects, check the docs on that.

No, if you take the OnClickListener for instance, it has protected access in View. Only a subclass could grant public access to the Listener fields, but no framework class does. On the other hand, you have the full responsibility to set the Listener in the first place, so you can set up your own data structures to track that information, if necessary.

Related

MVC - How to show the current int value in a Label in the View class?

So basically I am making a yahtzee game in java using MVC and I need to display the score for each category, which I have done through a Label in the view next to the button. This code is in my View class which puts it on there
onesValueLabel = new Label(""+myController.getOnesValue());
onesValueLabel.setBounds(620,80,30,20);
this.add(onesValueLabel);
The problem I am foreseeing is that once view is pressed, the onesValueLabel will not be updated on screen even though the myController.getOnesValue() is changed.
How can I display the current value of myController.getOnesValue()) so that it can change on the view?
You could implement some kind of observe pattern , where by the view can "observe" changes in the model and update it's state.
This is commonly known as "listeners".
Basically, you provide an interface that describes one or more events that the model might raise (you can have multiple listeners if need be).
Any interested party would implement the interface and pass a reference to, or "register" with, the model.
When some event occurred, the model would fire this event, notifying all the registered listeners that something has happened.
How and what it is up to you.

Why do i have to cast a Button?

This must be a really dumb question because I cant find an answer online.... I know that casting is changing one datatype to another. How is this button ever changing it's data dype? Button button = (Button)findViewById(R.Bla.Bla) Why cant we just write Button button = New Button() And then assign the xml to it another way? Please explain, I'm lost.
You can set a Button to a new button.
But findViewById returns a view. If you want to access any of its Buttonosity, you must cast, otherwise the reference isn't a button. There are times that may be okay, of course.
See In Android You can create the UI Elements in two ways:
1. create UI elements through layouts (.xml) files.
And to use them in java class map them to their corresponding class.
And to do so we have to call method findViewById(int id); which returns the view of that perticuler element with given id.and thus we have to type cast it to respective component.
And thus if you have created a element already in xml why will you create a different object again at java end. so just map the element created with xml file.
2. crate UI elements through java end.
To use this feature use have to create the elements in java with new keywords ex. Button button = new Button(); and then set the all properties on that object.
But But But,
According to android philosophy you should create UI in xml, and write your core business logic in java end. And with this concept you can write neet and clean application code.
But it is only recommended not compulsory at all. now its up to you....
and i think at starting you feel it different but after some time you will start loving it...
Thats the beauty of android.
Thanks. i hope, i answered your question throughly.
Also, remember that Button is a subclass of View. The findViewById() method returns a generic View (any View or subclass of View that you put in a layout file). The cast to Button is saying "It's okay - I know this is a Button, not just a regular View," which allows you to access properties and methods of the Button that aren't available in the View superclass.
final Button callButton = (Button) findViewById(R.id.callButton);
I believe that when finding an XML view using findViewbyId(), it returns the view in the UI, but the returned view must be cast in order to be used as a button within the Java code, and have access to the button methods.
There are ways to create a button in the Java code without specifying it in the XML, but this practice differentiates the UI from the logic.
Plus, declaring UI elements in the XML is better because it is makes the process changing entire layouts easy through usage of of setContentView().
You have two options to create View component in android including Button
1- Define it in a layout XML file and access it using (Button) findViewById(R.id.button)
2- Create it dynamically in the code e.g. Button button = new Button();
both has their own advantages and disadvantages, for example, defining the UI in layout xml makes your Activity concise and and give you more flexibility by separating the UI from the actual code
Dynamic UI creation is useful in many applications that needs to create Views on-the-fly

How to validate input data in a Tab on Tab Change?

I have an app structure like this:
Background: The Parent activity #1 is just holding the tab host, tab widget and is also setting up the 3 tabs to have their content set to the 3 tab activities (tab1, tab2, tab3 activities).
Update: I tried calling my validation method inside
onTabChangedListener in Parent Activity #1 but I got a Null Pointer
Exception. Couldn't really trace it to anywhere. I did
commented/deleted conflicting code but still I am not getting the
bottleneck.StackTrace(PasteBin Link).
Code for Parent Activity #1,Tab#1
Activity
Problem: I want to validate data entered by user in the form field(s) in the individual tab activities onTabChanged event but I am unable to set more than a single setOnTabChangedListener. Am I missing something here? The listener(s) are set in their own tab# activities under oncreate method. Apart from trying the above technique, I had tried setting up the listener in onResume() under the main Parent activity #1. But the on Resume() method was never invoked. I got a null pointer exception too.
Idea behind validation being: I want that while the user is changing tabs, the data should be validated before he can skip over a tab. So, ineffect I would require tab#1 to validate data in a event similar to onTabChanged if tab#2/tab#3 is selected. Also, this would apply if current tab#2 is selected and user selects tab#1/tab#3
Any advice will be appreciated..Thanks for reading..
I want to validate data entered by user in the form field(s) in the
individual tab activities onTabChanged event but I am unable to set
more than a single setOnTabChangedListener.
There is no need for a second OnTabChangeListener and even if you could set it it wouldn't help you. As you constructed the code you need access to the child activities. You can do this by using one of the answers in this question. The problem is, that those answers, except the accepted one, use deprecated methods.
My method, that I proposed in the comments is to have a static boolean field in each of the child activities used as tabs and let all of your activities update that boolean flag whenever there is a change of state of the views in those activities(if you check a CheckBox, enter something in an EditText etc). Then you can simply check the flag for the desired child activity in the OnTabChangeListener. My method should work but your code is a bit messy so you would have to modify it quite a bit.
I had tried setting up the listener in onResume() under the main
Parent activity #1. But the on Resume() method was never invoked. I
got a null pointer exception too.
It's normal that you get a NullPointerException with your code as I haven't seen where you initialize the references to the child activities that you use in the OnTabChangeListener.
Also:
Don't use TabActivity. It's been deprecated in favor of the Fragments framework which is more flexible. Those fragments could help you because, I think you want to stop changing the tabs if the validation of the current page fails and the OnTabChangeListener might come a bit late for that(but I may be mistaken about what you want).
As a side note, use equals in your code to test String equality and not ==.

Java "external on event call"

I am new to Java and I started to learn it on Android platform, I know its not good to start learning language on emulation of mobile platform, but anyway....
What I woud like to ask about java, is "external" calling of some methods. I mean, often in program, or tutorials, you just ovveride some method, and it gets run autmatically based on some action.
This is very nice actually, I really like it, but I would like to know how this this implemented. Does JVM has to implement those, or are they user-defineable somehow?
For axample on Android are methods surfaceCreated(),surfaceDestroyed() which are called on respective event, and you than can handle it. The similiar are button click handling, and many more events.
I would just like to know how this is implemented, becouse, for example in C you have to manually check wheather some action happened or not. And you are limited by data provided by OS. So, does JVM has predefined actions it can call, or can you manually somehow tell it to do somthing based on somthing? I know this is strange question, but in fact its so strange to me I cannot explain it better. Maybe you can understand my not-knowing if you knew I programmed mainly for MCU in C, so this behavior is strange to me. But I like it.
This is called Event-Delegation Model.
On occurrence of any event if listeners are registered, Proper delegates are called.
keep in mind that thing everything is oops in this and will be dealt in terms of classes and objects
We can understand this from a very simple Example say of a button click.
Consider i make this class
class MyButtonClickListener implements OnClickListener
{
public void onClick(View v)
{
//do something on button click
}
}
Now see this class is implementing a interface. This class has to provide body to the empty method of interface to implement it. else the code will not compile.
This ensures that every object of this class has a body of onClick Method. Now let us register this to listen our button click.
say my Button is button01
button01.setOnClickListener(new MyButtonClickListener());
now consider object button01 has a list maintained in it somewhere which has a address of object to do something later( new MyButtonClickListener() in our case).
now the layout managers are coded in a way that on a occurrence of a event (say button click) send this event to the object listener list to perform further action.
this will be happened in the fashion say when button is clicked then buttons list of listener is checked if it is found not null that means there is a listener. now on the reference found in the list , onClick Method is called. specifically onClick is called because we called the setOnCLickListener to set the listener. if you will check the code for this method. you will found method is receiving OnClickListener reference. this is a object of a class that implements the OnClickListener interface so must have provided a body to onClick method.
and thus this delegation is performed. this is simply oops. i hope i am able to explain it to a good level.
you ask about two distinct things:
some methods that you can override, which are called when some action happens (onResume(),...). They are always called (by the runtime/framework), and when your class overrides them, your implementation of that methods is called. But somewhere in the code is an actual call to this method. These are called virtual methods.
In the button click events you subscribe to are similar, but that is event-driven programming. When you subscribe to a button click event, for example
foo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
... do stuff
}
}
the foo object saves the OnClickListener somewhere to its internals. When the button is clicked, it looks in its internals if it has any OnClickListeners saved, and if so, it invokes onClick() method in each of them.

Intercepting clicks from subview in Android

My application has a custom view which contains other custom views of a different type. The subviews have their own click listeners (which I can't change, as these are in 3rd party libraries). How can I intercept a user's click at the level of my view to do some processing, and then pass the click on to the proper subview?
Justin, you can play with dispatchTouchEvent() or onInterceptTouchEvent().
I'm not entirely sure about this, but in Java what I would do is define various subview objects in my main view and simply send those to the draw/paint function. That way I'd only have ONE click listener which would be in the view anyways.
Can you do something like that in Android?

Categories

Resources