관리 메뉴

FU11M00N

[Andriod] Activity의 onCreateOptionsMenu() 오버라이딩 본문

Programming/Android

[Andriod] Activity의 onCreateOptionsMenu() 오버라이딩

호IT 2020. 11. 2. 17:29

Activity의 onCreateOptionsMenu() 오버라이딩 사용하여 메뉴 구현하기.

 

이번 글은 설명이 따로 없는 코드 글입니다.

 

package org.techtown.a2b_20201102_3;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    MenuItem itemRed, itemGreen, itemBlue, itemZoom,itemRotate;
    Button button1,btnMenu;
    LinearLayout container;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = findViewById(R.id.button1);
        btnMenu = findViewById(R.id.btnMenu);
        container = findViewById(R.id.container);
        itemRed = findViewById(R.id.itemRed);
        itemGreen = findViewById(R.id.itemGreen);
        itemBlue = findViewById(R.id.itemBlue);
        itemRotate= findViewById(R.id.itemRotate);
        itemZoom = findViewById(R.id.itemZoom);
        registerForContextMenu(btnMenu);



    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater menuInflater = getMenuInflater();


        if(v == btnMenu){
            menu.setHeaderTitle("배경색 변경");
            menuInflater.inflate(R.menu.menu2,menu);
        }

    }

    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.itemRed: container.setBackgroundColor(Color.RED);
                break;
            case R.id.itemGreen: container.setBackgroundColor(Color.GREEN);
                break;
            case R.id.itemBlue: container.setBackgroundColor(Color.BLUE);
                break;

        }

        return super.onContextItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflate = getMenuInflater();
        menuInflate.inflate(R.menu.menu1,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.itemRed: container.setBackgroundColor(Color.RED);
                break;
            case R.id.itemGreen: container.setBackgroundColor(Color.GREEN);
                break;
            case R.id.itemBlue: container.setBackgroundColor(Color.BLUE);
                break;
            case R.id.itemRotate: button1.setRotation(45);
                break;
            case R.id.itemZoom: button1.setScaleX(2);
                button1.setScaleY(2);
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

MainActivity.java

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="오른쪽 위 메뉴를 클릭 하세요"
        android:textSize="24sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@android:string/VideoView_error_button"
        android:textSize="24sp" />

    <Button
        android:id="@+id/btnMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Menu_Button"
        android:textSize="30sp" />
</LinearLayout>

activity_main.xml 

 

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/itemRed"
        android:title="배경색(빨강)" />
    <item
        android:id="@+id/itemGreen"
        android:title="배경색(녹색)" />
    <item
        android:id="@+id/itemBlue"
        android:title="배경색(파랑)"></item>
    <item android:title="Item">
        <menu>
            <item
                android:id="@+id/itemRotate"
                android:title="itemRotatedd" />
            <item
                android:id="@+id/itemZoom"
                android:title="itemZoom2" />
        </menu>
    </item>
</menu>

menu1.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/itemRed"
        android:title="배경색(빨강)" />
    <item
        android:id="@+id/itemGreen"
        android:title="배경색(녹색)" />
    <item
        android:id="@+id/itemBlue"
        android:title="배경색(파랑)" />
</menu>

menu2.xml

코드 메인 화면과 메뉴
메뉴의 색깔 버튼 기능
메뉴의 메뉴 기능 2

 

Comments