Search code examples
androidkotlinmenumenuitem

Custom Toolbar Options using Android Kotlin is not working


I have written the below code in MainActivity.kt

I am able to produce the 3 lines of Menu Icon, enter image description here

I am not getting any error but the Menu is also not opening which I have written in below codes:

package com.mytestapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_main.*

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

        setSupportActionBar(app_toolbar)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main_menu, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return when (item.itemId) {
            R.id.myid -> {
                Toast.makeText(applicationContext, "click on setting", Toast.LENGTH_LONG).show()
                true
            }
            R.id.share_app ->{
                Toast.makeText(applicationContext, "click on exit", Toast.LENGTH_LONG).show()
                return true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }


}

activity_main.xml is:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center"
        android:orientation="vertical">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/app_toolbar"
            android:layout_width="40dp"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:layout_marginBottom="-50dp"
            android:background="@android:color/transparent"
            android:clipToPadding="false"
            android:gravity="end"
            app:title="Menu"
            app:subtitle="Options"
            app:navigationIcon="@drawable/ic_menu_preferences"
            app:subtitleTextColor="#000000"
            app:titleTextColor="#000000" />
    </LinearLayout>
</ScrollView>

main_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"
    tools:context="com.mytestapp">

    <item
        android:id="@+id/myid"
        android:enabled="true"
        android:icon="@android:drawable/ic_input_add"
        android:title="@string/mytitle"
        android:visible="true"
        app:showAsAction="ifRoom" />

    <item
        android:id="@+id/share_app"
        android:icon="@drawable/ic_menu_share"
        android:title="@string/share" />

</menu>

Solution

  • I start in Android development with Kotlin but try this in your onCreate method:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    
        val toolbar: Toolbar = findViewById<View>(R.id.app_toolbar) as Toolbar
        setSupportActionBar(toolbar)
    }
    

    EDIT: Then, update your activity_main.xml to replace layout_width value by match_parent (toolbar will take the parent's width, all the screen) and to remove layout_marginBottom (or replace the value by another and non negative).

    I hope that helps you!