Issue
I’m new to programming, and I want to add a settings menu button.
Like on the picture, my app doesn’t show the three dots button.
MainActivity.java :
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.vueSatellite:
Toast.makeText(this, "Hi", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
activity_main.xml :
<?xml version"1.0" encoding"utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
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">
<androidx.viewpager.widget.ViewPager
android:id"@+id/view_pager"
android:layout_width"match_parent"
android:layout_height"match_parent"
app:layout_behavior"@string/appbar_scrolling_view_behavior" />
<com.google.android.material.appbar.AppBarLayout
android:layout_width"match_parent"
android:layout_height"wrap_content"
android:theme"@style/AppTheme">
<TextView
android:id"@+id/title"
android:layout_width"wrap_content"
android:layout_height"wrap_content"
android:gravity"center"
android:minHeight"?actionBarSize"
android:padding"@dimen/appbar_padding"
android:text"@string/app_name"
android:textColor"@color/colorAccent"
android:textAppearance"@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />
<com.google.android.material.tabs.TabLayout
android:id"@+id/tabs"
android:layout_width"match_parent"
android:layout_height"wrap_content"
android:background"@color/White"
app:tabTextColor"@color/colorAccent"/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
settings.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/vueSatellite"
android:title"@string/item_vue_satellite"
app:showAsAction"never"/>
</menu>
What is my mistake ?
Or what do I have to add?
Tell me if there is missing information like the file AndroidManifest.xml for example.
Thanks
Solution
In your Activity just add
Toolbar toolbar (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Hope it works.
Answered By – unaisulhadi