1. Create Option menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>
2. Create Menu Item
test_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/test_1"
android:title="Test1">
</item>
<item
android:id="@+id/test_2"
android:title="Test2">
</item>
<item
android:id="@+id/test_3"
android:title="Test3">
</item>
</menu>
MainActivity.java
onCreate OptionsMenu method 생성 ( 직접 작성해도 됨 )
ackage com.example.project7_optionmenu;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
}
코드 추가 ( xml, java 연결 )
...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// 코드 추가
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
//
return super.onCreateOptionsMenu(menu);
}
...
2. Event
...
// option menu event method
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId())
{
case R.id.test_1:
Toast.makeText(getApplicationContext(), "Test1", Toast.LENGTH_LONG).show();
case R.id.test_2:
Toast.makeText(getApplicationContext(), "Test2", Toast.LENGTH_LONG).show();
case R.id.test_3:
Toast.makeText(getApplicationContext(), "Test3", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
...
full main java code
package com.example.project7_optionmenu;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch(item.getItemId())
{
case R.id.test_1:
Toast.makeText(getApplicationContext(), "Test1", Toast.LENGTH_LONG).show();
case R.id.test_2:
Toast.makeText(getApplicationContext(), "Test2", Toast.LENGTH_LONG).show();
case R.id.test_3:
Toast.makeText(getApplicationContext(), "Test3", Toast.LENGTH_LONG).show();
}
return super.onOptionsItemSelected(item);
}
}
developer.android.com/guide/topics/ui/menus?hl=ko
728x90
반응형
'Flutter > Android' 카테고리의 다른 글
[안드로이드] AltBeacon Library ( Beacon ) (0) | 2021.03.17 |
---|---|
[안드로이드] 6. Activity and Intent (0) | 2021.01.25 |
[안드로이드] 4. EditText와 TextView를 이용해서 간단한 계산기 만들기. (0) | 2021.01.25 |
[안드로이드] 3. LinearLayout (0) | 2021.01.24 |
[안드로이드] 2. Buttons (0) | 2021.01.24 |