Search code examples
androidxmlandroid-studio

Why does the Menu doesn't appear in Android app?


I implemented the code for the menu that I created. It doesn't appear in the emulator and what appears is only the recycler view. Does anyone know what is causing the issue?

I am using API 29 Android version 5 on the emulator. Does the Android version I choose matter?

here is my code :

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/item1"
        android:icon="@drawable/ic_icon"
        android:title="About Me"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/item2"
        android:title="List"
        app:showAsAction="never" />

    <item
        android:id="@+id/item3"
        android:title="Grid"
        app:showAsAction="never">

    </item>

</menu>

Menu Activity.kt

package com.example.bangkit_recycleview

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {

private lateinit var foodRecycleView : RecyclerView
private lateinit var foodArrayList : ArrayList<FoodDetail>
lateinit var imageArticle : Array<Int>
lateinit var heading : Array<String>
lateinit var description : Array<String>

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

    imageArticle = arrayOf(
        R.drawable.apfelstrudel,
        R.drawable.kebab,
        R.drawable.kimchi,
        R.drawable.lasagna,
        R.drawable.rendang,
        R.drawable.sushi,
        R.drawable.tom_yam,
        R.drawable.paella,
        R.drawable.pecking_duck,
        R.drawable.pho,
    )

    heading = arrayOf(
        "Apfelstrudel",
        "Kebab Turkey",
        "Kimchi",
        "Lasagna",
        "Rendang",
        "Sushi",
        "Tom Yam",
        "Paella",
        "Pecking Duck",
        "Pho",
    )

    description = arrayOf(
        "More commonly known as Apple Strudel, Apfelstrudel is a popular pastry in Austria and other parts of Europe. The dish consists of an oval strudel pastry cover with mouthwatering apple filling inside. The apple filling is prepared with grated apples, cinnamon, sugar, breadcrumbs, and raisins. This delicacy tastes best when served with vanilla ice cream and whipped cream. ",
        "A dish popular across the Middle East, Kebabs are originally from Turkey. They consist of ground meat or seafood, fruits, and vegetables in some cases and are cooked on a skewer with a big fire underneath, just like a barbeque on the grill",
        "Kimchi is a staple Korean side dish prepared from fermented vegetables such as Korean radishes, and cabbage and topped with several seasonings, including garlic, chilli powder, scallions, and ginger.",
        "Italy's lasagna takes over Pizza to be added in the \"world's best food dishes\" list because of its comeback. It is one of the oldest pasta but has become popular only in the present times. The ingredients itself sound mouthwatering - meats, pasta, vegetables, tomato sauce, and lots and lots of cheese.",
        "Often called \"the world's most delicious dish,\" Rendang is prepared by simmering beef with coconut milk with a mixture of the best of spices including turmeric, garlic, lemongrass, ginger, chillies, and galangal. The dish is then stewed for a few hours which gives it a tender texture and exotic taste. The blast of flavours is surely one of the reasons why the dish is loved globally and is also one of the best dishes in the world. Easy to rustle up, this dish is often served at ceremonies or to honour guests.",
        "Prepared with vinegared rice and a wide range of ingredients including seafood, vegetables, and sometimes fruits. Sushi tastes best when served with wasabi, pickled ginger, and soy sauce. A popular garnish for this dish is Daikon radish. The type of fish in it defines a sushi's taste. However, the vinegared rice gives the dish a tangy taste overall. ",
        "A type of sour and hot Thai soup, Tom yam goong is prepared with shrimp along with a load of healthy herbs and spices?  lemongrass, lime, kaffir leaves, galangal, and red chili peppers to name a few. the soup plays around with a bit of all possible flavours? spicy, sour, salty and sweet. ",
        "Paella has got its roots in Valencia, Spain. It is an ancient dish recreated with a modern touch in the present times. There are various ways to eat Paella. The original recipe contains white rice with green beans, meat (rabbit or chicken, sometimes duck), butterbeans, snails, topped with seasoning such as rosemary and. The recipe also uses artichokes during its season. ",
        "Peking duck is a dish that finds its way to Beijing. The ducks for this dish are specially bred and slaughtered after 60 days and seasoned first before being roasted in closed ovens. This gives the meat a crisp skin and thin texture. The dish is served with cucumbers, spring onion, and sweet bean sauce. ",
        "A simple yet an incredible dish, Pho (pronounced as 'fuh') is a Vietnamese dish made of rice noodles and meat (usually beef or chicken) served in broth and topped with herbs. The dish has got a great fragrance which lingers in the eater's head for a while. ",
    )

    foodRecycleView = findViewById(R.id.recycleView)
    foodRecycleView.layoutManager = LinearLayoutManager(this)
    foodRecycleView.setHasFixedSize(true)

    foodArrayList = arrayListOf<FoodDetail>()
    getFoodData()
}

private fun getFoodData() {
    for(i in imageArticle.indices) {
        val foods = FoodDetail(imageArticle[i], heading[i], description[i])
        foodArrayList.add(foods)
    }

    var adapter = FoodAdapter(foodArrayList)
    foodRecycleView.adapter = adapter
    adapter.setOnItemClickCallback(object : FoodAdapter.OnItemClickCallback {
        override fun onItemClicked(data: FoodDetail) {
            showSelectedHero(data)
        }

    })
}

private fun showSelectedHero(food: FoodDetail) {
    Toast.makeText(this, "Kamu memilih " + food.heading, Toast.LENGTH_SHORT).show()
}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.menu_main, menu)
    return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.item1 -> {
            Toast.makeText(this, "Item 1 selected", Toast.LENGTH_SHORT).show()
            return true
        }
        R.id.item2 -> {
            Toast.makeText(this, "Item 2 selected", Toast.LENGTH_SHORT).show()
            return true
        }
        R.id.item3 -> {
            Toast.makeText(this, "Item 3 selected", Toast.LENGTH_SHORT).show()
            return true
        }
        else -> return super.onOptionsItemSelected(item)
    }
} }

Can anyone help me to solve this problem ?


Solution

  • We really need to see the AndroidManifest.xml, themes.xml and the activity_main.xml to know exactly what's going on.

    The most likely problem is that you're using a .NoActionBar theme for the MainActivity, but don't provide an explicit tool bar (like MaterialToolBar) in your activity_main.xml. In this case, you wouldn't see a tool bar at all (no title at top).