Assalamualaiqum
Pada kesempatan ini saya akan kembali membagikan pengetahuan yaitu cara membuat group menu pada android menggunakan ADT Eclipse.
Ok langsung saja.
- Buat project baru.
- Isikan source code pada file 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” >
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” >
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/hello_world” />
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/hello_world” />
</RelativeLayout>
- Source code MainActivity.java
package example.groupmenu; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// menu(group id, id, order,title )
menu.add(0, 1, 2, “Java”);
menu.add(0, 2, 3, “C++”);
menu.add(0, 3, 1, “PHP”);
menu.add(1, 4, 0, “Mysql”);
menu.add(1, 5, 4, “SqLite”);
menu.add(2, 6, 5, “Eclipse”);
menu.add(2, 7, 6, “Netbeans”);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
String group = “”;
if (item.getGroupId() == 0) {
group = “Bahasa Pemrograman”;
} else if (item.getGroupId() == 1) {
group = “Database”;
} else if (item.getGroupId() == 2) {
group = “Editor”;
}
Toast.makeText(
MainActivity.this,
“\nGroup: ” + group +
“Item ID : ” + item.getItemId()+
“\nTitle: ” + item.getTitle() +
“\nUrutan: “+ item.getOrder(),
Toast.LENGTH_LONG).show();
return super.onMenuItemSelected(featureId, item);
}
}
- Terakhir jalankan program pada emulator atau smartphone anda.
Selamat mencoba ☺
0 komentar:
Posting Komentar