Advertisement
Goodiny777

MyAplication/MainActivity

Mar 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.71 KB | None | 0 0
  1. package com.example.goodiny.myapplication;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Color;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.LinearLayout;
  10. import android.widget.TextView;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.     LinearLayout first_layout, second_layout, third_layout;
  14.     TextView countryName_text;
  15.     Button first_btn;
  16.     Context context;
  17.     int click_counter = 0;
  18.  
  19.     //arrays should have the same length
  20.     int[] first_bgColor = {Color.RED, Color.BLUE, Color.WHITE, Color.RED, Color.RED, Color.RED, Color.YELLOW, Color.RED, Color.GREEN, Color.YELLOW};
  21.     int[] second_bgColor = {Color.YELLOW, Color.WHITE, Color.BLUE, Color.WHITE, Color.BLUE, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.BLUE};
  22.     int[] third_bgColor = {Color.RED, Color.BLUE, Color.RED, Color.RED, Color.YELLOW, Color.BLACK, Color.GREEN, Color.BLUE, Color.BLUE, Color.RED};
  23.     String[] country_name = {"Spain", "Israel", "Russia", "Austria", "Armenia", "Egypt", "India", "Netherlands", "Sierra Leone", "Colombia"};
  24.  
  25.     @Override
  26.     protected void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.activity_main);
  29.         //set pointer to objects from main layout
  30.         setPointer();
  31.     }
  32.  
  33.     private void setFlagsAndName() {
  34.         //set parameters to all objects from arrays
  35.         first_layout.setBackgroundColor(first_bgColor[click_counter]);
  36.         second_layout.setBackgroundColor(second_bgColor[click_counter]);
  37.         third_layout.setBackgroundColor(third_bgColor[click_counter]);
  38.         countryName_text.setText(country_name[click_counter]);
  39.         //fist increase counter
  40.         click_counter += 1;
  41.         //if counter more than last element index of array turn it to 0 else save counter with it's current value
  42.         click_counter = click_counter == first_bgColor.length ? 0 : click_counter;
  43.     }
  44.  
  45.     private void setPointer() {
  46.         this.context = this;
  47.         //setting pointers
  48.         first_layout = findViewById(R.id.layout_first);
  49.         second_layout = findViewById(R.id.layout_second);
  50.         countryName_text = findViewById(R.id.countryName_text);
  51.         third_layout = findViewById(R.id.layout_third);
  52.         first_btn = findViewById(R.id.first_btn);
  53.         //listen to click event on first_btn Button
  54.         first_btn.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View view) {
  57.                 //set color and names of countries
  58.                 setFlagsAndName();
  59.             }
  60.         });
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement