Having trouble understanding this line of code - java

If anyone wants to break down this code and explain it to me. I'd be thankful.
I run into an error on view(cannot resolve symbol), not sure if I'm supposed to replace that with a specific view ?
Btw, this is an onClick method.
"else if(view.getId()==R.id.Button9){}"
What I understand from this code is it says when "if" "view" whatever the viewid is goes in here ()?? == <--is related to R.id.button9
then run this block of code. Am i even close? Thanks.
A little backstory, I have created an ImageButton and when it's clicked, I'd like to to clear the screen. I've built on onclicklistener and implemented view.OnClickListener on my user public class.
CLEARCANVAS = (ImageButton) findViewById(R.id.button9);
CLEARCANVAS.setOnClickListener(this);
#Override
public void onClick(View v) {
if (view.getId()==R.id.button9);
}

Your View parameter is v, not view.
Change it to v and it will compile:
#Override
public void onClick(View v) {
//if (view.getId()==R.id.button9){
if (v.getId() == R.id.button9){
//handle button9 click
}
}
Another common way to do it is to define a separate click listener for each clickable element, for example:
clearCanvas = (ImageButton) findViewById(R.id.button9);
clearCanvas.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Handle button9 click
}
});

If the error is related with class R, then you need to rebuild the project or else if its still not working for you, you will have to restart the IDE you are using. Basically if you are using Eclipse, I suggest to change to Android Studio as google has deprecated android support to eclipse. Cannot resolve symbol error basically means that the project does not have particular class reference or whatever you are trying to use.

It is like this.
You have 10 buttons from button1 to button10. Everything has an id stored in R.java file to uniquely identify it.
Suppose you want to perform a onClick event for button9. You create a generic onClick event for all the buttons.
#Override
public void onClick(View v) {
//now all the 10 buttons have the same onclick event.
}
Now to differentiate between which button was clicked, you need to use it's id.
#Override
public void onClick(View v) {
if (v.getId()==R.id.button9){
Toast.makeText(context, "Button 9 Clicked!", Toast.LENGTH_SHORT).show();
}
if (v.getId()==R.id.button8){
Toast.makeText(context, "Button 8 Clicked!", Toast.LENGTH_SHORT).show();
}
...
}
So, R.java is a link between your UI objects in the XML and the java code.
You'll need the R.java to identify each object from the UI.
This is automatically done. If you get an error saying cannot resolve R. You might want to re-build or clean your project.

you just need to change your view.getId() with the v.getId().
The view which is passed in the OnClick method is the view which is clicked, so It will check which Id is clicked.

Related

How to automatically execute numbers on dialpads without showing to user by clicking button

Is it possible when any users clicks on button which I created on Application, and code some numbers like *123#. When click happens then it automatically execute this without showing dialpad to User and show its responce in popup like we get.
May be it little bit confusing because I do not know how to clearly explain it. If any person know how to do that in Android Studio.
You can add this a method in your button listener dialToNumberStr("0123456789")
public void dialToNumberStr(String numberToDial){
//String numberToDial = "0123456789"
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:".concat(numberToDial)));
startActivity(dialIntent);
}
your listener should be (using Lambda Expression):
myButton.setOnClickListener(v->{
dialToNumberStr("0123456789");
});
your listener should be (or the old way):
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialToNumberStr("0123456789");
}
});

How was onClick(View v) Automatically executed?

Hi im a begginer on Android development. I knew basic core java. So i have a question on this particular code:
button.setOnClickListener(new View.OnClickListener()
{
#Override public void onClick(View v)
{
// do something when the button is clicked
}
});
I know that this is anonymous class and OnClickListener is an interface. But what i dont understand is the onClick(View v) method, v is the button that was clicked but under the hood how was this method AUTOMATICALLY executed? I mean isnt that to be able to call a method you must first create an object then a method beside it? I just need to understand this concept, thank you.
In simple words when you create a Button object it has some listener objects:
Example:
class Button extends View{
private OnClickListener clickListener;
public void setOnClickListener(OnClickListener clickListener){
this.clickListener = clickListener;
}
}
when you call this:
button.setOnClickListener();
basically you assign the value to clickListener in Button class and then each time you click the button it triggers
clickListener.onClick(this)
and perform your defined stuff.
Your listener is provided to the Button object, and by clicking the button, the Android framework will try to invoke the OnClickListener (if any) by calling the onClick method you provide.
So it is not really automatically. Your action triggers the click, and Android framework calls your onClick.

Support to setup onclick method for my login button

I'm brand new to this whole android coding and im trying to setup a login page for my app. I created the login button and im trying to setup the onClick thing for it but its not working. Ill paste my java file below
package com.example.user_000.appname;
import android.widget.EditText;
import android.view.View;
public class onClick {
EditText username = (EditText)findViewById(R.id.username);
EditText password = (EditText)findViewById(R.id.password);
public void login(View view){
if(username.getText().toString().equals("admin") && password.getText().toString().equals("admin")){
//correcct password
} else {
//wrong password
}
}
}
I am not changing anything in your code just i am providing you a way to get it correctly as you are missing lot of things
Button's onClick() method can be set up basically in 3 ways
Very first from xml side when you declare a button
<Button
android:layout_height="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:onClick="login"/>
And then just make a method in associated java class with the same name as given in XML file like this
public void login(View v)
{
//do whatever you want here
}
Second way you can make an inner implementation anywhere in your class by simply making onClickListner() on Button,but you have to make sure to get button on java side by findViewById() method once you got it then you can set like this
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//do stuff here
}
});
third way is also simple you can implement OnClickListner in your whole class and then you can use it for multiple views clicks but i would say if you have a button only then no need to use this you can use above two methods in this one you can do this
b.setOnClickListener(this);
It will show you an error that methods are not implemented and then you can just use Alt + Enter and onClick() method will be implemented
#Override
public void onClick(View v) {
if(v.getId()==R.id.yourId) {
//do stuff here
}
}
Some useful links are below
Best practice for defining button events in android
Difference between OnClick() event and OnClickListener?
Button Click Listeners in Android

Using buttons on android - newbie

I am a newbie to android development, trying to get buttons working. every time i use this code below, the error message "unfortunately the app has stopped". but when i remove the code the app runs but obviously the buttons do nothing. here is the code ive tried
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.ExerciseButton);
button1.setOnClickListener (new View.OnClickListener(){
public void onClick(View v) {
setContentView(R.layout.exercises);
}
});
}
}
anybody able to help me out there? thanks
Don't try to load another View in the current activity. Navigate to a new ExercisesActivity.
Use:
public void onClick(View v) {
Intent intent = new Intent(ExercisesActivity.this, WcActivity.class);
startActivity(intent);
}
You can't call setContentView anymore after the view has loaded (which it obviously has to receive button clicks). Use a different approach, like showing and hiding views or using a ViewFlipper (see Calling setContentView() multiple times), using fragments (see Fragments) or starting a new activity.
Well, from your code, I see a couple of things:
I am usually familiar to using the onClickListener of the Button class if I want to use it for a button. Makes sense, doesn't it?
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
Second thing:
Start a new Activity (if that is what you want) by using an Intent:
Intent myIntent = new Intent(this, exercises.class);
startActivity(myIntent);
You CAN absolutaly call setContentView on any event, even after the view has loaded
I tried your code in a demo project and it is working fine. So, i think the error will be some where in your layout.(Let me know more if you need more help on this)

Can I reference an OnClickListener's button from inside the listener? (android)

I have an android program where I have multiple buttons using the same OnClickListener, and I want to be able to reference the button's dynamically assigned text from inside the listener. Is there some way to reference the button that was pushed to get its text? I don't want to have to make multiple button-specific listeners that do the same thing.
In your onClick(View v) you can cast it to a button:
#Override
public void onClick(View v) {
Button clickedButton = (Button)v;
// do stuff with it here.
}
use the View which comes as the argument to the onClick(View v)
this can be casted to a button & worked with.
The argument to onClick is the View that originated the click, which will be the button to which you attached the listener. Cast it to Button to get the button object.
Yes, there should be a way.
public abstract void onClick (View v)
You'll notice that the View that was clicked is passed into the onClick() method. So if you have a reference to the View (Button) available (for example, as an instance variable in the Activity) then you can do this:
public abstract void onClick (View v) {
if (v == firstButton) {
//Do some stuff
}
else if (v == secondButton) {
//Do some other stuff
}
}

Categories