Demo entry 6687195

main_activity timer

   

Submitted by anonymous on Dec 26, 2017 at 22:17
Language: Java. Code size: 2.8 kB.

package com.example.tobi.guitimer;

import android.databinding.DataBindingUtil;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.tobi.guitimer.databinding.ActivityMainBinding;

public class MainActivity extends AppCompatActivity {

    //Feature related
    boolean isCounting = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this,R.layout.activity_main);

        //Feature related
        final GuiTimer guiTimer = new GuiTimer(this);
        binding.setGuiTimer(guiTimer);

        //TODO: Fix countdown error + implement progressbar!

        //Get UI Elements
        final EditText txtEditTime = (EditText)findViewById(R.id.txtEdit_time);
        final Button btnStartStop = (Button)findViewById(R.id.btn_Start);
        final TextView txtViewRemaining = (TextView)findViewById(R.id.txtView_remainingTime);
        final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progress_time);

        //Event Listeners
        btnStartStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isCounting){ //User presses stop button
                    isCounting = false;
                    guiTimer.stop();
                    btnStartStop.setText(R.string.btn_Start);
                }else{ //User presses start/mute button
                    if(guiTimer.isRinging()){ //if User presses mute button
                        guiTimer.mute();
                        btnStartStop.setText(R.string.btn_Start);
                    }else{ //User presses start button
                        isCounting = true;
                        guiTimer.start();
                        btnStartStop.setText(R.string.btn_Stop);
                    }
                }
            }
        });

        guiTimer.setTimeLimitReachedListener(new GuiTimer.TimeLimitReachedListener() {
            @Override
            public void onLimitReached() {
                guiTimer.ring();
                isCounting = false;
                btnStartStop.setText(R.string.btn_Mute);
            }

            @Override
            public void onProgressChanged(int value){
                Log.d("[TW]",Integer.toString(value));
                progressBar.setProgress(value);
            }
        });
    }
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).