Menu in Android
Menus are a common user interface component for android applications.To provide a familiar and consistent user experience to the user, you should use the Menu APIs to present user actions and other options in your activities.
Different menu types in android
1.Context Menus
2.Options menus
3.Sub menus
Create Android Project with name as "MenuDemo" with package name "com.etr.menudemo". Create Activity with name as "MenuActivity". This is the code for MenuActivity.
This is code for Setting Activity
This is code for activity_menu.xml
This is code for menu.xml
This is code for AndroidManifest.xml
You can download source code here.
Different menu types in android
1.Context Menus
2.Options menus
3.Sub menus
Create Android Project with name as "MenuDemo" with package name "com.etr.menudemo". Create Activity with name as "MenuActivity". This is the code for MenuActivity.
package com.etr.menudemo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
setting();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setting() {
startActivity(new Intent(MenuActivity.this, Setting.class));
finish();
}
}
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
public class MenuActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_settings:
setting();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void setting() {
startActivity(new Intent(MenuActivity.this, Setting.class));
finish();
}
}
This is code for Setting Activity
package com.etr.menudemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Setting extends Activity {
public void onCreate(Bundle bundle) {
try {
super.onCreate(bundle);
setContentView(R.layout.setting);
} catch (Exception e) {
Log.d("Setting Exception", Log.getStackTraceString(e));
}
}
}
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Setting extends Activity {
public void onCreate(Bundle bundle) {
try {
super.onCreate(bundle);
setContentView(R.layout.setting);
} catch (Exception e) {
Log.d("Setting Exception", Log.getStackTraceString(e));
}
}
}
This is code for activity_menu.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=".MenuActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
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=".MenuActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
This is code for setting.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/>
<TextView
android:layout_gravity="center|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Setting Activity" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=""/>
<TextView
android:layout_gravity="center|center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Setting Activity" />
</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
This is code for AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etr.menudemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.etr.menudemo.MenuActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Setting"></activity>
</application>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.etr.menudemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.etr.menudemo.MenuActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Setting"></activity>
</application>
</manifest>
You can download source code here.
Comments
Post a Comment