I am making a countdown timer in android eclipse and I want to use a Textview from another class.
This is my first class
public class Main extends Activity {
Spinner timerValueSpinner;
Button startButton;
TextView statusTextView;
Countdown timer;
String[] timeValues;
Resources resourcePointer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValueSpinner = (Spinner)this.findViewById(R.id.secondsSpinner);
statusTextView = (TextView)this.findViewById(R.id.timerView);
resourcePointer = getResources();
timeValues = resourcePointer.getStringArray(R.array.seconds_list);
startButton = (Button)this.findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main.this,Timer.class);
startActivity(intent);
if(timerValueSpinner.getSelectedItemPosition() > -1){
int parsedSpinnerValue = 0;
parsedSpinnerValue = Integer.parseInt(timeValues[timerValueSpinner.getSelectedItemPosition()]);
if(parsedSpinnerValue > 0){
if(timer != null){
timer.cancel();
}
timer = new Countdown(parsedSpinnerValue
* Countdown.oneSecond, Countdown.oneSecond,statusTextView);
timer.start();
}
}
}
});
}
This is my Second class and I want to get its statusTextVie and use it in the First(Main) class instead of its own textview so when I click the button the second class will show and the countdown will start from its textview.
public class Timer extends Activity {
TextView statusTextVie;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
statusTextVie = (TextView)this.findViewById(R.id.timerVie);
}
This is my first time asking a question here, so I am sorry if i made some mistakes.
Why not pass the time value from the first activity to second activity via intent and start the thread in second activity ?
First Activity:
public class Main extends Activity {
Spinner timerValueSpinner;
Button startButton;
String[] timeValues;
Resources resourcePointer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValueSpinner = (Spinner)this.findViewById(R.id.secondsSpinner);
resourcePointer = getResources();
timeValues = resourcePointer.getStringArray(R.array.seconds_list);
startButton = (Button)this.findViewById(R.id.startButton);
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Main.this,Timer.class);
if(timerValueSpinner.getSelectedItemPosition() > -1){
int parsedSpinnerValue = 0;
parsedSpinnerValue = Integer.parseInt(timeValues[timerValueSpinner.getSelectedItemPosition()]);
if(parsedSpinnerValue > 0){
intent.putExtra("TimeValue", parsedSpinnerValue);
startActivity(intent);
}
}
}
});
}
Second Activity:
public class Timer extends Activity {
Countdown timer;
TextView statusTextVie;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timer);
Bundle extras = getIntent().getExtras();
int timeVal = extras.getInt("TimeValue", 0);
statusTextVie = (TextView)this.findViewById(R.id.timerVie);
if (timeVal > 0){
if(timer != null){
timer.cancel();
}
timer = new Countdown(timeVal * Countdown.oneSecond, Countdown.oneSecond,statusTextVie);
timer.start();
}
}
}
Related
public class MainActivity extends ActionBarActivity {
Button b1;
TextView tv2;
Integer count ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
b1= (Button)findViewById(R.id.b1);
tv2=(TextView)findViewById(R.id.tv2);
tv2.setText(count);
if(count == 5) {
Intent ii = new Intent(this,Activity2.class );
Bundle bb = new Bundle();
bb.putInt("Count",count);
ii.putExtras(bb);
startActivity(ii);
finish();
}
else
{
Intent iii = new Intent(this,activity3.class);
// Bundle bb1 = new Bundle();
startActivity(iii);
}
}
public void onStart()
{
Bundle b33 = getIntent().getExtras();
count=b33.getInt("count");
tv2.setText(count);
}
in this code i want to count the number of times an activity is opened in this case activity1 will open only 5 times and after that activity3 will open what i m trying is killing the activity1 before killing i m sending the count value to the activity2 and after that i m calling again activity1 and again i m sending the count value to activity1 so the code will be executed from the start itself so again the value will be 1 but i want activity1 to catch the new values of count in oncreate() or in onstart()
and the error is the app is not opening its force closing and in logcat Null-exception error is displayed although i did all the bindings.
try this code at onStart() method
public void onStart()
{
Bundle b33 = getIntent().getExtras();
if(b33 != null)
{
count=b33.getInt("count");
tv2.setText(count);
}
}
change:
Integer count;
to
int count=0;
also change:
tv2.setText(count);
to(in both place onCreate and onStart):
tv2.setText(String.valueOf(count));
Updated code:
public class MainActivity extends ActionBarActivity {
Button b1;
TextView tv2;
int count = 0 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
b1= (Button)findViewById(R.id.b1);
tv2=(TextView)findViewById(R.id.tv2);
tv2.setText(String.ValueOf(count));
if(count == 5) {
Intent ii = new Intent(this,Activity2.class );
Bundle bb = new Bundle();
bb.putInt("Count",count);
ii.putExtras(bb);
startActivity(ii);
finish();
}
else
{
Intent iii = new Intent(this,activity3.class);
// Bundle bb1 = new Bundle();
startActivity(iii);
}
}
public void onStart()
{
super.onStart();
Bundle b33 = getIntent().getExtras();
if(b33 != null)
{
count=b33.getInt("count");
tv2.setText(String.valueOf(count));
}
}
i tried this code and its working.
i have following problem:
I've made a simple android app that adds 1 to an integer every 1000 ms using a handler, and then display this integer.
The problem is that when i start another activity the same thing happens, which would be fine, if that was intended. The mentioned function is not called in the new activity and yet it seems to be. Please look over my code and show me where it went wrong..
MainActivity:
public class MainActivity extends ActionBarActivity {
protected TextView text;
protected int position;
private Handler handler = new Handler();
private int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.text);
position=0;
SetButtonCLickListener();
counter();
}
protected void SetButtonCLickListener() {
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SwitchActivity();
}
});
}
private void counter() {
handler.removeCallbacks(count);
handler.postDelayed(count, 1000);
}
private Runnable count = new Runnable() {
public void run() {
i++;
text.setText("Count: " + i);
handler.postDelayed(count, 1000);
}
};
protected void SwitchActivity() {
if (position == 1) {
finish();
} else {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
}
SecondActivity
public class MainActivity2 extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
text = (TextView) findViewById(R.id.text);
SetButtonCLickListener();
position=1;
}
}
MainActivity2 has an onCreate() method. In the onCreate() method, you call super.onCreate(), which triggers the MainActivity implementation of onCreate(). The MainActivity implementation of onCreate() is where you are starting your counter thing, via its call to the counter() method. Hence, when MainActivity2 starts up, its onCreate() calls MainActivity's onCreate(), which calls counter().
My guess is that MainActivity2 should inherit from Activity, not from MainActivity.
in this app the Content of the TextView should change/update every second with a sleep thread.
The whole process starts when the button is clicked.
Firstable here is the normal code without the threads:
public class MainActivity extends ActionBarActivity {
Button btn;
TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tw = (TextView) findViewById(R.id.tw);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText("1"); //This is the TextView Content, it should update every second with a sleep thread
tw.setText("2");
tw.setText("3");
}
});
}
}
This is the code added ( not working ) sleep threads:
public class MainActivity extends ActionBarActivity {
Button btn;
TextView tw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tw = (TextView) findViewById(R.id.tw);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText("1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tw.setText("2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
tw.setText("3");
}
});
}
}
ThankĀ“s
The problem is that you are block the main UI(sleep) thread thus giving you unexpected result.
You need to use handler for this if you want to update this each second
sample:
public class MainActivity extends ActionBarActivity {
Button btn;
TextView tw;
int incre = 1;
Handler handler;
Runnable run;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
tw = (TextView) findViewById(R.id.tw);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tw.setText(incre++ + "");
handler = new Handler();
run = new Runnable() {
#Override
public void run() {
tw.setText(incre++ + ""); //set the textview text HERE every 1 second
if(incre != 3) //checks if it is not already 3 second
handler.postDelayed(run, 1000); //run the method again
else
incre -= 2;
}
};
handler.postDelayed(run, 1000); //will call the runnable every 1 second
}
});
}
}
Here I have 3 activities: A, B, and C. From Activity A When I click a button it will goes to Activity B. When Activity B loads the countdown timer will start. Again, when I click a button in Activity B it will go to Activity C. Here I Need a Help.
When Activity C starts I need the countdown timer from Activity B to resume.
Again I switch over from Activity C to Activity B the countdown timer should be resumed from Activity C.
Activity A
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.actone);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Act_Two.class);
startActivity(intent);
}
});
}
}
Activity B
public class Act_Two extends Activity{
Button button;
public TextView textView1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.act_two);
textView1=(TextView) findViewById(R.id.textView1);
MyCount counter = new MyCount(61000,1000);
counter.start();
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.acttwo);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, Act_Three.class);
startActivity(intent);
}
});
}
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
//iFallApp app1 = new iFallApp();
#Override
public void onFinish() {
// TODO Auto-generated method stub
setContentView(R.layout.activity_main);
//textView1.setText("done");
}
#Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
textView1.setText(Long.toString(millisUntilFinished/1000));
}
}
}
While switching between the activities, try to pass the current count down time with the help of Bundle.
You should save a value of your CountDownTimer in Activty B and stop the CountDownTimer (using the cancel() method) in Activty B's onPause and start that CountDownTimer with saved value in onResume of Activty B. something like (assuming this is an Activity B code):
#Override
public void onPause() {
// turning off the timer
isWihesCountUpdateTimerNeeded = false;
if (wihesCountUpdateTimer!=null)
wihesCountUpdateTimer.cancel();
super.onPause();
}
#Override
public void onResume() {
super.onResume();
// resuming the timer
isWihesCountUpdateTimerNeeded = true;
totalWishesCount = SharedPrefsHelper.getTotalWishesCount(getActivity());
startWihesCountUpdateTimer();
}
// the timer increases some wishes count
private boolean isWihesCountUpdateTimerNeeded;
private CountDownTimer wihesCountUpdateTimer;
protected static final int wihesCountUpdateTimerDuration=5000;
protected int totalWishesCount;
private void startWihesCountUpdateTimer() {
wihesCountUpdateTimer = null;
if (!isWihesCountUpdateTimerNeeded)
return;
final int duration = wihesCountUpdateTimerDuration;
//Log.i(this, "startWihesCountUpdateTimer() duration: "+duration);
wihesCountUpdateTimer = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {}
#Override
public void onFinish() {
totalWishesCount++;
SharedPrefsHelper.saveTotalWishesCount(getActivity(), totalWishesCount);
startWihesCountUpdateTimer();
}
}.start();
}
I'm trying to implement a traffic signal which will change color form red to green then to yellow. For this I used a button and I'm changing the background of the button to respected color. I'm using CountDownTimer for this purpose. Here is my code:
public class MainActivity extends Activity {
Button button1 = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setBackgroundColor(Color.RED);
while(true){
change(Color.GREEN);
change(Color.BLUE);
change(Color.RED);
}
}
void change(final int color)
{
CountDownTimer ctd = new CountDownTimer(3000, 3000)
{
#Override
public void onTick(long arg0) {}
#Override
public void onFinish() {
button1.setBackgroundColor(color);
}
};
ctd.start();
}
}
But the above code doesn't seems to work, the color of the button is not at all changing. What's the problem in this code?
This worked for me:
public class TestActivity extends Activity {
Button button1 = null;
long timeout = Long.MAX_VALUE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
button1.setBackgroundColor(Color.RED);
change();
}
void change() {
final int[] colors = {Color.GREEN, Color.BLUE, Color.RED};
CountDownTimer ctd = new CountDownTimer(timeout, 3000) {
int current = 0;
#Override
public void onTick(long arg0) {
Log.d("TEST", "Current color index: " + current);
button1.setBackgroundColor(colors[current++]);
if (current == 3)
current = 0;
}
#Override
public void onFinish() {
}
};
ctd.start();
}
}