Advertisement
pabloducato

QuizOnlineActivity

Jul 6th, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.66 KB | None | 0 0
  1. package com.example.quiz.AccountActivity;
  2.  
  3.  
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.content.res.ColorStateList;
  7. import android.graphics.Color;
  8. import android.graphics.drawable.ColorDrawable;
  9. import android.net.ConnectivityManager;
  10. import android.net.NetworkInfo;
  11. import android.os.Bundle;
  12. import android.os.CountDownTimer;
  13. import android.os.Handler;
  14. import android.support.annotation.NonNull;
  15. import android.support.annotation.Nullable;
  16. import android.support.v7.app.AppCompatActivity;
  17. import android.view.MotionEvent;
  18. import android.view.View;
  19. import android.widget.Button;
  20. import android.widget.TextView;
  21. import android.widget.Toast;
  22.  
  23. import com.example.quiz.AccountActivity.Model.QuestionFirebase;
  24. import com.example.quiz.R;
  25. import com.google.firebase.database.DataSnapshot;
  26. import com.google.firebase.database.DatabaseError;
  27. import com.google.firebase.database.DatabaseReference;
  28. import com.google.firebase.database.FirebaseDatabase;
  29. import com.google.firebase.database.ValueEventListener;
  30.  
  31. import java.util.Locale;
  32.  
  33. public class QuizOnlineActivity extends AppCompatActivity {
  34.  
  35.     Button beback,b1,b2,b3,b4,accept,reset;
  36.     TextView question, timer, maintitle;
  37.  
  38.     private CountDownTimer countDownTimer;
  39.     private long timeLeftInMillis;
  40.     private static final long COUNTDOWN_IN_MILLIS = 61000;
  41.     private ColorStateList textColorDefaultCd;
  42.  
  43.  
  44.     int total = 1;
  45.     int questiontag = 0;
  46.     int correct = 0;
  47.     int wrong = 0;
  48.     //int accept_er = 0;
  49.  
  50.  
  51.     DatabaseReference databaseReference;
  52.  
  53.     @Override
  54.     protected void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.activity_quiz_online);
  57.  
  58.         timer = findViewById(R.id.timer);
  59.  
  60.         textColorDefaultCd = timer.getTextColors();
  61.  
  62.         b1 = findViewById(R.id.button1);
  63.         b2 = findViewById(R.id.button2);
  64.         b3 = findViewById(R.id.button3);
  65.         b4 = findViewById(R.id.button4);
  66.  
  67.         question = findViewById(R.id.question);
  68.  
  69.         maintitle = findViewById(R.id.maintitle);
  70.  
  71.         accept = findViewById(R.id.accept);
  72.  
  73.         reset = findViewById(R.id.reset);
  74.  
  75.         beback = findViewById(R.id.beback);
  76.         beback.setOnClickListener(new View.OnClickListener() {
  77.             @Override
  78.             public void onClick(View v) {
  79.                 beback.setBackgroundColor(getResources().getColor(R.color.mclaren));
  80.                 Handler handler = new Handler();
  81.                 handler.postDelayed(new Runnable() {
  82.                     @Override
  83.                     public void run() {
  84.                         beback.setBackgroundColor(Color.WHITE);
  85.                         Intent intent = new Intent(QuizOnlineActivity.this, ProfileActivity.class);
  86.                         startActivity(intent);
  87.                     }
  88.                 },50);
  89.  
  90.             }
  91.         });
  92.  
  93.         /*accept.setOnTouchListener(new View.OnTouchListener() {
  94.             @Override
  95.             public boolean onTouch(View v, MotionEvent event) {
  96.                 return false;
  97.             }
  98.         });*/
  99.  
  100.         accept.setOnClickListener(new View.OnClickListener() {
  101.             @Override
  102.             public void onClick(View v) {
  103.                 accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  104.                 maintitle.setVisibility(v.GONE);
  105.                 accept.setText("Potwierdź");
  106.                 beback.setText("Przerwij");
  107.                 b1.setVisibility(v.VISIBLE);
  108.                 b2.setVisibility(v.VISIBLE);
  109.                 b3.setVisibility(v.VISIBLE);
  110.                 b4.setVisibility(v.VISIBLE);
  111.                 question.setVisibility(v.VISIBLE);
  112.                 timer.setVisibility(v.VISIBLE);
  113.                 reset.setVisibility(v.VISIBLE);
  114.                 Handler handler = new Handler();
  115.                 handler.postDelayed(new Runnable() {
  116.                     @Override
  117.                     public void run() {
  118.                         accept.setBackgroundColor(Color.WHITE);
  119.                         updateQuestions();
  120.                     }
  121.                 },0);
  122.             }
  123.         });
  124.  
  125.     }
  126.  
  127.  
  128.  
  129.     private boolean isNetworkAvailable() {
  130.         ConnectivityManager connectivityManager
  131.                 = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  132.         NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  133.         return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  134.     }
  135.  
  136.     private void startCountDown() {
  137.         countDownTimer = new CountDownTimer(timeLeftInMillis, 1000) {
  138.             @Override
  139.             public void onTick(long millisUntilFinished) {
  140.                 timeLeftInMillis = millisUntilFinished;
  141.                 updateCountDownText();
  142.             }
  143.  
  144.             @Override
  145.             public void onFinish() {
  146.                 countDownTimer.cancel();
  147.                 //timeLeftInMillis = 60000; BRON BOZE!!!
  148.                 updateCountDownText();
  149.                 b1.setBackgroundColor(Color.WHITE);
  150.                 b2.setBackgroundColor(Color.WHITE);
  151.                 b3.setBackgroundColor(Color.WHITE);
  152.                 b4.setBackgroundColor(Color.WHITE);
  153.                 updateQuestions();
  154.             }
  155.         }.start();
  156.     }
  157.  
  158.     private void updateCountDownText() {
  159.         int minutes = (int) (timeLeftInMillis / 1000) / 60; //minutes
  160.         int seconds = (int) (timeLeftInMillis / 1000) % 60;
  161.         String timeFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
  162.         timer.setText(timeFormatted);
  163.         if (timeLeftInMillis < 65000 && timeLeftInMillis >= 30000) {
  164.             timer.setTextColor(getResources().getColor(R.color.greengrenade));
  165.         } else if (timeLeftInMillis <= 30000 && timeLeftInMillis >= 15000) {
  166.             timer.setTextColor(getResources().getColor(R.color.timer));
  167.         } else if (timeLeftInMillis <= 15000 && timeLeftInMillis > 0) {
  168.             timer.setTextColor(getResources().getColor(R.color.ferrari)); //RED KOLOR CZERWONY
  169.         } else if (timeLeftInMillis == 0) {
  170.             updateQuestions();
  171.         } else {
  172.             timer.setTextColor(textColorDefaultCd);
  173.         }
  174.     }
  175.  
  176.     private void updateQuestions()
  177.     {
  178.         timeLeftInMillis = COUNTDOWN_IN_MILLIS;
  179.         if(isNetworkAvailable()==false)
  180.         {
  181.             Toast.makeText(QuizOnlineActivity.this, "Brak połączenia z internetem", Toast.LENGTH_SHORT).show();
  182.  
  183.         }
  184.         else if(isNetworkAvailable()==true && total==31)
  185.         {
  186.             Intent i = new Intent(getApplicationContext(), ResultActivity.class);
  187.             i.putExtra("Łącznie pytań:", String.valueOf(questiontag));
  188.             i.putExtra("Poprawne odpowiedzi:", String.valueOf(correct));
  189.             i.putExtra("Złe odpowiedzi:", String.valueOf(wrong));
  190.             startActivity(i);
  191.         }
  192.         else
  193.         {
  194.             databaseReference = FirebaseDatabase.getInstance().getReference().child("questions").child(String.valueOf(total));
  195.             databaseReference.addValueEventListener(new ValueEventListener() {
  196.                 @Override
  197.                 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  198.                     final QuestionFirebase questionFirebase = dataSnapshot.getValue(QuestionFirebase.class);
  199.  
  200.                     question.setText(questionFirebase.getQuestion());
  201.                     b1.setText(questionFirebase.getOption1());
  202.                     b2.setText(questionFirebase.getOption2());
  203.                     b3.setText(questionFirebase.getOption3());
  204.                     b4.setText(questionFirebase.getOption4());
  205.  
  206.                     //timeLeftInMillis = 60000;
  207.                     //updateCountDownText();
  208.                     startCountDown();
  209.  
  210.                     //final boolean[] tmp1 = new boolean[1];
  211.                     //final boolean[] tmp2 = new boolean[1];
  212.                     //final boolean[] tmp3 = new boolean[1];
  213.                     //final boolean[] tmp4 = new boolean[1];
  214.  
  215.                     //tmp1[0]=true;
  216.                     //tmp2[0]=true;
  217.                     //tmp3[0]=true;
  218.                     //tmp4[0]=true;
  219.  
  220.                     //final String[] s1 = {String.valueOf(tmp1)};
  221.                     //final String[] s2 = {String.valueOf(tmp2)};
  222.                     //final String[] s3 = {String.valueOf(tmp4)};
  223.                     //final String[] s4 = {String.valueOf(tmp4)};
  224.  
  225.                     final boolean[] varControlOne = {true};
  226.                     final boolean[] varControlTwo = {true};
  227.                     final boolean[] varControlThree = {true};
  228.                     final boolean[] varControlFour = {true};
  229.  
  230.  
  231.                     if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
  232.                     {
  233.                         varControlOne[0] = false;
  234.                     }
  235.                     else
  236.                     {
  237.                         varControlOne[0] = true;
  238.                     }
  239.                     if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
  240.                     {
  241.                         varControlTwo[0] = false;
  242.                     }
  243.                     else
  244.                     {
  245.                         varControlTwo[0] = true;
  246.                     }
  247.                     if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
  248.                     {
  249.                         varControlThree[0] = false;
  250.                     }
  251.                     else
  252.                     {
  253.                         varControlThree[0] = true;
  254.                     }
  255.                     if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
  256.                     {
  257.                         varControlFour[0] = false;
  258.                     }
  259.                     else
  260.                     {
  261.                         varControlFour[0] = true;
  262.                     }
  263.  
  264.  
  265.                     b1.setOnClickListener(new View.OnClickListener() {
  266.                         @Override
  267.                         public void onClick(View view) {
  268.                             b1.setBackgroundColor(getResources().getColor(R.color.mclaren));
  269.                             if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
  270.                             {
  271.                                 varControlOne[0] = true;
  272.                             }
  273.                         }
  274.                     });
  275.  
  276.                     b2.setOnClickListener(new View.OnClickListener() {
  277.                         @Override
  278.                         public void onClick(View v) {
  279.                             b2.setBackgroundColor(getResources().getColor(R.color.mclaren));
  280.                             if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
  281.                             {
  282.                                 varControlTwo[0] = true;
  283.                             }
  284.  
  285.                         }
  286.                     });
  287.  
  288.                     b3.setOnClickListener(new View.OnClickListener() {
  289.                         @Override
  290.                         public void onClick(View v) {
  291.                             b3.setBackgroundColor(getResources().getColor(R.color.mclaren));
  292.                             if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
  293.                             {
  294.                                 varControlThree[0] = true;
  295.                             }
  296.                         }
  297.                     });
  298.  
  299.                     b4.setOnClickListener(new View.OnClickListener() {
  300.                         @Override
  301.                         public void onClick(View v) {
  302.                             b4.setBackgroundColor(getResources().getColor(R.color.mclaren));
  303.                             if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
  304.                             {
  305.                                 varControlFour[0] = true;
  306.                             }
  307.                         }
  308.                     });
  309.  
  310.                     reset.setOnClickListener(new View.OnClickListener() {
  311.                         @Override
  312.                         public void onClick(View v) {
  313.                             reset.setBackgroundColor(getResources().getColor(R.color.mclaren));
  314.                             b1.setBackgroundColor(Color.WHITE);
  315.                             b2.setBackgroundColor(Color.WHITE);
  316.                             b3.setBackgroundColor(Color.WHITE);
  317.                             b4.setBackgroundColor(Color.WHITE);
  318.                             varControlOne[0] = true;
  319.                             varControlTwo[0] = true;
  320.                             varControlThree[0] = true;
  321.                             varControlFour[0] = true;
  322.                             if(b1.getText().toString().equals(questionFirebase.getAnswer1()))
  323.                             {
  324.                                 varControlOne[0] = false;
  325.                             }
  326.                             else
  327.                             {
  328.                                 varControlOne[0] = true;
  329.                             }
  330.                             if(b2.getText().toString().equals(questionFirebase.getAnswer2()))
  331.                             {
  332.                                 varControlTwo[0] = false;
  333.                             }
  334.                             else
  335.                             {
  336.                                 varControlTwo[0] = true;
  337.                             }
  338.                             if(b3.getText().toString().equals(questionFirebase.getAnswer3()))
  339.                             {
  340.                                 varControlThree[0] = false;
  341.                             }
  342.                             else
  343.                             {
  344.                                 varControlThree[0] = true;
  345.                             }
  346.                             if(b4.getText().toString().equals(questionFirebase.getAnswer4()))
  347.                             {
  348.                                 varControlFour[0] = false;
  349.                             }
  350.                             else
  351.                             {
  352.                                 varControlFour[0] = true;
  353.                             }
  354.                             Handler handler = new Handler();
  355.                             handler.postDelayed(new Runnable() {
  356.                                 @Override
  357.                                 public void run() {
  358.                                     reset.setBackgroundColor(Color.WHITE);
  359.                                 }
  360.                             },50);
  361.                         }
  362.                     });
  363.  
  364.  
  365.                     accept.setOnClickListener(new View.OnClickListener() {
  366.                         @Override
  367.                         public void onClick(View v) {
  368.                             countDownTimer.cancel();
  369.                             //timeLeftInMillis = 60000;
  370.                             //updateCountDownText();
  371.                             //startCountDown();  BRON BOZE
  372.                             accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  373.                             //startCountDown();
  374.                             //updateCountDownText();
  375.                             if(varControlOne[0] == true && varControlTwo[0] == true && varControlThree[0] == true && varControlFour[0]==true)
  376.                             {
  377.                                 correct++;
  378.                                 questiontag++;
  379.                                 Handler handler = new Handler();
  380.                                 handler.postDelayed(new Runnable() {
  381.                                     @Override
  382.                                     public void run() {
  383.                                         //countDownTimer.cancel();
  384.                                         accept.setBackgroundColor(Color.WHITE);
  385.                                         b1.setBackgroundColor(Color.WHITE);
  386.                                         b2.setBackgroundColor(Color.WHITE);
  387.                                         b3.setBackgroundColor(Color.WHITE);
  388.                                         b4.setBackgroundColor(Color.WHITE);
  389.                                         updateQuestions();
  390.                                     }
  391.                                 },0);
  392.                             }
  393.                             else
  394.                             {
  395.                                 accept.setBackgroundColor(getResources().getColor(R.color.mclaren));
  396.                                 wrong++;
  397.                                 questiontag++;
  398.                                 Handler handler = new Handler();
  399.                                 handler.postDelayed(new Runnable() {
  400.                                     @Override
  401.                                     public void run() {
  402.                                         accept.setBackgroundColor(Color.WHITE);
  403.                                         b1.setBackgroundColor(Color.WHITE);
  404.                                         b2.setBackgroundColor(Color.WHITE);
  405.                                         b3.setBackgroundColor(Color.WHITE);
  406.                                         b4.setBackgroundColor(Color.WHITE);
  407.                                         updateQuestions();
  408.                                     }
  409.                                 },0);
  410.                             }
  411.                         }
  412.                     });
  413.                 }
  414.  
  415.                 @Override
  416.                 public void onCancelled(@NonNull DatabaseError databaseError) {
  417.  
  418.                 }
  419.             });
  420.         }
  421.         total++;
  422.     }
  423.  
  424.  
  425.     @Override
  426.     protected void onDestroy() {
  427.         super.onDestroy();
  428.         if (countDownTimer != null) {
  429.             countDownTimer.cancel();
  430.         }
  431.     }
  432.  
  433.  
  434. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement