-3
public class Milk extends Activity {

private CountDownTimer countDownTimer;
private final long startTime = 1000 * 60 * 60 * 24 * 7;
private final long interval = 1 * 1000;
public TextView textView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timer);


    textView = (TextView) this.findViewById(R.id.milkTimer);
    countDownTimer = new CountDownTimerActivity(startTime, interval);
    textView.setText(textView.getText() + String.format("%02d:%02d:%02d", startTime / 3600,
            (startTime % 3600) / 60, (startTime % 60)));

    countDownTimer.start();

}

I have this Countdown timer on my Milk.Java page. I want to move the Countdown timer to my

Timer.Java Page which is

public class Timer extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timer);

    }

}

How can I code it so my textView is passed to my Timer page?

AndroidEx
  • 15,524
  • 9
  • 54
  • 50
Cross113
  • 15
  • 1
  • 6
  • You want to start the Timer activity from the Milk activity and pass to it the countDownTimer value ?? – MChaker May 05 '15 at 22:25
  • I have a onclick image button in my main activity page. When that is click it will start the intent in Milk.Java which is the timer. But what i want to do is move that count down timer to my timer page when the timer page is clicked – Cross113 May 05 '15 at 22:30
  • 1
    Minor note: `int * int` will give you `int` even if you would store it later in `long`. So for instance if your value would be `1000 * 60 * 60 * 24 * 30` you would get as result `-1702967296` because of integer overflow. To avoid this use `long * int` which would give as result `long`, so consider rewriting your `1000 * 60 * 60 * 24 * 7` into `1000L * 60 * 60 * 24 * 7` (just in case you would later want to increase this value). – Pshemo May 05 '15 at 22:32

3 Answers3

3

You can pass data from one activity to another activity using intents. Please specify your question if this is not the answer you were looking for.

Intent intent = new Intent(this, Timer.class);
intent.putExtra("EXTRA_TIMERDATA", textView.getText().toString());
startActivity(intent);

http://developer.android.com/training/basics/firstapp/starting-activity.html You can find more information on this link. It's explained in a very clearly and easy to understanding way.

Nike Sprite
  • 476
  • 2
  • 16
3

As @nick-spriet said, you can pass the string of the TextView using this:

Intent intent = new Intent(this, Timer.class);
intent.putExtra("EXTRA_TIMERDATA", textView.getText().toString());
startActivity(intent);

And in your Timer activity you can get it like so:

String timerData = (String) getIntent().getStringExtra("EXTRA_TIMERDATA");
MiguelCatalan
  • 916
  • 10
  • 26
0

As i have answered on your other question, you'll have put some extra on the Intent. But first, you need the Button, and when someone clicks it you start the intent.

int timeLeft = textView.getText();
Intent myIntent = new Intent(mycurentActivity.this, secondActivity.class);
myIntent.putExtra("key", timeLeft);
startActivity(myIntent); //When the user click you should run this line.

On the receiving activity, you put the other textView and you have: call the intent from the previous activity:

Intent myIntent = getIntent();  
String time = myIntent.getStringExtra("key");
textView.setText(time);    

And set this time to your countDownTimer.

Sherline
  • 13
  • 5
George
  • 6,886
  • 3
  • 44
  • 56
  • int timeLeft = textView.getText(); is giving me an error saying i need a character sequence. And also the coutndown timer is in textView so doesn't that make it a string? – Cross113 May 05 '15 at 23:30
  • @Cross113 you have started to learn Android programming now? I strongly suggest that you search for some examples of what you want to do and understand how these textviews and functions work. You seem to be pretty lost after lots of talks... I have tryied my best, friend. – George May 06 '15 at 01:07