How to navigation page with action menu bar Android studio?

Viewed 20

I have action menu bar android studio, i want tried to move page with icon on action bar, how do this? once search on google it's are using MenuItem, i'm expected menuitem it's a dropdown list on action bar, but i just using icon. In a sample code below, i got wrong with my code? tell me if u have see, thanks.

code :

package com.example.submitfirst

//import android.R


import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Menu
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

import kotlin.collections.ArrayList


class MainActivity : AppCompatActivity() {
    private lateinit var rvCat: RecyclerView
    private var list :ArrayList<Cat> = arrayListOf()

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

        rvCat= findViewById(R.id.rv_cat)
        rvCat.setHasFixedSize(true)
        list.addAll(CatData.listData)
        showRecycleList()
        val actBar = getSupportActionBar()


        actBar?.title = "Cat Show"


        val orange = resources.getColor(android.R.color.holo_blue_dark)

        actBar?.setBackgroundDrawable(ColorDrawable(orange))
    }

    private fun showRecycleList(){
        rvCat.layoutManager = LinearLayoutManager(this)
        val listCatAdapter= ListCatAdapter(list)
        rvCat.adapter = listCatAdapter
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
         menuInflater.inflate(R.menu.menu, menu)
        val moveActivity: Menu = findViewById(R.id.menu_profile)


        return super.onCreateOptionsMenu(menu)
    }


    }
1 Answers

use this xml for activity

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/login_screen_bg"
        tools:context=".ui.student.StudentActivity">

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottomNavigationView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#fff"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/menu_bottom_navigation_view" />


    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_graph" />

</androidx.constraintlayout.widget.ConstraintLayout>

the menu layout

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

    <item
        android:id="@+id/first_scren"
        android:title="first_title"
        android:icon="@drawable/first_icon"/>
    <item
        android:id="@+id/second_scren"
        android:title="secon_title"
        android:icon="@drawable/second_icon"/>
    <item
        android:id="@+id/third_screen"
        android:title="third_title"
        android:icon="@drawable/third_icon"/>

</menu>

add this code in onCreate activity

val navController = findNavController(R.id.nav_host_fragment)
binding.bottomNavigationView.setupWithNavController(navController)

and add your navigation with file name nav_graph

Related