Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.goodiny.myapplication;
- import android.content.Context;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- public class MainActivity extends AppCompatActivity {
- LinearLayout first_layout, second_layout, third_layout;
- TextView countryName_text;
- Button first_btn;
- Context context;
- int click_counter = 0;
- //arrays should have the same length
- int[] first_bgColor = {Color.RED, Color.BLUE, Color.WHITE, Color.RED, Color.RED, Color.RED, Color.YELLOW, Color.RED, Color.GREEN, Color.YELLOW};
- int[] second_bgColor = {Color.YELLOW, Color.WHITE, Color.BLUE, Color.WHITE, Color.BLUE, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.BLUE};
- int[] third_bgColor = {Color.RED, Color.BLUE, Color.RED, Color.RED, Color.YELLOW, Color.BLACK, Color.GREEN, Color.BLUE, Color.BLUE, Color.RED};
- String[] country_name = {"Spain", "Israel", "Russia", "Austria", "Armenia", "Egypt", "India", "Netherlands", "Sierra Leone", "Colombia"};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- //set pointer to objects from main layout
- setPointer();
- }
- private void setFlagsAndName() {
- //set parameters to all objects from arrays
- first_layout.setBackgroundColor(first_bgColor[click_counter]);
- second_layout.setBackgroundColor(second_bgColor[click_counter]);
- third_layout.setBackgroundColor(third_bgColor[click_counter]);
- countryName_text.setText(country_name[click_counter]);
- //fist increase counter
- click_counter += 1;
- //if counter more than last element index of array turn it to 0 else save counter with it's current value
- click_counter = click_counter == first_bgColor.length ? 0 : click_counter;
- }
- private void setPointer() {
- this.context = this;
- //setting pointers
- first_layout = findViewById(R.id.layout_first);
- second_layout = findViewById(R.id.layout_second);
- countryName_text = findViewById(R.id.countryName_text);
- third_layout = findViewById(R.id.layout_third);
- first_btn = findViewById(R.id.first_btn);
- //listen to click event on first_btn Button
- first_btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //set color and names of countries
- setFlagsAndName();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement