Change the color of the screen, based on selected options

Here I am gonna teach you amazing thing to create an application that will change the color of the screen, based on selected options from the menu.

Read the steps carefully given in the below table:

Steps Description
1 Create a New Project with Basic Activity
2 Modify src/MainActivity.java and add the code
3 Modify layout XML file res/layout/activity_main.xml

Check Also –> UI is such that, one screen has a list of all friends

Content of src/MainActivity.java 

package com.example.kenilvavaliya.mo21;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Not Coding yet...", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        switch (item.getItemId())
        {
            case R.id.red:
                getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FF0000"));
                break;
            case R.id.green:
                getWindow().getDecorView().setBackgroundColor(Color.parseColor("#7FFF00"));
                break;
            case R.id.blue:
                getWindow().getDecorView().setBackgroundColor(Color.parseColor("#0000FF"));
                break;
            case R.id.yellow:
                getWindow().getDecorView().setBackgroundColor(Color.parseColor("#FFFF00"));
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

Content of res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Content of res/layout/content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="119dp"
        android:layout_marginTop="141dp"
        android:text="Click On Three Dots" />
</RelativeLayout>

For changing the different color you can change the color code in MainActivity.java

Output:

For any query comment below in comment box.

Stay Connect with our app: – 

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/mobile-application-devlopmentseit2021/

About easyengineering 38 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

Be the first to comment

Leave a Reply

Your email address will not be published.


*