I want to show an ActionBar in my Activity while only showing a closing symbol. Therefore I used the method getSupportActionBar():
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
setTitle("Add Event");
But it throws a NullPointerExeption because apparently there's no ActionBar to call:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tutorial/com.example.tutorial.AddEventActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setHomeAsUpIndicator(int)' on a null object reference
In my MainActivity I implemented a Drawer Navigation where I already used a toolbar that works:
public class MainActivity extends AppCompatActivity {
protected DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
// Make sure to be using androidx.appcompat.app.ActionBarDrawerToggle version.
private ActionBarDrawerToggle drawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// This will display an Up icon (<-), we will replace it with hamburger later
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Find our drawer view
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Setup toggle to display hamburger icon with nice animation
drawerToggle.setDrawerIndicatorEnabled(true);
drawerToggle.syncState();
// Tie DrawerLayout events to the ActionBarToggle
mDrawer.addDrawerListener(drawerToggle);
// Find our drawer view
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
}
How do I fix that?
Thanks in advance!
Replace this line :
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
with this line :
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
This should solve your problem.
You may try to add following attribute to your MainActivity item under your ActivityManifest.xml
android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
Then try to access it with the getSupportActionBar(). With this way you can choose the activities that you don't or do want to include actionbars by using relevant themes. For example:
<activity
android:name=".SecondActivity"
android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
android:parentActivityName=".MainActivity" />
Related
I am calling fragments from Main_activity.java and also calling fragments from other fragments but i want to show backspace(<-) arrow button when any fragment is loaded(either from Main_activity or any other fragments) instead of ActionBarDrawerToggle. how can I achieve this?
Main Activity code
public class MainActivity extends AppCompatActivity {
NavigationView nav;
ActionBarDrawerToggle toggle;
DrawerLayout drawerLayout;
Toolbar toolbar;
ImageView homeInToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
nav=findViewById(R.id.navigationview);
drawerLayout=findViewById(R.id.drawer);
// homeInToolbar=findViewById(R.id.homeInToolbar);
toggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.open,R.string.close);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
FragmentManager manager=getSupportFragmentManager();
nav.setCheckedItem(R.id.menu_home);
FragmentTransaction transaction=manager.beginTransaction();
manager.beginTransaction().replace(R.id.framelayout,new HomeFragment()).commit();}
Main_Activity where the toogle is shown and after click on students, the image should look like
Add these 2 lines after setSupportActionBar(toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
This works for me.
In main_activity from where I calling the fragment, I have disabled toggle as shown in below code.
public void onClick(View v) {
toggle.setHomeAsUpIndicator(R.drawable.ic_back);
toggle.setDrawerIndicatorEnabled(false);
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,new FeeFragment(toggle)).addToBackStack(null).commit();
}
});
and where ever necessary, I can enable toggle by below code.
toggle.setDrawerIndicatorEnabled(true);
When I go back after changing it to a back button it disappears with the following code:
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(false);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mToggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerListener(mToggle);
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
I think that code should make the navigation drawer button to open menu to appear again, but it doesn't.
Any idea of what could be wrong in that code, so the navigation drawer button appears again?
Try like this:
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//.....
Also, you could use androidx.navigation.ui.NavigationUI package to setup your drawer menu.
Here are good articles about NavigationUI.
I am using mikepenz drawer library but I want to change default humburger icon and back arrow icon with my own drawable icon.
I have tried many times but I am unable to change the icon with my own icon .
Can anyone help me ?
new DrawerBuilder()
.withActivity(this)
.withTranslucentStatusBar(false)
.withActionBarDrawerToggle(false)
.withToolbar(toolbar)
.addDrawerItems(
//pass your items here
)
.build();
CODE TO SHOW THE HUMBURGER ICON:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
following is the code I found many times but i tried this also but it did not work
Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
And when I am searching I also come to know that you can not change the icon if you passing the toolbar in drawer builder so can anyone tell me what can I do?
I haven't tried it with that library but, try the following:
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
final Drawable upArrow = getResources().getDrawable(R.drawable.my_drawable);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
As per this link, you need to remove the withToolbar() from the DrawerBuilder and then you will have to handle open/close completely on your own.
For that you can do some thing like that
protected void onCreate(Bundle savedInstanceState) {
...
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(false);
toggle.setDrawerIndicatorEnabled(false);
toggle.setHomeAsUpIndicator(R.drawable.ic_custom_drawer_icon);
...
}
Also you had to add a toolbar navigation click listener to listen for click events on the custom drawer icon.
protected void onCreate(Bundle savedInstanceState) {
...
toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
drawer.openDrawer(GravityCompat.START);
}
}
});
...
}
You can update the icon dynamically whenever required as
toggle.setHomeAsUpIndicator(R.drawable.ic_new_icon);
Hope this will help you.
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar;
String Drawer_Open,Drawer_Close;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar=(Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//set it button icon
getSuppotActionBar().setDisplayHomeAsUpEnabled(true);
//set it makes button Clickble
getSuppotActionBar().setHomeButtonEnabled(true);
//set your own icon by using this code
getSuppotActionBar().setHomeAsUpIndicator(R.drawable.my_icon);
drawerLayout=(DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,Drawer_Open,Drawer_Close);
drawerLayout.serDrawerListener(actionBarDrawerToggle);
}
}
Again Do you have any quires get Consult me here.....,hope you got solution to your problem...
Try this by modify following:
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(true);
to
result.getActionBarDrawerToggle().setDrawerIndicatorEnabled(false);
this disable library default icon then change the icon...
getSupportActionBar().setHomeAsUpIndicator(R.drawable.my_drawable);
I am trying to set up my Navigation Drawer listener for my MainScreen activity. When I set everything up, my app crashes when I try use ActionBarDrawerToggle. Are there other ways of implementing this?
Unable to start activity ComponentInfo{koeksworld.com.firstandroidproject/koeksworld.com.firstandroidproject.UserMainPanel}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference
Here is some of the code in my activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_main_panel);
listener = new NavigationDrawerItemListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.groceryAppHomeToolbar);
homeDrawer = (DrawerLayout) findViewById(R.id.mainDrawerLayout);
homeListView = (ListView) findViewById(R.id.homeListView);
homeListView.setBackgroundColor(getResources().getColor(R.color.colorBlue));
navigationArray = getResources().getStringArray(R.array.navigationArray);
navigationAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
navigationArray);
homeListView.setAdapter(navigationAdapter);
homeListView.setOnItemClickListener(listener);
//The suspected culprit
drawerListener = new ActionBarDrawerToggle(this, homeDrawer, toolbar, R
.string
.open_drawer, R
.string.close_Drawer);
homeDrawer.setDrawerListener(drawerListener);
drawerListener.syncState();
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Make sure you are using the same id for the navigation drawer in findViewById(R.id.mainDrawerLayout) and in the layout file.
Seems mDrawerLayout is null when calling .setDrawerListener()
I have an application with the following hierarchy:
MainActivity (Shows list of dates)
|
ViewPagerFragment (Shows list of children for those dates)
|
ChildFragment (Detail View)
I am trying to implement the navigation view from the design support library, but am having trouble getting the actual navigation on the toolbar to work.
Here is the Main Activity toolbar:
Here is the ViewPagerFragment after navigating there from the main activity, note that there is no back button...
Here is the desired toolbar:
I am adding fragments using the following code:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.content_frame, fragment, fragment.getClass().getName())
.addToBackStack(fragment.getClass().getName())
.commitAllowingStateLoss();
Here is the code related to activity startup:
protected void setupActionBar() {
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
And my navigation drawer setup:
protected void setupNavigationDrawer() {
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
}
Hitting the hardware back button on the device navigates back properly. I just can't seem to get the back arrow to show up on the drawer toggle... Any suggestions?
To disable the navigation drawer icon and show a different icon instead, you need to call setDrawerIndicatorEnabled(false) on your ActionBarDrawerToggle.
You may also need to call setHomeAsUpIndicator() to specify the icon you want to use instead of the "hamburger" icon.
Have you tried to reach the ActionBar of the Activity ?
in onViewCreated:
Toolbar mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
((Activity) getActivity()).setSupportActionBar(mToolbar);
((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);