관리 메뉴

드럼치는 프로그래머

[안드로이드] Context Menu 를 사용하는 예 본문

★─Programing/☆─Android

[안드로이드] Context Menu 를 사용하는 예

드럼치는한동이 2011. 5. 31. 13:59
화면 중앙을 누르면 Context Menu가 나타나고 색상을 선택하여 화면의 색상을 변경하는 예
 



main.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"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="화면 중앙을 누르세요"
    />
    <LinearLayout
     android:id="@+id/center"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>
</LinearLayout>




MenuDemoActivity.java

package com.test.menu;

import android.app.*;
import android.graphics.Color;
import android.os.*;
import android.view.*;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.*;

public class MenuDemoActivity extends Activity {
    /** Called when the activity is first created. */
 LinearLayout center;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        center = (LinearLayout)findViewById(R.id.center);
        /* 컨텍스트 메뉴를 사용하려는 View를 Activity에 등록한다 */
        registerForContextMenu(center);
    }

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  if(v==center){
   menu.setHeaderTitle("색상 선택");
   menu.add(0,1,0,"Red");
   menu.add(0,2,0,"Green");
   menu.add(0,3,0,"Blue");
  }
 }

 @Override
 public boolean onContextItemSelected(MenuItem item) {
  switch(item.getItemId()){
  case 1:
   center.setBackgroundColor(Color.RED);
   return true;
  case 2:
   center.setBackgroundColor(Color.GREEN);
   return true;
  case 3:
   center.setBackgroundColor(Color.BLUE);
   return true;
  }
  return false;
 }
}




res/menu/menu.xml 파일에 메뉴를 설정하고 MenuInflater 를 이용하여 Context Menu 로 전개하는 예
main.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"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="화면 중앙을 누르세요"
    />
    <LinearLayout
     android:id="@+id/center"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"/>
</LinearLayout>



res/menu/menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:id="@+id/menu_red"
  android:title="Red"/>
 <item android:id="@+id/menu_green"
  android:title="Green"/>
 <item android:id="@+id/menu_etc"
  android:title="Others">
  <menu>
   <group android:checkableBehavior="single">
    <item android:id="@+id/menu_black"
     android:checked="true"
     android:title="Black"/>
    <item android:id="@+id/menu_yellow"
     android:title="Yellow"/>
   </group>
  </menu>
 </item>
</menu>



MenuDemoActivity.java

package com.test.menu;

import android.app.*;
import android.graphics.Color;
import android.os.*;
import android.view.*;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.*;

public class MenuDemoActivity extends Activity {
    /** Called when the activity is first created. */
 LinearLayout center;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        center = (LinearLayout)findViewById(R.id.center);
        /* 컨텍스트 메뉴를 사용하려는 View를 Activity에 등록한다 */
        registerForContextMenu(center);
    }

 @Override
 public void onCreateContextMenu(ContextMenu menu, View v,
   ContextMenuInfo menuInfo) {
  if(v==center){
   MenuInflater inflater = this.getMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   menu.setHeaderTitle("색상 선택");
   //menu.add(0,1,0,"Red");
   //menu.add(0,2,0,"Green");
   //menu.add(0,3,0,"Blue");
  }
 }

 @Override
 public boolean onContextItemSelected(MenuItem item) {
  switch(item.getItemId()){
  case R.id.menu_red:
   center.setBackgroundColor(Color.RED);
   return true;
  case R.id.menu_green:
   center.setBackgroundColor(Color.GREEN);
   return true;
  case R.id.menu_black:
   center.setBackgroundColor(Color.BLACK);
   return true;
  case R.id.menu_yellow:
   center.setBackgroundColor(Color.YELLOW);
   return true;
  }
  return false;
 }
}



 


Comments