What is (View view) in android? - android

Can anyone please explain why should we use (View view), and what does it mean in Android while defining a method.
public void dosomething(View view) {}
Thanks in advance. I'm a beginner, so my questions might seem basic.

Usually View is used as arguments in methods which act as some kind of listener.
For example when you have more than 1 Button in your layout and you set onClickListener on them, you create a method like this:
public void onClick(View view){
}
Here the View is the view on which the user has clicked. So if you have 2 buttons on your layout, you can check which one the user has clicked by using the following code:
public void onClick(View view){
switch(view.getId()){
case R.id.button1: //do something here
break;
case R.id.button2: //do sonething else here
break;
}
}
Hence, View is supplied as an argument when the method is for a listener and the view(Button,Spinner,Switch,etc.) is used to distinguish which view on the layout has been clicked/selected.

Related

Click, Doble click and Hold Button

i have a little question. How can i implement click, doble click and hold in a Button. I really need add this functions to my button. I have AndroidStudio 2,3,3. Thanks you!!
button.setOnLongClickListener and button.setOnClickListener should do the trick for long and single clicks respectively.
For double tap here's what I do in the setOnClickListener.
boolean click=false;
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(click==true)
//DO SOMETHING
new Handler().postDelayed(new Runnable(){
public void run(){
click=true;
}, 1000};
});
Your activity has to implement the following interfaces: View.OnClickListener, View.OnLongClickListener.
When you have your ButtonId defined like this: android:id="#+id/button":
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.button: //what should happen when the button is pressed
break;
}
}
You also have to set the Listeners in onCreate:
setOnClickListener(this);
setOnLongClickListener(this);
The code for onLongClick looks exactly the same.
Double Tap is a bit more complicated, here you can find how to implement double tap.

Why does it used R.id.buttonX in onClick method instead of comparing view to exists buttons?

I have onClick method, for example, in Activity. Why it is a bad aproach to handle such way?
#Override
public void onClick(final View v) {
if (v == mBtnGo || v == mBtnBack) {
handleActionClick();
} else if (v == mBtnNext) {
handleNextClick();
}
}
Supposed to use this logic:
#Override
public void onClick(final View v) {
switch (v.getId()) {
case R.id.btn_go:
case R.id.btn_back:
handleActionClick();
break;
case R.id.btn_next:
handleNextClick();
break;
default:
break;
}
}
Both of the methods will work.
When dealing with multiple clicks inside a single onClick() method
the second method allows you to use a switch statement,
which is faster and far more readable than a chain of if-else statements.
Sometime we don't need keeping a reference of a View. We just want to register for click event like: findViewById(R.id.button).setOnClickListener()
And I also prefer using switch instead of if-else

how to lock method and release it after test

I have tried to lock onclick method after 2 clicks and release it after test/compare betweeen these clicks, I have recyclerview and listener to these click
is there a way to do it?
When you want to lock onClick method, write this code:
int counter=0; //definite it as an instance variable
public void onClick(View view)
{
if(counter<2)
{
swtich(view.getId())
{
case R.id.firstcard:
//do something
break;
case R.id.secondcard:
//do something
break;
...
counter++;
}
}
}
Of course you have to set this in the layout for each card:
android:onClick="onClick"
And when you want to reset the clicks, you have to set:
counter=0;

retrieving the id of the view that was clicked [duplicate]

This question already has answers here:
android: How to get the ID of a textview when clicked?
(2 answers)
Closed 9 years ago.
I'm using MyActivity extends Activity implements OnClickListener{
This activity references around 10+ buttons and has setOnClicklistener(this) method called on every button.
#Override
public void onClick(View v){
//here I need to get the id of the view that was clicked...
//Depending on the button that was clicked different actions need to be called...
//How do I get the ID of the button that was clicked...
}
#Override
public void onClick(View v){
switch(v.getId()){
case R.id.btnCancel:
//your code for onclick of that button
break;
}
you can use following method to get id.
v.getId()
#Override
public void onClick(View v){
int id = v.getId();
if(id == R.id.button_ok){
}
}
The View parameter that is sent to your onClick method is the actual button that was clicked, therefore you can check which one it is, for example:
#Override
public void onClick(View v){
switch(v.getId()) {
case R.id.button_1: ...; break;
case R.id.button_2: ...; break;
case R.id.button_3: ...; break;
...
default: //unknown button clicked
}
}
This is only one option, there are other. Search google for more info.
use :
if(v.getId()==R.id.whatever)
{
// do something
}
or you can even use :
Button btn = (Button)findViewById(R.id.btn);
if(v==btn)
{
// do something
}
but the second one is not recommended.

OnClick listener - one function for all clicks

In my layout xml file I have set the android:onClick attribute for a Button element to a function in my activity. So when I click the button the function is called with a View as its argument. Are there any information in that View argument that has the id of the button being clicked? I'm trying to figure out if I have to have one onClick function for every element or if I can use one function and switch depending on the id of the element being clicked.
switch (v.getID) {
case R.id.A:
.....
}
ohh Apps has the answer all right... just for throughness I have something like so...
case sensitive stuff.... funny how a getID won't work while a getId will be golden... funny how a compiler couldn't do a "sloppy check" and correct such case issues.
like so
View myButton = findViewById(R.id.mybutton);
myButton.setOnClickListener(this);
View myOtherButton = findViewById(R.id.myotherbutton);
myOtherButton.setOnClickListener(this);
public void onClick(View v) {
switch (v.getId()) {
case R.id.mybutton:
//Do something related to the mybutton click
break;
case R.id.myotherbutton:
//Do something related to the myotherbutton click
break;
//chain all Resource ID's here like above....
}
}
you must also not to forget to setup a Onclick listener for every click event before the switch or the case will never get resolved....
//whoo hoo. 8cupsaday android app coming soon!

Categories

Resources