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");
}
}
});
Related
I wanted to ask what was the significance of creating a switch statement when implementing the onClickListener interface for multiple buttons. Like, we're already calling the setOnClickListener for that particular button by saying button_name.setOnClickListener()
So what's the point of specifying the id's again in the switch statement ? Why is it necessary to do so ? doesn't the point of doing button_name.setOnClickListener() mean - "do what's in here for this button" ?
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Capture our button from layout
Button button = (Button)findViewById(R.id.corky);
Button button2 = (Button)findViewById(R.id.corky2);
Button button3 = (Button)findViewById(R.id.corky3);
// Register the onClick listener with the implementation above
button.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// do something when the button is clicked
// Yes we will handle click here but which button clicked??? We don't know
// So we will make
switch (v.getId() /*to get clicked view id**/) {
case R.id.corky:
// do something when the corky is clicked
break;
case R.id.corky2:
// do something when the corky2 is clicked
break;
case R.id.corky3:
// do something when the corky3 is clicked
break;
default:
break;
}
}
}
What I'm trying to ask is, doing button2.setOnCLickListener() means - "Do what's set by this function for button2" right ? if not then what is the actual purpose/function performed by setOnClickListener() ?
Since you have set clickListeners to 3 different buttons, the method
....onClick(View v){.....
is going to get called when you click any of those 3 buttons. If you click the button1 you do not want to perform task actually assigned for button3. So to check and find out which button was pressed the switch statement is necessary.
Alternatively you could have done this:
corkyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// do something when the corky is clicked
}
});
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// do something when the play is clicked
}
});
so on and so forth...
This way you don't need a switch statement as in this case you know which button is being clicked on, AS you're setting the clickListener AND ALSO specifiying what you wanna do there itself.
It's more of a clean look, a more readable code style structure. If you have multiple widgets who need click listener, by implementing View.OnClickListener interface
and overriding onClick gives a cleaner look.
and inside onClick(View view){
- you can also use if statement but when the number of widgets is more
then it becomes messy, that's why we use switch statement to look cleaner.
}
and
btn.setOnClickListener(this)
// telling the button to pass the interface
so that overridden onClick can listen to this.
But If you don't want that or you have only one widget for click
then just use this, no need to implement and override.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
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 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
The Android code consists of two parts Java and the XML. The XML code has helped display the buttons and switches, what code to write so as to get an output of On and Off from it.
Here is an example of basic code that allows a user to access the camera once a button has been pushed in an app:
Button camera = (Button) findViewById(R.id.button1);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
button1 corresponds to the android id for button in the xml script. You can then use the object that you've set the button to to set the OnClickListener. Inside of the the camera.setOnclickListener brackets is what you want to happen once the button is clicked. Public void onClick is a function that tells android to do something. I tried to put this in basic terms. If you'd like more specifics, there's really good android studio tutorial videos out there. This one is my favorite:
https://www.youtube.com/watch?v=QAbQgLGKd3Y&list=PL6gx4Cwl9DGBsvRxJJOzG4r4k_zLKrnxl
public class MyActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.content_layout_id);
final Button button = (Button) findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
}
or
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#string/self_destruct"
android:onClick="selfDestruct" />
public void selfDestruct(View view) {
// do something
}
if I understand your question you are asking how to react to user click on a button or a switch..
if so, you should create an instance of OnClickListener and implement the onClick() method..
simple example:
Button b = findViewById(R.id.btn_id);
b.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
//do something
}
});
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);