Java 안드로이드/안드로이드
7. 메뉴 – 2. Context Menu
언휴
2025. 1. 4. 08:40
이번에는 특정 컨트롤을 우측 마우스 버튼 클릭하였을 때 나오는 컨텍스트 메뉴(Context Menu)를 사용하는 실습을 해 봅시다.

이번 실습에서는 두 개의 TextView를 배치하고 각각의 TextView 개체마다 서로 다른 Context Menu를 띄우는 실습을 할 거예요.
먼저 activity_main.xml 파일에 두 개의 TextView를 배치하세요.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item_red"
android:title="배경색 RED"/>
<item android:id="@+id/item_green"
android:title="배경색 GREEN"/>
<item android:id="@+id/item_blue"
android:title="배경색 BLUE"/>
</menu>
app>>res 하위에 menu 폴더를 생성한 후에 두 개의 메뉴 파일(menu1.xml, menu2.xml)을 추가하세요.

다음은 menu1.xml 파일의 내용입니다.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item_red"
android:title="배경색 RED"/>
<item android:id="@+id/item_green"
android:title="배경색 GREEN"/>
<item android:id="@+id/item_blue"
android:title="배경색 BLUE"/>
</menu>
다음은 menu2.xml 파일의 내용입니다.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item_fs_big"
android:title="크게"/>
<item android:id="@+id/item_fs_small"
android:title="작게"/>
</menu>
두 개의 TextView 개체를 참조할 멤버 필드를 선언하고 두 번째 TextView의 textSize 속성 값을 기억할 멤버 필드를 선언하고 30으로 초기화합시다.
TextView tv1,tv2;
float fs = 30;
onCreate 메서드에서 두 개의 TextView 개체를 참조합니다. 그리고 registerForContextMenu 메서드를 통해 두 개체에 Context Menu를 등록합니다.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.tv_1);
registerForContextMenu(tv1);
tv2 = (TextView)findViewById(R.id.tv_2);
registerForContextMenu(tv2);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,fs);
}
onCreateContextMenu 메서드를 재정의하여 첫 번째 인자로 전달받은 개체에 따라 적절한 메뉴를 설정합니다. 이 때 MenuInflater 개체의 inflate 메서드를 호출합니다.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mi = getMenuInflater();
if(v==tv1){
mi.inflate(R.menu.menu1,menu);
}
if(v==tv2){
mi.inflate(R.menu.menu2,menu);
}
}
onContextItemSelected 메서드를 재정의하세요. 그리고 선택한 메뉴 항목에 따라 적절한 처리를 수행하게 작성하세요.
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_red: tv1.setBackgroundColor(Color.RED); return true;
case R.id.item_green: tv1.setBackgroundColor(Color.GREEN); return true;
case R.id.item_blue: tv1.setBackgroundColor(Color.BLUE); return true;
case R.id.item_fs_big: Increment(5); return true;
case R.id.item_fs_small: Increment(-5); return true;
}
return false;
}
private void Increment(int value){
fs += value;
tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,fs);
}
다음은 MainActivity.java 파일의 소스 코드입니다.
package com.example.ehclub.ex_contextmenu;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView tv1,tv2;
float fs = 30;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView)findViewById(R.id.tv_1);
registerForContextMenu(tv1);
tv2 = (TextView)findViewById(R.id.tv_2);
registerForContextMenu(tv2);
tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,fs);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater mi = getMenuInflater();
if(v==tv1){
mi.inflate(R.menu.menu1,menu);
}
if(v==tv2){
mi.inflate(R.menu.menu2,menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item_red: tv1.setBackgroundColor(Color.RED); return true;
case R.id.item_green: tv1.setBackgroundColor(Color.GREEN); return true;
case R.id.item_blue: tv1.setBackgroundColor(Color.BLUE); return true;
case R.id.item_fs_big: Increment(5); return true;
case R.id.item_fs_small: Increment(-5); return true;
}
return false;
}
private void Increment(int value){
fs += value;
tv2.setTextSize(TypedValue.COMPLEX_UNIT_SP,fs);
}
}
언제나휴일 추천 여행 및 산책