menu in android.
package com.youthon;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Intent close1 = new Intent(getApplicationContext(),Conatctlist2.class);
startActivity(close1);
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item4:
Intent close2 = new Intent(getApplicationContext(),MainFrame.class);
startActivity(close2);
return true;
// finish();
case R.id.item5:
Intent close = new Intent(getApplicationContext(),Contact.class);
startActivity(close);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item2:
Intent close1 = new Intent(getApplicationContext(),Conatctlist2.class);
startActivity(close1);
case R.id.item3:
Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();
return true;
case R.id.item4:
Intent close2 = new Intent(getApplicationContext(),MainFrame.class);
startActivity(close2);
return true;
// finish();
case R.id.item5:
Intent close = new Intent(getApplicationContext(),Contact.class);
startActivity(close);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
res/layout/activity_main.xml
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"
tools:context=".MainActivity" >
</RelativeLayout>
res/menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item1"
android:title="Home"/>
<item android:id="@+id/item2"
android:title="About-Us"/>
<item android:id="@+id/item3"
android:title="Contacts"/>
<item android:id="@+id/item4"
android:title="Login"/>
<item android:id="@+id/item5"
android:title="Profile"/>
</menu>