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

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linear">
</LinearLayout>


Tut_2_2Activity.java
package com.tut_2_2;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.LinearLayout;
public class Tut_2_2Activity extends Activity {
/** Called when the activity is first created. */
LinearLayout l;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
l=(LinearLayout)findViewById(R.id.linear);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuInflater mymenu=getMenuInflater();
mymenu.inflate(R.menu.mymenu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==R.id.item1)
{
l.setBackgroundColor(Color.RED);
}
if(item.getItemId()==R.id.item2)
{
l.setBackgroundColor(Color.GREEN);
}
if(item.getItemId()==R.id.item3)
{
l.setBackgroundColor(Color.BLUE);
}
return super.onOptionsItemSelected(item);
}
}


Output



No comments:

Post a Comment