[챕터 2-4-3] 옵션메뉴

2020. 2. 15. 20:16Android/Android 챕터 2-4

반응형

결과화면

첫번째 아이템 버튼 클릭 시 

두번째 아이템 버튼 클릭 시 

세번째 아이템 버튼 클릭 시 


프로젝트명 : MyOptionMenu

패키지명 : org.minokuma.myoptionmenu

 

 


디렉토리 생성 : res/menu


menu_main

 


 

menu_main.xml

 

더보기
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_refresh"
        android:title="새로고침"
        android:icon="@drawable/menu_refresh"
        app:showAsAction="always"
        />

    <item android:id="@+id/menu_search"
        android:title="검색"
        android:icon="@drawable/menu_search"
        app:showAsAction="always"
        />

    <item android:id="@+id/menu_settings"
        android:title="설정"
        android:icon="@drawable/menu_settings"
        app:showAsAction="always"
        />
</menu>

 


이미지 격납

res/drawable

 

menu_refresh.png
0.03MB
menu_search.png
0.02MB
menu_settings.png
0.03MB


MainActivity.java

 


MainActivity.java

 

더보기
package org.minokuma.myoptionmenu;

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

import android.os.Bundle;
import android.view.Menu;
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) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int curId = item.getItemId();

        switch (curId) {
            case R.id.menu_refresh:
                showToast("새로고침 메뉴 선택됨");
                break;
            case R.id.menu_search:
                showToast("검색 메뉴 선택됨");
                break;
            case R.id.menu_settings:
                showToast("설정 메뉴 선택됨");
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);

    }

    public void showToast (String data){
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
    }

}

 


이미지 추가 격납

 

res/drawable

 

back.png
0.00MB
home.png
0.00MB


액션바 추가

 

MainActivity.java

더보기
package org.minokuma.myoptionmenu;

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

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        actionBar = getSupportActionBar();
        actionBar.setLogo(R.drawable.home);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
                                    ActionBar.DISPLAY_USE_LOGO);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int curId = item.getItemId();

        switch (curId) {
            case R.id.menu_refresh:
                showToast("새로고침 메뉴 선택됨");
                break;
            case R.id.menu_search:
                showToast("검색 메뉴 선택됨");
                break;
            case R.id.menu_settings:
                showToast("설정 메뉴 선택됨");
                break;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);

    }

    public void showToast (String data){
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
    }

}
반응형

'Android > Android 챕터 2-4' 카테고리의 다른 글

[챕터 2-4-6] 뷰 페이저  (0) 2020.02.16
[챕터 2-4-5] 탭2  (0) 2020.02.16
[챕터 2-4-4] 탭  (0) 2020.02.16
[챕터 2-4-2] 프래그먼트 2  (0) 2020.02.15
[챕터 2-4-1] 프래그먼트  (0) 2020.02.14