Ahtisham
Ahtisham

Reputation: 10116

Menu not showing on toolbar in android

Background

I have created a Java class namely BaseActivity that does all the basic work for implementing menu items and toolbar. All the other Activities inherit BaseActivity.

Problem

When I built the project I can see the toolbar but no menu.

Code

BasicActivity

public class BaseActivity extends AppCompatActivity {

   private Toolbar toolbar;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);

        toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
   }

   @Override
   public boolean onOptionsItemSelected(MenuItem item){

        switch (item.getItemId()){
           case R.id.about:
                // code for about
                break;
           case R.id.exit:
                // code for exit
        }
        return true;
   }
}

activity_base

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".BaseActivity">

        <androidx.appcompat.widget.Toolbar
               android:layout_width="match_parent"
               android:layout_height="wrap_content"
               android:id="@+id/toolbar"
               android:background="@color/colorMatel"                   
               android:elevation="@android:dimen/app_icon_size"
               app:title="MyApplication"
               app:popupTheme="@style/ToolBarPopupStyle"
               app:theme="@style/ToolBarStyle" />

</RelativeLayout>

MainActivity

public class MainActivity extends BaseActivity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);        
      }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context=".MainActivity">

       <include layout="@layout/activity_base" />

</RelativeLayout>

main_menu

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

      <item
          android:title="@string/about"
          android:id="@+id/about" />
      <item
          android:title="@string/exit"
          android:id="@+id/exit" />

</menu>

Output

enter image description here

Edit:

Moving these lines of code to MainActivity from BaseActivity fixes the problem:

toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

But then I would have to include them every time I introduce new Activity.

Upvotes: 0

Views: 74

Answers (2)

Ahtisham
Ahtisham

Reputation: 10116

After doing couple of digging I found a solution and here its :

BaseActivity

public class BaseActivity extends AppCompatActivity {

      // added this function
      public void initToolbar(){

           toolbar = findViewById(R.id.toolbar);
           setSupportActionBar(toolbar);
      }

      @Override
      protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);               
           // removed setContentView(R.layout.activity_base);
      }

      ...
}

MainActivity

public class MainActivity extends BaseActivity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);    
          initToolbar();    
     }
}

Upvotes: 0

Yann Huissoud
Yann Huissoud

Reputation: 1023

This line setContentView(R.layout.activity_base); will be overridden by this line setContentView(R.layout.activity_main);.

You can't achieve what you are trying to do the way you do it.

Upvotes: 1

Related Questions