Understand Menu option.
i. Create an application that will change color of the screen, based on selected options from the menu.



MainActivity.java


package com.example.assignment7;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button b1=(Button)findViewById(R.id.btnRed);
        final Button b2=(Button)findViewById(R.id.btnBlue);
        final Button b3=(Button)findViewById(R.id.btnPink);
        final Button b4=(Button)findViewById(R.id.btnGreen);
        final Button b5=(Button)findViewById(R.id.btnOrange);
        final Button b6=(Button)findViewById(R.id.btnYellow);
        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMyScreenColor(Color.RED);
            }
        });
        b2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMyScreenColor(Color.BLUE);
            }
        });
        b3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMyScreenColor(0xFFFFC0CB);
            }
        });
        b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMyScreenColor(Color.GREEN);
            }
        });
        b5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMyScreenColor(0xFFFF8300);
            }
        });
        b6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setMyScreenColor(Color.YELLOW);
            }
        });
    }
    public  void  setMyScreenColor(int color){
        View view=this.getWindow().getDecorView();
        view.setBackgroundColor(color);
    }
}




activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btnRed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red"
        android:textSize="24sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/btnBlue"
        app:layout_constraintEnd_toStartOf="@+id/btnBlue"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnBlue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue"
        android:textSize="24sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/btnPink"
        app:layout_constraintEnd_toStartOf="@+id/btnPink"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnRed" />

    <Button
        android:id="@+id/btnPink"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="28dp"
        android:text="pink"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/btnYellow"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnBlue" />

    <Button
        android:id="@+id/btnGreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="40dp"
        android:text="green"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnOrange"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnOrange"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="orange"
        android:textSize="24sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/btnGreen"
        app:layout_constraintEnd_toStartOf="@+id/btnYellow"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnGreen" />

    <Button
        android:id="@+id/btnYellow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="yellow"
        android:textSize="24sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/btnOrange"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/btnOrange" />
</androidx.constraintlayout.widget.ConstraintLayout>




Output




To download follow this link : Program 7

No comments:

Post a Comment