I want to add overflow menu on my custom action bar. I know, I can add new settings activity but that's too confusing, and I wanted to know the exact code to add those.
I have seen few posts, but it was limited to older SDKs. As I am using custom layout for my action bar. Please help me so that code could work for Android 8.0 and for sdk's lower than that.
My code is as follows:
HomescreenActivity.java :-
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homescreen);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.abs_layout);
}
activity_homescreen.xml :-
<?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:background="@drawable/paper_planes_color_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomescreenActivity">
<android.support.v7.widget.RecyclerView
android:layout_marginTop="15dp"
android:id="@+id/recy_category"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
abs_layout.xml :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/tvTitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#FFFFFF" />
</LinearLayout>
Just override "onCreateOptionsMenu()" method, and mention your menu through "menu/your_menu_file.xml ", and to handle menu click listener override "onOptionsItemSelected" method
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}