Boil an egg app - step by step (step 3 – code)

 

After reviewing part 2 of the tutorial, it’s time for coding. let’s get started.

In the main activity class we’ll have the following things to do:

1. set some private variables to be accessed from different places in the class.

2. bind some variables to views from the xml reviewed in part 2

3. set listeners for the different buttons on the screen

4. handle timer changing and displaying time remained

 

let’s elaborate on this :

1. initialize some variables in the main class, place those prior to the onCreate() method:

    private Button btnSmallEgg,btnMediumEgg,btnLargeEgg;
    private Button btnCookAnEggTimer,btnResetTimer;
    private TextView btnSoftEgg,btnHardEgg;
    private TextView tvTimer,tvTimerLabel;
    private TextView tvChooseEggSize,tvChooseEggType,tvIntro ;
   
    long minutes;
    long seconds;
    long maxTimer=600000;
    long lastKnowTimer=600000;

    final static int boilTimeHardLargeEgg=600000; // 10 minutes
    final static int boilTimeSoftLargeEgg=300000; // 6 minutes

    final static int boilTimeSoftMediumEgg=200000; // 4 minutes
    final static int boilTimeHardMediumEgg=500000; // 4 minutes

    final static int boilTimeSoftSmallEgg=100000; // 4 minutes
    final static int boilTimeHardSmallEgg=250000; // 4 minutes

    private int selected = 0; //default soft eggs
    private int selectedSize=1;

    private CountDownTimer timer;

 

2. bind some variables to the different UI controls from the XML, we’ll extract it to a 

   single method called init()

private void init() {
        tvTimerLabel = (TextView)findViewById(R.id.main_eggs_timer_label);   
        tvChooseEggSize = (TextView)findViewById(R.id.tvChooseEggSize);
        tvChooseEggType = (TextView)findViewById(R.id.tvChooseEggType);
        tvIntro = (TextView)findViewById(R.id.intro);
        btnCookAnEggTimer = (Button)findViewById(R.id.startCookingAnEgg);
        btnResetTimer = (Button)findViewById(R.id.resetCookingAnEgg);
        btnSmallEgg = (Button)findViewById(R.id.btnSmallEgg);
        btnMediumEgg = (Button)findViewById(R.id.btnMediumEgg);
        btnLargeEgg = (Button)findViewById(R.id.btnLargeEgg);
        btnSoftEgg = (TextView)findViewById(R.id.btnSoftEgg);
        btnHardEgg = (TextView)findViewById(R.id.btnHardEgg);       
        tvTimer = (TextView)findViewById(R.id.tvTimer);                       

        setInitialValues();

    }

private void setInitialValues() {
        btnResetTimer.setEnabled(false);
       
        btnSmallEgg.setBackgroundResource(R.drawable.egg_m_not_checked);
        btnMediumEgg.setBackgroundResource(R.drawable.egg_l);
        btnLargeEgg.setBackgroundResource(R.drawable.egg_xl_not_checked);
    }

3. set listeners for the buttons:


//Listeners for all eggs
btnSmallEgg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSmallEgg.setBackgroundResource(R.drawable.egg_m);
        btnMediumEgg.setBackgroundResource(R.drawable.egg_l_not_checked);
        btnLargeEgg.setBackgroundResource(R.drawable.egg_xl_not_checked);           
        selectedSize=0;
    }
});

btnMediumEgg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSmallEgg.setBackgroundResource(R.drawable.egg_m_not_checked);
        btnMediumEgg.setBackgroundResource(R.drawable.egg_l);
        btnLargeEgg.setBackgroundResource(R.drawable.egg_xl_not_checked);
        selectedSize=1;
    }
});

btnLargeEgg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        btnSmallEgg.setBackgroundResource(R.drawable.egg_m_not_checked);
        btnMediumEgg.setBackgroundResource(R.drawable.egg_l_not_checked);
        btnLargeEgg.setBackgroundResource(R.drawable.egg_xl);
        selectedSize=2;
    }
});

// listener for "cook" button
btnCookAnEggTimer.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {                                   

       
        tvTimer.clearAnimation();
        tvTimerLabel.setVisibility(View.VISIBLE);
       
        btnCookAnEggTimer.setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.timer_disabled , 0);
        btnResetTimer.setEnabled(true);
        btnResetTimer.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.timer_red, 0);
        btnCookAnEggTimer.setEnabled(false);               
       
        if (selected==1)
        {
            switch (selectedSize) {
            case 0:                       
                InitializeTimer(boilTimeHardSmallEgg);
                break;
            case 1:
                InitializeTimer(boilTimeHardMediumEgg);
                break;
            case 2:
                InitializeTimer(boilTimeHardLargeEgg);
                break;           
            }
        }
        if (selected==0)
        {
            switch (selectedSize) {
            case 0:                       
                InitializeTimer(boilTimeSoftSmallEgg);
                break;
            case 1:
                InitializeTimer(boilTimeSoftMediumEgg);
                break;
            case 2:
                InitializeTimer(boilTimeSoftLargeEgg);
                break;
            default:
                break;
            }               
        }
        timer.start();

    }
});


// listener for reset timer button
btnResetTimer.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        timer.cancel();
        InitializeTimer(maxTimer);       
        btnCookAnEggTimer.setEnabled(true);
        tvTimer.setText("");
        tvTimerLabel.setVisibility(View.GONE);
        btnResetTimer.setEnabled(false);           
        // Change the icon inside the buttons
        btnCookAnEggTimer.setCompoundDrawablesWithIntrinsicBounds(0,   0,R.drawable.timer_green, 0);
        btnResetTimer.setCompoundDrawablesWithIntrinsicBounds(0, 0,R.drawable.timer_disabled, 0);
        btnResetTimer.setEnabled(false);
    }
});

btnHardEgg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        selected=1;
        btnHardEgg.setTextColor(getResources().getColor(R.color.orange));
        btnSoftEgg.setTextColor(getResources().getColor(R.color.gray));

    }
});

btnSoftEgg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        selected=0;
       
        btnSoftEgg.setTextColor(getResources().getColor(R.color.orange));
        btnHardEgg.setTextColor(getResources().getColor(R.color.gray));
    }
});

4. set timers depending on selections and updating the ui controls with time remaining:

private void InitializeTimer(long lastknowtimer) {
        timer = new CountDownTimer(lastknowtimer, 1000) {

            @Override
            public void onTick(long lastknowtimer) {

                int seconds = (int) (lastknowtimer / 1000) % 60 ;
                int minutes = (int) ((lastknowtimer / (1000*60)) % 60);               
                tvTimerLabel.setVisibility(View.VISIBLE);
                String timeLeft = (seconds<10) ? minutes + ":0" + seconds : minutes + ":" + seconds;
                tvTimer.setText(timeLeft);                               
            }

            @Override
            public void onFinish() {
                tvTimer.setText("finished");
                MediaPlayer mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.chicken);

                try {
                    mPlayer.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                mPlayer.start();
            }
        };
    }

 

 

and that’s it,

patch it all up together and your'e good to go Smile

Comments

Popular posts from this blog

Spinner VS AutoCompleteTextView

Utilizing Http response cache

ORM for android