GTU DE Sem6 – 3360704 – 7 – Create an application that will change color of the screen based on selected options from the menu

GTU DE Sem6 – 3360704 – 7 – Create an application that will change color of the screen based on selected options from the menu

 GTU DE Sem6 - 3360704 - 7 - Create an application that will change color of the screen, based on selected options from the menu

This Android example explains how we can add menu options to any Activity along with how to manage Click event of the Menu. Menu is the most common input options to display about us, feedback and rating kind of options, here I have taken example of the Menu to change background color of the Activity.

Jump to Android Program 8

FileName:MainActivity.java

/*
 * 
Mobile Computing and Application Development : 3360704

GTU DE Sem6 - 3360704 - 7 - Create an application that will change color of the screen, based on 
selected options from the menu

*/

package gtu.de.mcad.ex07;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;

public class MainActivity extends Activity {
	View view1;

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

		view1 = (View) findViewById(R.id.mainactivity);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.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.
		if(item.getTitle().equals("Red"))
		{
			view1.setBackgroundColor(Color.RED);
		}
		else if(item.getTitle().equals("Green"))
		{
			view1.setBackgroundColor(Color.GREEN);
		}
		else if(item.getTitle().equals("Blue"))
		{
			view1.setBackgroundColor(Color.BLUE);
		}
		
		return super.onOptionsItemSelected(item);
	}
}

 

FileName: activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/mainactivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Hit Menu Button"
        android:gravity="center_vertical|center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

 

FileName for Menu: main.xml

<menu 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"
    tools:context="gtu.de.mcad.ex01.MainActivity" >
    <item android:id="@+id/red" android:title="Red"></item>
    <item android:id="@+id/green" android:title="Green"></item>
    <item android:id="@+id/blue" android:title="Blue"></item>

</menu>

 

Output 1

GTU DE Sem6 - 3360704 - 7 - Create an application that will change color of the screen, based on selected options from the menu

 

Output After Click on Red Menu Option

GTU DE Sem6 - 3360704 - 7 - Create an application that will change color of the screen, based on selected options from the menu

 

Output After click on Green Menu Item

GTU DE Sem6 - 3360704 - 7 - Create an application that will change color of the screen, based on selected options from the menu

 

Output After Click on Blue Menu Item

GTU DE Sem6 - 3360704 - 7 - Create an application that will change color of the screen, based on selected options from the menu

Leave a Reply