0

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>

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

3 Comments

You had import android.view.View?
@Thompson Here my code that works for me if it helps you. I think that you handle image too.
So, I tried your toolbar layout (activity_main.xml) and I have almost the same bug. I replaced layout_width value by match_parent and I removed layout_marginBottom. Now, I have the toolbar. I think your bug came from front and back.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.