GTU Android Program 7 Android Menu Example

      Comments Off on GTU Android Program 7 Android Menu Example

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

ndroid Program 7

Android Menu is the most common functionality used by most of the android apps. Using menu you can avail the most useful functionality to users. Basically there are two ways to add items into menu. 1) using xml file and 2) using programmatically (coding). Here i have used xml file to add menu. using programmatically i have change color of background based on the click.

Jump to SLMC Program 8

Don’t forget to add menu using menu.xml file from menu folder of res, else you can download complete project using link at the bottom. You may be interested in Android Timer Tutorial.

 

Location : res\layout

FileName: activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/mainlayout"
    tools:context="gtu.android.prog07.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="51dp"
        android:text="Menu Example"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="67dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Location : res\menu

FileName: main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="gtu.android.prog07.MainActivity" >

    <item
        android:id="@+id/action_red"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Red"/>
    
    <item
        android:id="@+id/action_green"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Green"/>
    
    <item
        android:id="@+id/action_blue"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="Blue"/>

</menu>

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog07;

/*
* GTU Android Program 7 Understand Android Menu option. Create an application that will change color of the screen, based on selected options from the menu.
*
*/

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.Toast;

public class MainActivity extends Activity {

	View mainlayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mainlayout = (View) findViewById(R.id.mainlayout);
		
		
	}

	@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.
		int id = item.getItemId();
		
		if(item.getTitle().toString().equals("Red"))
		{
			
			mainlayout.setBackgroundColor(Color.RED);
		}
		else if(item.getTitle().toString().equals("Green"))
		{
			mainlayout.setBackgroundColor(Color.GREEN);
		}
		else if(item.getTitle().toString().equals("Blue"))
		{
			mainlayout.setBackgroundColor(Color.BLUE);
		}
		
		
		/*if (id == R.id.action_settings) {
			return true;
		}*/
		return super.onOptionsItemSelected(item);
	}
}

 

Continue Reading…