Change button text before function call in Android Studio - java

I am designing a simple app in which myfunction() is called when the start button is pressed. myfunction() takes a few seconds to complete. Right now when I press the Start Button, myfunction() runs for a few seconds and after it is complete, the text of the button changes to "Stop". I want the text of the button to change to "Stop" as soon as the "Start" Button is pressed and before the function is called.
This is my first time working with Android Studio and Java. Any help is appreciated.
I've attached the function which is called when the button is pressed.
public void buttonClick(View v) {
Button tv = (Button)findViewById(R.id.button1);
if(tv.getText() == "Start") {
tv.setText("Stop");
myfunction();
}
else {
tv.setText("Start");
}
}

Don't use tv.getText()=="Start" use tv.getText().equals("Start") like this...
public void buttonClick(View v) {
Button tv = (Button)findViewById(R.id.button1);
if(tv.getText().equals("Start")) {
tv.setText("Stop");
myfunction();
}
else {
tv.setText("Start");
}
}

to use the equals, do something like :
public void buttonClick(View v) {
Button tv = (Button)findViewById(R.id.button1);
if(tv.getText().toString() == "Start") {
tv.setText("Stop");
myfunction();
}
else {
tv.setText("Start");
}
}

Related

2 OnClickListeners for one Button cannot resolve the OnClick Listener

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

Code seems to stop xml layout from showing in Android Studio

The problem is that I have an Android app that doesn't seem to show the xml layout when I put this while loop into the class file. The loop is as follows:
while(!clicked){
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
clicked = true;
letterTapped = 0;
}
});
}
The whole project works completely fine without it so I'm pretty sure that there must be something wrong with the loop that I am overlooking.
If you want me to put any other bits of code up here I will be more than happy to.
To stop listening as soon as the button is pressed, you can use this code:
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
letterTapped = 0;
// Ignore further clicks
button_a.setOnClickListener(null);
// Disable button so the user knows that he can't click again
button_a.setEnabled(false);
}
});

Creating buttons and separating there functionality android

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");
}
}
});

Cancel Button During Countdown

Quick question here, I have the following code working for the most part, but I would like for the button to go back to it's original state if it is clicked during the countdown. Any suggestions?
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new CountDownTimer(4000, 1000) {
#Override
public void onFinish() {
button.setText("SENT");
}
#Override
public void onTick(long sec) {
button.setText("CANCEL (" + sec / 1000 + ")");
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cancel();
}
});
}
}.start();
}
});
Before the button is ever pressed, it will say "Push Me." Once pressed, the countdown will begin and the button's text will change each second (CANCEL (3), CANCEL (2), CANCEL (1)) and after the countdown the button will update its text to "SENT." If the button is pressed during the countdown (onTick), it will cancel the countdown. I would like to know how to make it revert to the "Push Me" state, and basically allow it to be pressed again and begin a new countdown. Any help would be appreciated!
So you should not have a new onClickListener inside of the timer because that uses a lot more memory then is needed, and may cause unexpected results. Instead I would suggest using a boolean value for if the timer is on, and use your existing button's onClickListener. Make sure that your timer is declared globally if you are using this. Here is an example of how that might work:
button.setOnClickListener(new OnClickListener {
if(timerIsOn){
if(timer != null){
timer.cancel();
timerIsOn = false;
}
}
else{
timerIsOn = true;
//start the timer and do whatever else
}
}

get the button that fired the event

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);

Categories