I am learning android development from Android Developers
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
// Do something in response to button
}
The sendMessage message method contains a parameter 'View view'? What is a View object and what does it do?
Why is it passed to the method as a parameter and where does it come from?
While writing a click event you might need to know which object is clicked.
In android mostly all the UI components will extend View Class.
So you are getting the instance here
public void sendMessage(View view) {
// Do something in response to button
}
In android we can handle click events by two ways
First way
Providing the method in xml itself. For example
android:onClick="sendMessage"
This is how it happens in the example provided by him.
Second way
We can extend an onClickListener in the Activity or Fragment and we should override onClick method.
See the question "existence of parameter (View view)" for further references.
A View object in Android app development is the building block for a user interface. They are used to create things onscreen for a user to interact with.
http://developer.android.com/guide/topics/ui/overview.html
Edit:
In your case, when the Send button is clicked and it calls the sendMessage function, it passed the View object of the Send button (it passes the View of the object that called the method).
The parameter is the clicked button, that's all. It actually says so in the link that you provided :/
Specifically, the method must:
Be public
Have a void return value
Have a View as the only parameter (this will be the View that was clicked)
I am sure you should have understanded the following method, because it is the basic parameter we often used.
public void sendName(String name) {
//some stuff
}
And this method will be called when the sendName() appeared next time. The only difference is the sendMessage will be called when you click the button, as button is a view exactly, so you have to use View class instead of String class.
sendMessage is a method name
View is your class
view is your parameter
check in xml there are declare Sendmessage button or imageview. and set its property to android:onClick="sendMessage";. so send message is not userdefined method but its a listner method on button or imageview click. so there are View view parameter pass in method.
in that case we con not need to create object and findviewbyid and setonclicklistener class..
thats it....
Related
I'm new to android and Java, so I'm having problems with on click method. Here's i have 2 activities main and sub having different layouts.
I want to know is there any method to implement on click action on button view which is not from main activity layout
I can able show button view of sub activity layout but its on click action does not works.
You need to create object of your Activity, By using this Object you are able to use activity method.
For more detail; visit this link. How to use method from another Class?
Use this to call your Button's OnClick Action.
What I normally like to do is set the onClickListener programmatically
myButton.setOnClickListener(v -> handleMyButtonClick());
Since you are new to Java you may not be familiar with lambdas. So your code could also look more like is.
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
// maybe call handleMyButtonClick();
}
};
The onClickListener has to be set after the button has been initialized but before you actually click it.
After a user clicks a button i want to read what's in the TextViews of the activity and then call a method from the presenter to get a return based on the info sent to it.
However i don't know if the presenter should get this data by itself or if i should pass to him from the Activity class when i call one of his methods (this would mean get the data in the TextFields inside the activity class and then send the data as parameters when i call a Presenter method).
I've tried both methods but i don't know which one is the more organised way to do this.
You will pass the string to the presenter wanted method , then do what you want .
button.setOnClickListenr(new View.OnClickListener() {
#Override
public void onClick(View view) {
String str = myTextView.getText().toString();
presenter.doSomething(str);
)}
When using the MVP architecture pattern, you're supposed to have a reference to the Presenter on the View (in this case, the Viewis your Activity). They have very different responsibilities:
Your View should be as dumb as possible, i.e., it should only be responsible for displaying whatever data to the user and collecting user input.
Your Presentershould be responsible for both handling all the data that is displayed on the View and acting as a middleman between the View and the Model. In other words, for each possible user input, there should be a method in the Presenter capable of handling it.
For instance, in your case, the Viewis responsible for handling the contents of the TextView over to the Presenter. Then, the Presenter has to pass those contents down to the Model, which does something with it according to your business logic. When the Model finishes processing, it returns the result to the Presenter, which then prepares the content to be displayed on the View. When the content is ready, the Presenter then sends it to the View.
Knowing all of this, you should be able to answer your question. It would be something like this (pseudo code):
class MyActivity
{
// you should inject this
Presenter myPresenter;
TextView myTextView;
...
myPresenter.doStuff(myTextView.getText().toString());
}
Why you need read what's in the TextViews?
All data show in View should come from Presenter.
Your data should save in your Repository, Presenter can get everything that you want in your Repository.
Dabbling with Android Studio, and working through a few tutorials , but I don't understand a concept. Oh, and new to java too...haha.
Lets say I have a screen, an activity, MainActivity.xml with a button.
MainActivity.xml has an onClick attribute goForIt defined for the button.
In the MainActivity class I have a method goForIt.
This is where the button being clicked will be responded to.
Inside that goForIt method,
I build an Intent to start another activity,
and fire it off by the statement startActivity(intent)
Questions:
Why do I need a listener? (If I do)? The MainActivity.xml is an explicit directive to a specific method. Or is that a "listener"?
What's the role of the manifest in this? The activity is there... but for what purpose? Again, being able to find the class and method is pretty explicit without any need to consult a lookup like the manifest....?
I'm confused by the Activity XML having an explicit attribute to a specific method in the class, and then at the same time, the Listener saying that if the onClick happens, then do something... they are both trying to do the same thing are they not?
You can use listener or you can specify a method in xml to listen to the click on a view. In both you can the view as the parameter.
2.Manifest file helps you for many purpose:
to specify which activity is to be launched first
to get permissions for accessing internet, getting cal logs, using maps etc,
Specifying the theme or label for each activity and so and so ..
3.Both does the same thing. One is an alternative for other.
This question is waaay to wide and should be (probably will be) closed.
But, here goes:
onClick attribute in xml file is a shortcut for creating a listener (the listener in such case gets created behind your back). You either use that, or a listener done by hand.
Manifest has no role in this (pressing the button). But it is necessary to configure the activity so that it starts when the launcher icon is pressed (among many other things).
Android API looks as if it was never properly designed... it just grew and evolved. So yes, there are multiple, confusing ways to do a single thing.
Using
<Button
android:id="#+id/button"
android:onClick="goForIt"/>
and then
public void goForIt(View v) {
startActivity(...)
}
is exactly the same thing that using:
Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(...)
}
});
The first option is just a workaround, a short path to have the same effect.
Internally, it is just doing things the second way - take the button, add a click listener to it, and run it as soon as the user clicks on the button.
When your layout is inflated (after you call setContentView(R.layout.my_layout)) the attribute android:onClick gets internally converted into something like this:
Button b = findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goForIt(v);
}
});
The only difference is that it does it for you.
The manifest has no role in this context.
Why do I need a listener? (If I do)? The MainActivity.xml is an explicit directive to a specific method. Or is that a "listener"?
Think you have 10 buttons where click is required, so Listener can handle all those types of button click.
What's the role of the manifest in this? The activity is there... but for what purpose? Again, being able to find the class and method is pretty explicit without any need to consult a lookup like the manifest....?
From manifest Android ( application manager ) understands things about your app like, which page is 1st or launcher page.
I'm confused by the Activity XML having an explicit attribute to a specific method in the class, and then at the same time, the Listener saying that if the onClick happens, then do something... they are both trying to do the same thing are they not?
yes they are doing same thing, try to use listener for them.
You can invoke findViewById on View as well as Activity. Now I need to invoke it on both, depending on the cases. I want to avoid type checking or null checking and use either of them, but would like to cast the parameter to one type when passing it.
Is there something like a common interface to invike findViewById? Because from the source code it looks like it's just implemented for both, without connection to a super class or interface.
What you can do in your Activity instead is getting decor view, on which you call findViewById.
public void myMethodRequiringView(View view) {
view.findViewById(...);
}
in Activity:
myMethodRequiringView(getWindow().getDecorView());
in Fragment:
myMethodRequiringView(rootView);
If you look at the source of Actvity and Window Android classes, you will see this is exactly what is called in Actviity.findViewById.
I have a function void buttonPress(View v) that is called android:onClick="buttonPress".
I am now implementing an interface to a Pebble app, and I want to call buttonPress() from my receiveData handler.
What do I pass for View? If I remove the requirement, it force closes when called via button or Pebble. If I pass it null from the Pebble handler, it behaves on click, but force closes when Pebble triggers.
I do not need the button, that was purely to test the Android app. But I do need to call this function which does reads a file and then triggers an intent, and I'm not sure what I need to pass as the View, because it's not clear to me what is passed as this parameter if triggered onClick?
In the function void buttonPress(View v) the view that generates the event is passed as parameter. Since multiple views can be mapped to the same function, View v allows you to identify the button that generated the event.
You can pass NULL to that function if you want, and it is completely harmless. After all it is just a function like any other in your class.
Your code must be doing something wrong elsewhere.
you can do something like this :
void buttonPress(View v)
{
doWork();
}
and from your receive handler you can call doWork().
so it remain same as what button press do and what your receive handler do.
put your read file code and whatever else you want to do in doWork method.