I am getting error message : ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
But I have called this in my code , still getting same error and the toggle button is not visible.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//layout variables
mDrawerLayout = findViewById(R.id.main_drawer_layout);
mNavigationView = findViewById(R.id.main_nav_view);
mActionDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mActionDrawerToggle);
mActionDrawerToggle.syncState();
//changed as keeps throws NULLPOINTEXCEPTION
if(getSupportActionBar() != null) {
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// assigning ID of the toolbar to a variable
toolbar = (Toolbar) findViewById(R.id.my_toolbar);
// using toolbar as ActionBar
setSupportActionBar(toolbar); //set
ActionBar actionbar = getSupportActionBar(); //get
actionbar.setTitle("SharePixel");
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_home_menu);
}
setHomeAsUpIndicator() won't get executed as the if(getSupportActionBar() != null) condition shouldn't be valid in drawer-based activity, because it requires a custom Toolbar, and you call this condition before setting the custom toolBar.
This condition won't be achieved unless your activity theme has an ActionBar set in your styles/themes XML. (if you've no customized activity theme set, then it's inherited from the base application theme.
To fix this:
Make sure that your activity theme inherits from a NoActionBar decedent; for instance Theme.MaterialComponents.DayNight.NoActionBar (or if your activity doesn't have customized style; do that on the base application theme).
Remove the if(getSupportActionBar() != null) condition.
Related
I have a back arrow in my toolbar for an activity.
It shows up correctly when I land on the activity. But after I go back and come again to the same activity, it is not showing. **This activity is in a library that I am creating **. So when we import this library into another project and call this activity from the main project activity then this problem occurs. The code runs, but from consecutive activity calls, the arrow is not visible. I have to relaunch the whole app to see the arrow.
Activity Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setContentView(R.layout.activity_A);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);// code runs but arrow is not showing
}
}
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
#Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
Log.d(TAG, "onBackPressed: Finished!!");
}
Use this its working on my code.
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
Don't forget to update you manifest file setting up the parent activity name.
<activity
android:name=".WebActivity"
android:screenOrientation="portrait"
android:parentActivityName=".MainActivity"
/>
In my android app I have a default app theme which has no action bar since I am adding my own toolbar
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
So this works well with my other activities except settings activity, So as a quick fix I've decided to add a custom style for it via
<style name="SettingsMenu" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorBackground">#color/colorPrimaryDark</item>
<item name="textColor">#color/colorAccent</item>
</style>
and also in the manifest I've added
<activity
android:name=".backend.settings.SettingsActivity"
android:theme="#style/SettingsMenu" //theme here
android:label="#string/title_activity_settings">
</activity>
So in my settings activity which extends AppCompatPreferenceActivity i have
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
}
/**
* Set up the {#link android.app.ActionBar}, if the API is available.
*/
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
So the above still doesnt show the toolbar.
I have also tried adding a custom toolbar via
private void setupActionBar() {
ViewGroup rootView = (ViewGroup)findViewById(R.id.action_bar_root); //id from appcompat
if (rootView != null) {
View view = getLayoutInflater().inflate(R.layout.app_bar_layout, rootView, false);
rootView.addView(view, 0);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
ActionBar actionBar = getSupportActionBar();
//here assign the toolbar
if (actionBar != null) {
// Show the Up button in the action bar.
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
So in my both tries neither toolbar nor actionbar works
Where am i going wrong?
I have my app as a single Activity with different fragments. I navigate the fragments via the Navigation Drawer on the left. The thing is that I need to have different toolbars for each fragment.
What I did was set the toolbar in the layout of the fragment like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hectorviov.###.activity.HomeFragment">
<include
android:id="#+id/toolbar_logo"
layout="#layout/toolbar_logo" />
...
And set it as SupportActionBar on my fragment with with:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppCompatActivity act = (AppCompatActivity)getActivity();
Toolbar mToolbar = act.findViewById(R.id.toolbar_logo);
act.setSupportActionBar(mToolbar);
}
It works correctly, the thing is, it doesn't show the hamburger icon on the left of the action bar. It shows me a warning:
W/ActionBarDrawerToggle: DrawerToggle may not show up because NavigationIcon is not visible. You may need to call actionbar.setDisplayHomeAsUpEnabled(true);
So I tried different ways to do this after googling a lot. The last one I tried is:
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
And no matter what I try, it always gives me a NullPointerException:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hectorviov.###/com.hectorviov.###.activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
Please help!!
try with this
AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
ActionBar ab = activity.getSupportActionBar();
if (ab != null) {
ab.setDisplayHomeAsUpEnabled(true);
}
}
refer this link will help you.
doesn't show the hamburger icon?
you need to add ActionBarDrawerToggle object
public class DrawerActivity extends AppCompatActivity {
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private void setupDrawerLayout() {
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close);
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
So, I've this little activity and I want to add a back button (the standard arrow one) I search for the answers here and I found this little code:
public class PozosFluyentes extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pozos_fluyentes);
ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
}
This crash my app, so anyone could help me with this please! I know is something pretty usual but I new to android development. Thanks in advance.
It's probably you add an android.support.v7.widget.Toolbar and not using a Theme that includes NoActionBar. Theme.AppCompat.Light.NoActionBar like theme will fix the issue. But if it does not, share your xml, styles.xml and logcat to check error. With little info the first thing to look is this. Also you can check here and see you need to disable theme provided ActionBar
Replcae your code with below.
ActionBar actionBar = getSupportActionBar();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (actionBar!= null) { // you have to use actionBar (ActionBar Object) instead.
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
}
let me know it working or not.....Hope this will helps you...(:
Why does this seem so difficult to accomplish? I have a condition in which I want to dynamically change the action bar of my activity.
android.support.v7.app.ActionBar ab = getSupportActionBar();
ab.setTitle(getString(R.string.voted_on_by));
Why doesn't this change the title of my action bar to the string I want?
In onCreate I do this:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
assert getSupportActionBar() != null;
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
you can use this
((MainActivity) this).getSupportActionBar().setTitle(R.string.voted_on_by);
or
((AppCompatActivity)this).getSupportActionBar().setTitle("sub categories");