Nomar Ramon Nomar Ramon - 2 months ago 17
Android Question

How to start a countdown with a textview from another class?

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.

Answer

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