Aim : Create an application to change
screen color as per the user choice from a menu.
Software Required : Android
Studio
Pre-Requisite : Basic
knowledge of Android Studio
Theory/Logic :
- Here in
this application we have to make one application in which there is one menubar
.
- So when
we click on menubar and select any options from it then according to option
there should be change in a background of application.
- For
that we have to make one menu.xml file and we have to write some logic on .java
file.
Program :
activity_main.xml File :-
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns: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"
android:id="@+id/relative"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/intro"
android:textColor="@color/black"
android:textSize="16sp"
/>
</RelativeLayout>
MainActivity.java File :-
package
com.example.screencolor;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RelativeLayout;
public class MainActivityextends AppCompatActivity{
RelativeLayoutr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
r=(RelativeLayout) findViewById(R.id.relative);
}
public booleanonCreateOptionsMenu(Menu menu){
MenuInflatermymenu=getMenuInflater();
mymenu.inflate(R.menu.mymenu,menu);
return true;
}
@Override
public booleanonOptionsItemSelected(@NonNullMenuItemitem)
{
if (item.getItemId()==R.id.purple){
r.setBackgroundResource(R.color.purple);
}
if (item.getItemId()==R.id.blue){
r.setBackgroundResource(R.color.blue);
}
if (item.getItemId()==R.id.skyblue){
r.setBackgroundResource(R.color.skyblue);
}
if (item.getItemId()==R.id.green){
r.setBackgroundResource(R.color.green);
}
if (item.getItemId()==R.id.yellow){
r.setBackgroundResource(R.color.yellow);
}
if (item.getItemId()==R.id.orange){
r.setBackgroundResource(R.color.orange);
}
if (item.getItemId()==R.id.red){
r.setBackgroundResource(R.color.red);
}
return super.onOptionsItemSelected(item);
}
}
string.xml File :-
<resources>
<string name="app_name">screencolor</string>
<string name="blue">Blue</string>
<string name="green">Green</string>
<string name="red">Red</string>
<string name="orange">Orange</string>
<string name="yellow">Yellow</string>
<string name="skyblue">Sky Blue</string>
<string name="purple">Purple</string>
<string name="intro">Hello , My name is
Dhruvpuri Goswami...</string>
</resources>
colors.xml File :-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="purple">#9400D3</color>
<color name="blue">#0000FF</color>
<color name="skyblue">#076CBB</color>
<color name="green">#00FF00</color>
<color name="yellow">#FFFF00</color>
<color name="orange">#FF7F00</color>
<color name="red">#FF0000</color>
<color name="purple_200">#FFBB86FC</color>
<color name="purple_500">#FF6200EE</color>
<color name="purple_700">#FF3700B3</color>
<color name="teal_200">#FF03DAC5</color>
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
</resources>
mymenu.xml File :-
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/purple"
android:title="@string/purple" />
<item
android:id="@+id/blue"
android:title="@string/blue" />
<item
android:id="@+id/skyblue"
android:title="@string/skyblue" />
<item
android:id="@+id/green"
android:title="@string/green" />
<item
android:id="@+id/yellow"
android:title="@string/yellow" />
<item
android:id="@+id/orange"
android:title="@string/orange" />
<item
android:id="@+id/red"
android:title="@string/red" />
</menu>
Output :
Conclusion :
-
In this practical we learnt that how we can
apply different background color on one layout or application.
-
We also understand basic information about color.xml
and menu.xml file.
-----***-----***-----***-----
No comments: