skip to Main Content

I have a drawer menu like this. It has more than one item but we can assume all the same. Each one is an activity. I can change the activity from there. But I want to highlight background of the current activity. How can I do that?

Thanks for your help.

Example screenshot:
Imgur link

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <item
        android:id="@+id/miHome"
        android:title="@string/menu_home" />

</menu>

activity_main.xml:

<com.google.android.material.navigation.NavigationView
    android:id="@+id/navView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/nav_drawer_menu" />

2

Answers


  1. Chosen as BEST ANSWER

    This is activity_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.drawerlayout.widget.DrawerLayout 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/drawerLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <Button
                android:id="@+id/btnGoToAdult"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/main_btn_goto_adult"
                app:layout_constraintEnd_toStartOf="@+id/btnGoToChild"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent"
                tools:layout_editor_absoluteY="448dp" />
    
            <Button
                android:id="@+id/btnGoToChild"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/main_btn_goto_child"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toEndOf="@+id/btnGoToAdult"
                tools:layout_editor_absoluteY="448dp" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
        <com.google.android.material.navigation.NavigationView
            android:id="@+id/navView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_drawer_menu" />
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    This is MainActivity.kt:

    package com.example.myapp
    
    import android.content.Intent
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.Menu
    import android.view.MenuItem
    import android.widget.Toast
    import androidx.appcompat.app.ActionBarDrawerToggle
    import com.google.android.gms.maps.CameraUpdateFactory
    import com.google.android.gms.maps.GoogleMap
    import com.google.android.gms.maps.OnMapReadyCallback
    import com.google.android.gms.maps.SupportMapFragment
    import com.google.android.gms.maps.model.LatLng
    import com.google.android.gms.maps.model.MarkerOptions
    import kotlinx.android.synthetic.main.activity_main.*
    import android.text.Html
    import androidx.core.text.HtmlCompat
    import androidx.core.view.get
    import com.google.android.material.navigation.NavigationView
    
    class MainActivity : AppCompatActivity() {
    
        // navDrawer
        lateinit var toggle: ActionBarDrawerToggle
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            // navDrawer
            toggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open_nav, R.string.close_nav)
            drawerLayout.addDrawerListener(toggle)
            toggle.syncState()
            supportActionBar?.setDisplayHomeAsUpEnabled(true)
    
            // navDrawer renk değiştirme
            val main = navView.menu[0]
            main.title = HtmlCompat.fromHtml(
                "<background-color='#ff3824'>Anasayfa</ackground-color>",
                HtmlCompat.FROM_HTML_MODE_LEGACY
            )
    
            // navDrawer
            navView.setNavigationItemSelectedListener {
                when (it.itemId) {
                    R.id.miHome -> {
                        Toast.makeText(this, "Zaten anasayfadasın", Toast.LENGTH_SHORT).show()
                    }
                    R.id.miProductAdult -> {
                        val intent = Intent(this, ProductActivity::class.java)
                        startActivity(intent)
                    }
                    R.id.miProductChild -> {
                        val intent = Intent(this, ProductChildActivity::class.java)
                        startActivity(intent)
                    }
                    R.id.miContent -> {
                        val intent = Intent(this, ContentActivity::class.java)
                        startActivity(intent)
                    }
                    R.id.miBuy -> {
                        val intent = Intent(this, BuyActivity::class.java)
                        startActivity(intent)
                    }
                    R.id.miAbout -> {
                        val intent = Intent(this, AboutActivity::class.java)
                        startActivity(intent)
                    }
                }
                true
            }
    
            btnGoToChild.setOnClickListener {
                val intent = Intent(this, ProductChildActivity::class.java)
                startActivity(intent)
            }
    
            btnGoToAdult.setOnClickListener {
                val intent = Intent(this, ProductActivity::class.java)
                startActivity(intent)
            }
        }
    
        // navDrawer
        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            // home icon
            when (item.itemId) {
                R.id.miHomeLogo -> Toast.makeText(this, "Zaten anasayfadasın", Toast.LENGTH_SHORT).show()
            }
    
            // navDrawer
            if (toggle.onOptionsItemSelected(item)) {
                return true
            }
            return super.onOptionsItemSelected(item)
        }
    
        // home icon
        override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.app_bar_menu, menu)
            return true
        }
    
    }
    

    This is app_bar_menu.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/miHomeLogo"
            android:title="@string/menu_home"
            android:icon="@drawable/ic_home"
            app:showAsAction="ifRoom" />
    
    </menu>
    

    nav_drawer_menu.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"
        xmlns:tools="http://schemas.android.com/tools">
    
        <item
            android:id="@+id/miHome"
            android:title="@string/menu_home" />
    
        <item
            android:id="@+id/miProduct"
            android:title="@string/menu_products">
            <menu>
                <item
                    android:id="@+id/miProductAdult"
                    android:title="@string/menu_product_adult">
                </item>
                <item
                    android:id="@+id/miProductChild"
                    android:title="@string/menu_product_child">
                </item>
            </menu>
        </item>
    
    
        <item
            android:id="@+id/miContent"
            android:title="@string/menu_content"/>
    
        <item
            android:id="@+id/miBuy"
            android:title="@string/menu_buy"/>
    
        <item
            android:id="@+id/miAbout"
            android:title="@string/menu_about"/>
    
    </menu>
    

  2. If you are trying to select an Item of the Navigation drawer then you can try out using this:

    navigationView.getMenu().getItem(0).setChecked(true); 
    

    The getItem(int index) method gets the MenuItem then you can call the setChecked(true); on the menu item.

    You can also highlight the item using this:

    onNavigationItemSelected(navigationView.getMenu().getItem(0));
    

    Here is the reference

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search