Perbedaan Context Menu dengan Option
Menu adalah Context menu biasanya
menampilkan menu dengan aksi menge-click menu tersebut, sedangkan Option Menu adalah menampilkan menu
dengan aksi menge-click / menekan
tombol Menu.
Berikut kita akan coba membuat Options
Menu dan Contex Menu, buatlah project dengan ketentuan berikut:
Project Name :
MenuLayout
Build Target : Android 4.2.2
Aplication name : menulayout
Package name :
com.immobulus.menulayout
Activity : menulayout
Buatlah file xml di res/layout dengan
nama file menu.xml.
menu.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"
>
<Button android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text =
"Hello, Tampilan Menu!"
/>
</LinearLayout>
menulayout.java
package com.immobulus.menulayout;
import
android.app.Activity;
import android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.widget.Button;
import
android.widget.Toast;
public class menulayout extends Activity
{
@Override
public void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Button btn = (Button)
findViewById(R.id.btn1);
btn.setOnCreateContextMenuListener(this);
}
private void CreateMenu(Menu
menu)
{
menu.setQwertyMode(true);
MenuItem mnu1 = menu.add(0, 0, 0, "Item
1");
{
mnu1.setAlphabeticShortcut('a');
mnu1.setIcon(R.drawable.icon);
}
MenuItem mnu2 = menu.add(0, 1, 1, "Item
2");
{
mnu2.setAlphabeticShortcut('b');
mnu2.setIcon(R.drawable.icon);
}
MenuItem mnu3 = menu.add(0, 2, 2, "Item
3");
{
mnu3.setAlphabeticShortcut('c');
mnu3.setIcon(R.drawable.next);
}
MenuItem mnu4 = menu.add(0, 3, 3, "Item
4");
{
mnu4.setAlphabeticShortcut('d');
}
menu.add(0, 3, 3, "Item
5");
menu.add(0, 3, 3, "Item
6");
menu.add(0, 3, 3, "Item
7");
}
private boolean
MenuChoice(MenuItem item)
{
switch
(item.getItemId()) {
case 0:
Toast.makeText(this, "You
clicked on Item 1",
Toast.LENGTH_LONG).show();
return true;
case 1:
Toast.makeText(this, "You
clicked on Item 2",
Toast.LENGTH_LONG).show();
return true;
case 2:
Toast.makeText(this, "You
clicked on Item 3",
Toast.LENGTH_LONG).show();
return true;
case 3:
Toast.makeText(this, "You
clicked on Item 4",
Toast.LENGTH_LONG).show();
return true;
case 4:
Toast.makeText(this, "You
clicked on Item 5",
Toast.LENGTH_LONG).show();
return true;
case 5:
Toast.makeText(this, "You
clicked on Item 6",
Toast.LENGTH_LONG).show();
return true;
case 6:
Toast.makeText(this, "You
clicked on Item 7",
Toast.LENGTH_LONG).show();
return true;
}
return false;
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}
@Override
public boolean
onOptionsItemSelected(MenuItem item)
{
return
MenuChoice(item);
}
}
ViewsActivity.java
package com.immobulus.menulayout;
import
android.app.Activity;
import
android.os.Bundle;
import
android.content.Intent;
public class ViewsActivity extends Activity {
/** Called when the activity is first
created. */
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startActivity(new Intent(this, menulayout.class));
}
}
AndroidManifest.xml
<?xml version="1.0"
encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.immobulus.menulayout"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".menulayout"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"
/>
<category android:name="android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="9"
/>
</manifest>
Running project sehingga menghasilkan gambar seperti berikut:
Tekan tombol menu sehingga muncul seperti berikut:
Untuk
menampilkan Context Menu Layout click tombol
yang bertuliskan More sehingga
muncul seperti gambar berikut:
Tidak ada komentar:
Posting Komentar