I want to use ActionBarDrawerToggle without having custom toolbar instead using current actionBar
my code goes as follow :
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.grid);
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, actionbar, R.string.app_name, R.string.app_name);
I know I should use :
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
and use toolBar instead of action bar in ActionBarDrawerToggle but can I use ActionBarDrawerToggle without having new toolbar.
Error I get :
error: incompatible types: ActionBar cannot be converted to Toolbar
Solved that by removing actionBar my code now goes like this :
ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.grid, R.drawable.contact) {
#Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
// creates call to onPrepareOptionsMenu()
}
};
Related
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.
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();
}
I managed to create a drawer and have the hamburger sign but the hamburger is not working when tapped. Also how can I change the code so that my app has a transparent notification bar so that the color is same(or preferably a little darker) and one can see the app drawer opened in status bar. Something like this: Transparent status bar
FirstActivity.java:
public class FirstActivity extends AppCompatActivity {
DrawerLayout mDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false); //removes the package name from toolbar
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Window w = getWindow(); // in Activity's onCreate() for instance //Integration of app into status bar
// w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// }
// These lines are needed to display the top-left hamburger button
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Make the hamburger button work
mDrawer = (DrawerLayout) findViewById(R.id.DL);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawer,R.string.app_name,R.string.app_name){
#Override
public void onDrawerClosed(View drawerView) {
}
#Override
public void onDrawerOpened(View drawerView) {
}
};
mDrawer.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
// toasts the message when ListView item is clicked
ListView mDrawerListView = (ListView) findViewById(R.id.left_drawer);
mDrawerListView.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String drawerstring = ("Menu Item at position " + position + " clicked.");
mDrawer.closeDrawer(GravityCompat.START);
Toast.makeText(getApplicationContext(),drawerstring,Toast.LENGTH_SHORT).show();
}
});
}
You have to override onOptionsItemSelected and handle the home item to open the drawer. Its not done for you, because Android doesn't know what you're using that button for (home? back? hamburger? Something else?). The ActionDrawerToggle knows how to handle it if you want to just delegate it.
public void setContentView(int layoutResID)
{
DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
{
actionBar.setDisplayHomeAsUpEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view)
{
supportInvalidateOptionsMenu();
//drawerOpened = false;
}
public void onDrawerOpened(View drawerView)
{
supportInvalidateOptionsMenu();
//drawerOpened = true;
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
}
}
}
I want to add navigation drawer icon to this navigation drawer activity. now i have edited code but still now error app stops at startup
maybe you can try to add it to layout file modifying the xml code,or follow this tutorial for more information Android Sliding Menu
I am new to android and I am trying to learn by making a simple chat app in android studio.
I started by creating a navigation drawer. Now I want to add tabs to my app like this - chats / contact / calls below navigation drawer, but I could not find a simple step by step tutorial for doing this.
Before I started this chat app I worked on a simpler app and added some tabs to that old app like this:
public class MainActivity extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources resources = getResources();
TabHost tabHost = getTabHost();
// First tab
Intent intentTabOne = new Intent().setClass(this, TabOneActivity.class);
TabSpec tabSpecTabOne = tabHost
.newTabSpec("Tab One")
.setIndicator("", resources.getDrawable(R.drawable.icon_one_config))
.setContent(intentTabOne);
// Second tab
Intent intentTabTwo = new Intent().setClass(this, TabTwoActivity.class);
TabSpec tabSpecSecondTab = tabHost
.newTabSpec("Tab Two")
.setIndicator("", resources.getDrawable(R.drawable.icon_two_config))
.setContent(intentTabTwo);
// Third tab
Intent intentTabThree = new Intent().setClass(this, TabThree.class);
TabSpec tabSpecSent = tabHost
.newTabSpec("Tab Three")
.setIndicator("", resources.getDrawable(R.drawable.icon_three_invitations_config))
.setContent(intentTabThree);
// add all tabs
tabHost.addTab(tabSpecTabOne);
tabHost.addTab(tabSpecTabTwo);
tabHost.addTab(tabSpecTabThree);
//set Windows tab as default
tabHost.setCurrentTab(0);
}
But now my main activity looks like this:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed()...
Also, since TabActivity is now deprecated and I think I should use fragment, I got a little bit lost. I would really appreciate if someone could point me in the right direction. Thanks in advance.
I'd suggest to use a TabLayout that you can put below your Toolbar/Actionbar, so you can use it in parallel with the navigation drawer.
This answer shows how setup the Tabbar with a ViewPager. In short: You need to include the Design Support Library to use the TabLayout. In your XML layout, put the TabLayout and the ViewPager somewhere below the Toolbar, such that it fits your intentions. Then you can setup the different tabs with their own Fragments in your Activity.