well at school every week we are making a calculator each week on a different platform (wp7,javascript,iphone,android), today it's android, so i have a method that receives all the keystrokes and depending on the value of the key my class do something to get the value of the button in c# is the sender parameter , in javascript this , in android??
private OnClickListener leclicke= new OnClickListener() {
public void onClick(View v) {
//get the id of the button that fired the click event
//findViewById(R.id.???);
} };
thank you.
private OnClickListener leclicke= new OnClickListener() {
public void onClick(View v) {
//get the id of the button that fired the click event
int id = v.getId();
} };
then you must check if this view has an id or not using this
if(id == View.NO_ID){
//this view does not have an id
}
else{
//the view has an id
}
Call the method getId()
v.getId();
If you want to use the same OnClickListener for all buttons, then you can do something like this:
Button b1 = (Button)findViewById(R.id.button1);
Button b1 = (Button)findViewById(R.id.button2);
private OnClickListener leclicke= new OnClickListener() {
public void onClick(View v) {
if(v.equals(b1)) {
// handle button 1
} else if(v.equals(b2)) {
// handle button 2
} // etc.
} };
But this is a little clunky. Another thing you can do is specify a separate on click method for each method by setting the on click property for the button in the UI Designer, and then declaring that method in your Activity, e.g. public onClickButton1(View v);
Related
I added the following lines of Code into my OnCreate method.My goal is to assign a button to two functions and to call them up alternately. With the first click the text of the button should be changed and the EditText should be editable. At the second click, the fields should no longer be editable and the button text should change to the first alternative. I have implemented two OnClickListeners and the program structure seems logical to me. Nevertheless, I get an error message; "Cannot resolve symbol onClickListener". What can I do to get the setup described above up and running? Thanks for all responses!
private Button ProfilUpdate;
ProfilUpdate=findViewById(R.id.buttonProfilUpdate);
.
.
.
.
final ProfilUpdate.OnClickListener listener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Profil bearbeiten");
profilVorname.setFocusable(false);
}
};
ProfilUpdate.OnClickListener listener1 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Ă„nderungen speichern");
profilVorname.setFocusable(true);
v.setOnClickListener(listener2);
}
};
ProfilUpdate.setOnClickListener(listener1);
why don't you create a boolean isFirstClick = true , and then check it in the same listener
ProfilUpdate.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFirstClick){
//Do the job for the first click process
isFirstClick= false;
}else {
//Do the job for the second click process
isFirstClick= true;
}
}
};
ProfilUpdate.setOnClickListener(listener);
There can only be one click listener on one view at a time. Use ProfileUpdate.setOnClickListener(listener object). To get the alternate functionality, you can define a Boolean to keep track of the state, for the example, define a class variable at the top Boolean shouldChangeText = true, and in the onClick body in the listener, do something like:
If (shouldChangeText) { // change the text
}
else { // clear the text
}
shouldChangeText = !shouldChangeText
I have dozens of buttons, each of which I want to do the following for:
Button abcdefg = (Button) this.findViewById(R.id.abcdefg);
abcdefg.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
audiotoplay = "abcdefg";
playAudio(audiotoplay);
}
});
In this case is there a simpler way to do this for every button than copying and pasting this code block for every button and just replacing abcdefg with each buttons ID?
In strings.xml create a string array containing as strings the ids of the buttons, like:
<string-array name="buttonids">
<item>abc</item>
<item>def</item>
<item>ghi</item>
</string-array>
and then in your activity, get this array and for each string id get the integer id of the button with getIdentifier().
Then by findViewById() get a reference to each button and set the listener:
String[] buttonids = getResources().getStringArray(R.array.buttonids);
for (String buttonid : buttonids) {
final String name = buttonid;
int id = getResources().getIdentifier(name, "id", getPackageName());
Button button = findViewById(id);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
audiotoplay = name;
playAudio(audiotoplay);
}
});
}
I assume that as in your sample code, you will assign the string id of each button to the variable audiotoplay.
If you add or remove buttons in your app, the only change you have to do is add or remove its string id from the array in strings.xml.
You can try butterknife, https://github.com/JakeWharton/butterknife
#OnClick(R.id.submit1)
#OnClick(R.id.submit2)
#OnClick(R.id.submit3)
#OnClick(R.id.submit4)
void submit() {
// Clicked ...
}
You can always create a class that extends from Button and define that specific behaviour there. After that you need to update your layout .xml to use your new class instead of the default one.
I'm not sure about the use case - but if you need the id you can save that as the button tag and then access it from OnClick.
For example:
public class YourButtonClass extends AppCompatButton {
....
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
audiotoplay = getTag();
playAudio(audiotoplay);
}
});
}
}
in your activity/ fragment:
findViewById<YourButtonClass>(R.id.btn_your_button).setTag("123")
and on your layout:
<this.is.your.package.name.YourButtonClass
android:id="#+id/btn_your_button"
...
/>
The simplest way would be to use tags. Each button in the layout xml should have
android:onClick="buttonClick"
android:tag="abcd1234"
Then you should have a single function in your Activity
public void buttonClick(View v) {
audiotoplay = v.getTag();
playAudio(audiotoplay);
}
You do not have to call any setOnClickListener, as it is set in the xml. Just make sure the buttonClick method is in your Activity class, not Fragment class. You can call it any name you like, I just used buttonClick to differentiate from any other onClick. If you must have it in Fragment, then you would assign a single onClickListener to a variable (or have the Fragment implement onClickListener) with same code, and call setOnClickListener on all buttons to it.
I have 9 buttons on an activity, and want to handle clicking on two buttons -any of them- (respectively not spontaneously), is there any way to do that?
If you create your own click listener as so...
View.OnClickListener listener = new View.OnClickListener() {
private Button firstButton;
public void onClick(View v) {
if(firstButton == null) {
firstButton = (Button) v;
} else {
twoButtonsWereClicked(firstButton, (Button) v);
firstButton = null;
}
}
};
...and implement the method twoButtonsWereClicked to do what you want it to do, then add this listener to the buttons you want this to happen for.
The twoButtonsWereClicked method can pretty much do anything you want.
Hope this helps
my app have action bar on top of windows. Where are some buttons. Buttons count and there functions is changing depending on activity user are.
I want to write a class with methods addFirstButton, removeFirstButton and so on.
So i other classes i want to do this:
MyButtons myButtons = new MyButtons();
myButtons.addFirstButton();
So there is everything alright, but how to create a listener button if i want to do this ?
Normally i would do this:
Button backButton = (Button) customNav.findViewById(R.id.back);
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
But i want that this would be in MyButtons class and method somehow would return a listener to that action.
So any ideas if this possible ?
Thanks.
If you're programming an Action Bar, then you can handle its "buttons" in onOptionsItemSelected(). For more information, see here: http://developer.android.com/guide/topics/ui/menus.html
If you are supporting Android 1.6-2.x, you can make a copy of the ActionBarCompat sample app. It will use some of the same XML flags as >=3.x ActionBar, but not all functionality is emulated. You may also consider using Action Bar Sherlock.
If you want to set and get your onClickListeners, you can. Nothing says you have to instantiate the click listener inside the button. But you'll have to do some bookkeeping. At the least, instantiate the listener outside your button array and pass it in.
Here's how I make a standalone click listener:
Button.OnClickListener mTakePicOnClickListener =
new Button.OnClickListener() {
public void onClick(View v) {
dispatchTakePictureIntent(ACTION_TAKE_PHOTO_B);
}
};
And here's where I attach it to a button (trivial example):
private void setBtnListener(
Button btn,
Button.OnClickListener onClickListener ) {
btn.setOnClickListener(onClickListener);
}
(If you want to see what this function really looks like, it's part of the Capturing Photos sample app.)
But I think you can see how you could use this function internal to MyButtons.
Or the hard way to code:
final Button backButton = null;
final LinearLayout navBar = (LinearLayout) customNav.findViewById(R.id.root);
Button addButton = (Button) customNav.findViewById(R.id.add_button);
addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
if (backButton == null)
{
backButton = new Button(this);
backButton.setText("Back");
backButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Toast.makeText(Action_Bar_TestingActivity.this, "BACK", Toast.LENGTH_SHORT).show();
}
});
navBar.addView(backButton);
addButton.setText("Remove Back button");
}
{
navBar.removeView(backButton);
backButton = null;
addButton.setText("Add Back button");
}
}
});
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
}
}