반응형

ContextMenu

설명

  • 뷰를 길게 눌렀을 때 제공하는 메뉴

코드

res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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:text="menu" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
res/menu/context_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu1"
        android:title="메뉴1" />
    <item
        android:id="@+id/menu2"
        android:title="메뉴2" />
    <item
        android:id="@+id/menu3"
        android:title="메뉴3">
        <menu>
            <item
                android:id="@+id/menu3_1"
                android:title="메뉴3-1" />
            <item
                android:id="@+id/menu3_2"
                android:title="메뉴3-2" />
        </menu>
    </item>
</menu>
MainActivity
class MainActivity : AppCompatActivity() {
    val data = arrayOf("리스트1", "리스트2", "리스트3")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val textView: TextView = findViewById(R.id.textView)
        val listView: ListView = findViewById(R.id.listView)

        listView.adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, data)

        registerForContextMenu(textView)
        registerForContextMenu(listView)
    }

    override fun onCreateContextMenu(menu: ContextMenu?, view: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        when (view?.id) {
            R.id.textView -> {
                menu?.setHeaderTitle("TextView Menu")
                menuInflater.inflate(R.menu.context_menu, menu)
            }
            R.id.listView -> {
                menu?.setHeaderTitle("ListView Menu")
                menuInflater.inflate(R.menu.context_menu, menu)

                val info = menuInfo as AdapterView.AdapterContextMenuInfo
                Toast.makeText(baseContext, "항목 - ${info.position}", Toast.LENGTH_SHORT).show()
            }
        }
    }

    override fun onContextItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu1 -> Toast.makeText(baseContext, "메뉴1", Toast.LENGTH_SHORT).show()
            R.id.menu2 -> Toast.makeText(baseContext, "메뉴2", Toast.LENGTH_SHORT).show()
            R.id.menu3 -> Toast.makeText(baseContext, "메뉴3", Toast.LENGTH_SHORT).show()
            R.id.menu3_1 -> Toast.makeText(baseContext, "메뉴3-1", Toast.LENGTH_SHORT).show()
            R.id.menu3_2 -> Toast.makeText(baseContext, "메뉴3-2", Toast.LENGTH_SHORT).show()
        }
        return super.onContextItemSelected(item)
    }
}

참고

반응형

'Development > Android' 카테고리의 다른 글

[Android] ActionBar  (0) 2021.02.09
[Android] PopupMenu  (0) 2021.02.09
[Android] OptionMenu  (0) 2021.02.09
[Android] Activity  (0) 2021.02.09
[Android] RecyclerView  (0) 2021.02.09

+ Recent posts