I'm working on simple 'Click Countdown' application, which basically has an imagebutton function, which when pressed, displays the number of clicks from 10 to 9, 8, 7,... to 0. I have an issue finding how to change an imagebutton and stop counting when number of cliks will be 0.
This is the code I have so far:
package com.example.testapp;
import com.example.testapp.R;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.ImageButton;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
ImageButton button1;
TextView textView1;
int counter = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imageButton;
imageButton = (ImageButton) findViewById(R.id.button1);
textView1 = (TextView) findViewById(R.id.textView1);
imageButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
counter--;
textView1.setText(""+ counter);
}
});
}
}
Change your ClickListener to the following:
public void onClick(View v) {
if (counter >= 0) {
counter--;
textView1.setText("" + counter);
} else if (counter == 0){
button1.setImageResource(R.id.yourdrawable);
counter--;
}
}
Also make sure to actually set button1:
button1 = (ImageButton) findViewById(R.id.button1);
You need to check the value of counter for the zero condition. For example, inside your onClick method:
if (counter == 0) {
//do something else, or do nothing
} else {
counter--;
textView1.setText(""+counter);
}
Related
This code is suppose to display "Hello World" + a counter after each time you press the button. For example: "Hello World!" to "Hello World 1". It currently displays "Hello World!" to "1".
Code:
package com.example.victornguyen.updatedengrassignment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView showValue;
int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showValue = (TextView) findViewById(R.id.hello);
}
public void button(View view) {
counter++;
showValue.setText(Integer.toString(counter));
}
}
Change the last line to:
showValue.setText("Hello World "+Integer.toString(counter));
button.setOnClickListener(new OnClickListener() {
int counter = 0;
public void onClick(View v) {
counter ++;
showValue.setText(" Hello World "+counter);
}
});
hope this will help
I have tried Everything to my knowledge and nothing seems to work. Any help would be appreciated.
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class activity_main extends Activity {
TextView txtCount;
Button btnCount;
int count=0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtCount= (TextView) findViewById(R.id.textView);
txtCount.setText(String.valueOf(count));
btnCount= (Button)findViewById(R.id.button);
btnCount.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
count++;
txtCount.setText(String.valueOf(count));
}
});
}
}
}
No errors appear at any point and the program works without errors; the button clicks and does not crash, but no click is counted numerically in the textView.
you onClick() listener is wrong.
try this one
btnCount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
txtCount.setText(String.valueOf(count));
}
});
I'm working on a simple 'Click Countdown' application, which basically has an imagebutton
function. When pressed it displays the number of clicks from 10 to 9, 8, 7,... to 0.
I have a problem, when I close the application the number of clicks starts again from
10. I wrote somethink, but it doesn´t work and say: Cannot refer to a non-final variable prefsEditor inside an inner class defined in a different method.-(prefsEditor) Can someone help me please?
This is the code I have so far. I there mistake?
package com.example.testapp;
import com.example.testapp.R;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ImageButton;
import android.view.View;
import android.widget.TextView;
import android.view.View.OnClickListener;
import com.google.ads.AdRequest;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import com.google.ads.AdView;
public class MainActivity extends Activity {
ImageButton button1;
TextView textView1;
int counter = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor prefsEditor = prefs.edit();
ImageButton imageButton;
imageButton = (ImageButton) findViewById(R.id.button1);
textView1 = (TextView) findViewById(R.id.textView1);
button1 = (ImageButton) findViewById(R.id.button1);
imageButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
if (counter >= 1) {
counter--;
textView1.setText("" + counter);
prefsEditor.putInt("counter", counter);
prefsEditor.commit();
} else if (counter == 0){
button1.setImageResource(R.drawable.image2);
counter--;
prefsEditor.putInt("counter", counter);
prefsEditor.commit();
}
}
});
}
}
make prefsEditor final
final SharedPreferences.Editor prefsEditor = prefs.edit();
I got this code from a website for chronometer i want to run the chronometer when my activity starts and i want to recive the time running in seconds in a integer value like
myclock = mChronometer.get vaule (example idea);
or anyother methods that can get value from chronometer directly
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
public class StopWatch extends Activity {
Chronometer mChronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button;
mChronometer = (Chronometer) findViewById(R.id.chronometer);
// Watch for button clicks.
button = (Button) findViewById(R.id.start);
button.setOnClickListener(mStartListener);
button = (Button) findViewById(R.id.stop);
button.setOnClickListener(mStopListener);
button = (Button) findViewById(R.id.reset);
button.setOnClickListener(mResetListener);
}
View.OnClickListener mStartListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.start();
}
};
View.OnClickListener mStopListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.stop();
}
};
View.OnClickListener mResetListener = new OnClickListener() {
public void onClick(View v) {
mChronometer.setBase(SystemClock.elapsedRealtime());
}
};
}
Thank u all in advance for helping me
here is the link to the orginal site http://www.edumobile.org/android/android-development/stop-watch-example/
I wrote the following code and compiled it but when I run the application, I get the error android the application has stopped unexpectedly force close eclipse. I thought this is because I didn't initialize the Button and TextView objects but when I initialize them, I get missing token ";" error. What is the reason for this error.
package com.umer.first.project;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class StartingPoint extends Activity {
int counter;
TextView display;
Button add, sub;
//add= new Button(this);
//sub=new Button(this);
//display=new TextView();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
add= (Button) findViewById(R.id.aButton);
sub = (Button) findViewById(R.id.sButton);
display= (Button) findViewById(R.id.tvButton);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter++;
display.setText("The total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
counter--;
display.setText("You counter is " + counter);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_starting_point, menu);
return true;
}
}
Display is a TextView, you can't cast it to a button.
display= (Button) findViewById(R.id.tvButton);
2. You have declared display as TextView,but initialized as Button, thats a Casting Exception .
display= (Button) findViewById(R.id.tvButton); ///// Wrong.
Must be.
display= (TextView) findViewById(R.id.tvButton);
1. Do the declaration of the Views as instance variables before onCreate() Method, then initialize them in the onCreate().