When i open my app and click on the hamburger icon, it is does not open the navigation drawer. But when i swipe from the edges it is opening. Also the drawer responds to hamburger icon click events after the swipe action.
I tried every solution on stackoverflow but couldn't resolve the issue. Please someone help. Thanks in advance.
XML Code:
<androidx.drawerlayout.widget.DrawerLayout
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:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/activity_main">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="Nexzen"
app:titleTextColor="#color/white"
android:background="#color/dark_green"/>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.tabs.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tabLayout"
android:background="#color/dark_green"
app:tabTextColor="#color/white"
app:tabIndicator="#color/receive_message"
app:tabSelectedTextColor="#color/receive_message"
android:layout_below="#id/appBarLayout"/>
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/viewPager"
android:layout_below="#id/tabLayout"/>
</RelativeLayout>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navmenu"
android:visibility="gone"
app:menu="#menu/drawer_menu"
app:itemTextColor="#color/text_color"
app:itemIconTint="#color/text_color"
app:headerLayout="#layout/nav_header"
android:layout_gravity = "start" />
</androidx.drawerlayout.widget.DrawerLayout>
Activity code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
toggle = new ActionBarDrawerToggle(this, binding.drawer, binding.toolbar, R.string.open, R.string.close);
toggle.getDrawerArrowDrawable().setColor(getColor(R.color.white));
toggle.setDrawerIndicatorEnabled(true);
toggle.setDrawerSlideAnimationEnabled(true);
binding.drawer.addDrawerListener(toggle);
toggle.syncState();
}
#Override
protected void onStart() {
super.onStart();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
if (toggle.onOptionsItemSelected(item)) {
binding.drawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(#Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
Ensure you haven't set the visibility of the NavigationView in the fragment to invisible or gone. This was the cause for me.
Swiping must make the navigation drawer visible. Clicking the hamburger button does not.
Related
I'm working on a menu navigation drawer , in that I'm not able to click items when I'm adding a constraint layout inside the drawer layout. But after removing that click, the menu item works fine. So please tell me how can I fix this issue.
here below is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
tools:context=".MainActivity"
android:id="#+id/drawerlayout"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include layout="#layout/appbar">
</include>
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigationitem"
android:id="#+id/navigationview"
app:headerLayout="#layout/header_layout"
android:layout_gravity="start">
</com.google.android.material.navigation.NavigationView>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:textColor="#color/white">
</TextView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
And here below is my mainactivity
public class MainActivity extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar1;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawerLayout=findViewById(R.id.drawerlayout);
toolbar1=findViewById(R.id.tbar);
navigationView=findViewById(R.id.navigationview);
toolbar1.inflateMenu(R.menu.navigationitem);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar1,R.string.nav_open,R.string.nav_close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id==R.id.profile){
Toast.makeText(MainActivity.this, "Profile", Toast.LENGTH_SHORT).show();
}
else if(id==R.id.contact){
Toast.makeText(MainActivity.this, "Contact", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this, "Logoutact", Toast.LENGTH_SHORT).show();
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
}
Now, How Can Solve This Problem?
I have created the layout as attached below, I haven't used any drawerlayout or navigation view. top bar is created using toolbar tag. Now I want to add a navigation view which will invoke once user clicks that hamburger icon. And it should be accessible from other activities as well
You should use the navigation drawer.
You can do this this by creating one common class named BaseActivity and put your code of navigation drawer in this class. and extend your classes/Activities with BaseActivity, where you want to show navigation drawer.
public class BaseActivity extends AppCompatActivity {
public Toolbar toolbar;
ActionBarDrawerToggle mDrawerToggle;
Context context;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
protected boolean useToolbar() {
return true;
}
#Override
public void setContentView(int layoutResID) {
context = this;
DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.drawer_main, null);
FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.frame);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
toolbar = (Toolbar) fullView.findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("");
toolbar.setTitle("");
this.getSupportActionBar().setElevation(0);
getSupportActionBar().setLogo(R.drawable.ic_arrahm);
// toolbar.setLogo(R.drawable.ic_main);
if (useToolbar()) {
setSupportActionBar(toolbar);
setTitle("Places Near Me");
} else {
toolbar.setVisibility(View.GONE);
}
//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigation_view);
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
case R.id.edit_profile:
return true;
case R.id.change_password:
return true;
default:
Toast.makeText(getApplicationContext(), "Work in progress", Toast.LENGTH_SHORT).show();
return true;
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
View header = navigationView.getHeaderView(0);
TextView tvName = (TextView) header.findViewById(R.id.name);
TextView tvEmail = (TextView) header.findViewById(R.id.email);
String name = Preferences.getDataFromStringPreferences(context,Constants.USER_DETAILS, Constants.USER_NAME);
if (name != null) {
tvName.setText(name);
}
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.openDrawer, R.string.closeDrawer) {
#Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return mDrawerToggle.onOptionsItemSelected(item);
}
}
create drawer_main.xml layout file
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:menu="#menu/drawer">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
create toolbar.xml under layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
>
</android.support.v7.widget.Toolbar>
create drawer_header.xml layout resource file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="178dp"
android:background="#color/colorPrimary"
android:orientation="vertical"
android:padding="20dp"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text=""
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:text=""
android:textColor="#ffffff"
android:textSize="14sp"
android:textStyle="normal"
/>
</LinearLayout>
<ImageView
android:id="#+id/circleView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="30dp" />
</RelativeLayout>
I'm trying to display an ActionBar in my PreferenceScreen. In my MainActivity it works totally fine, but in my PreferenceScreen it lokks messed up, because the title of the actinBar is in the middle of the screen and the layout is placed over the actionBar (see screenshot).
SettingsActivity.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings_drawer, new SettingsFragment())
.commit();
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mTitle = getTitle();
mDrawerLayout = findViewById(R.id.settings_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(R.string.app_name);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
setNavigationViewListener();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
settings_activity.xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/settings_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SettingsActivity">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
Don't replace your fragment directly with the DrawerLayout, instead use a FrameLayout inside the DrawerLayout that you can replace your fragment with.
Something along the following lines should work.
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/settings_drawer_base"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".SettingsActivity">
<!-- This LinearLayout represents the contents of the screen -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:visibility="visible" />
<!-- The main content view where fragments are loaded -->
<FrameLayout
android:id="#+id/settings_drawer"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
P.S -
You'll need to reference your DrawerLayout with the new id I gave it.
mDrawerLayout = findViewById(R.id.settings_drawer_base);
This question already has answers here:
Clicking hamburger icon on Toolbar does not open Navigation Drawer
(1 answer)
on clicking hamburger icon navigation drawer not opening
(2 answers)
Closed 3 years ago.
The drew the red line to show where the orange line should keep going.
After searching similar questions, I could not find a way to solve my problem.
I have a toggle button that shows in my Activity but when I press it, it doesn't open the navigation drawer (if I slide my finger it opens).
*The toggle button shows in the activity (the toolbar is transparent)
OnCreate and onOptionsItemSelect
ActionBarDrawerToggle toggle;
DrawerLayout drawer;
NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle
(
this,
drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
)
{
};
drawer.addDrawerListener(toggle);
toggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setTitle("");
toggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.colorPrimaryDark));
FloatingActionButton fab = findViewById(R.id.action_btn);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), NewEventActivity.class);
startActivity(intent);
/*Intent intent = new Intent(getApplicationContext(), testes.class);
startActivity(intent);*/
}
});
navigationView = findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(toggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
XML code:
<android.support.v4.widget.DrawerLayout
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"
android:id="#+id/drawer_layout">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title=""
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/wrapper_layout">
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/action_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:backgroundTint="#FE7D4B"
app:srcCompat="#drawable/ic_add" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/drawer_menu"
android:layout_gravity="start"
android:id="#+id/navigation_view"
>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
before I only used activity's and that worked but when I added the fragments to the activity's the menu icon disappeared and I could find a fix for it
my custom toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/apptheme"
app:theme="#style/AlertDialog.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="100">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/actionbar_logo_click"
android:paddingLeft="3dp"
android:layout_weight="5">
<ImageView
android:id="#+id/actionbar_logo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingRight="10dp" />
</FrameLayout>
<TextView
android:id="#+id/actionbar_titel"
style="#style/AudioFileInfoOverlayText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingTop="7dp"
android:textColor="#color/white"
android:textSize="27dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
my menu items
<?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"
android:icon="#drawable/ic_more_vert_white_24dp"
app:showAsAction="always">
<item
android:id="#+id/menu_rate"
android:title="#string/menu_rate"
/>
<item
android:id="#+id/menu_about"
android:title="#string/menu_about" />
</menu>
my activity xml layout where I add the custom toolbar with the id
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/app_bar"></include>
<!-- Container for loading and the actual fragment-->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ProgressBar
android:id="#+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone"
android:indeterminate="true" />
</LinearLayout>
</FrameLayout>
my java activity where I call the toolbar and also inflate the menu items
#EActivity(R.layout.activity_main_layout)
public class MainActivity extends BaseActivity {
#ViewById(R.id.adview)
MoPubView mMoPubView;
#ViewById(R.id.app_bar)
Toolbar toolbar;
#ViewById(R.id.actionbar_logo)
ImageView mAactionbarImage;
#ViewById(R.id.actionbar_titel)
TextView mActionbarTitel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(toolbar);
}
#AfterViews
protected void onViewsInitialized() {
gotoFragment(MainFragment_.builder().build(), false);
mAactionbarImage.setImageResource(R.mipmap.ic_launcher);
//Set the titel of the actionbar
mActionbarTitel.setText("How To Twitch");
}
#Override
public void onBackPressed() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
menu.getItem(0).setIcon(R.drawable.ic_more_vert_white_24dp);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_rate:
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
break;
case R.id.menu_about:
Intent aboutIntent = new Intent(MainActivity.this, AboutActivity_.class);
startActivity(aboutIntent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
for some reason my menu Icon is not showing up in the toolbar so if someone knows what I am doing wrong that would help me alot
I think you are missing an overridden method onPrepareOptionsMenu
Try to add this overridden method and check:
#Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id. menu_rate).setVisible(true);
menu.findItem(R.id. menu_about).setVisible(true);
super.onPrepareOptionsMenu(menu);
}
And add setHasOptionsMenu(true); within onCreateView() of your fragment.