I have implemented Hamburger bar with App toolbar and both of them are working fine. Following is the snapshot of toolbar and hamburgerbar:
Hamburger bar
I can open this bar by sliding it but I also want to make it open by clicking on drawable icon (right top corner icon). How can i do that?
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
drawerFragment = (FragmentDrawer)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerFragment.setDrawerListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
I don't think so that I need to do some changes in layout files. What do I have to add in MainActivity file to make it possible?
I am newbie in Android code. Any help will be appreciable.
Using the Toolbar component should be fairly easy to achieve this by using a similar code to this:
Toolbar toolbar = (Toolbar) findViewById(R.id.home_toolbar);
toolbar.inflateMenu(R.menu.menu_home);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.action_settings) {
mDrawerLayout.openDrawer(Gravity.RIGHT); // where mDrawerLayout is your android.support.v4.widget.DrawerLayout in your activity's xml layout.
}
return false;
}
});
EDIT:
The key component here is the menu_home.xml file which goes to your res/menu folder. You can add your desired menu item there, customize it's icon and even more, add as many items as you'd like to have on the right side of the toolbar(Obviously handle the openDrawer() method on whichever menu item you need - the recommended one is the rightmost though).
Use the openDrawer() method.
private DrawerLayout mDrawerLayout;
...
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
...
mDrawerLayout.openDrawer(Gravity.END); // or whatever gravity the of the drawer you want to open
Use Activity's onOptionsItemSelected(MenuItem menuItem) method:
First of all, keep the reference to your DrawerLayout in a class field:
DrawerLayout drawerLayout;
Somewhere in onCreate put this:
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout)
And implement the method:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
// if you want the default back/home hamburger menu item just put android.R.id.home instead
if (menuItem.getItemId() == R.drawable.icon_navigation) {
drawerLayout.openDrawer(GravityCompat.END);
}
return super.onOptionsItemSelected(menuItem);
}
Related
I am working in library format and I have design different headers.
But the library should have some interfaces. So from main activity in the project, you call the library and send the array of icons that will be loaded dynamically in the toolbar of the header.
MainActivity.java code is:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView =
(NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
There is an option to load the array dynamically in the toolbar
You can set the icon of the items of your menu by calling the method setIcon(int) of the desired item. In this example there is only one item that changes its icon if the user liked or not a movie. "liked" is a boolean that is changed in other part of the code and then invalidateOptionsMenu() is called to recreate the menu (onCreateOptionsMenu is called after invalidation).
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.your_xml_menu, menu);
MenuItem item = menu.getItem(0);
if (liked) {
item.setIcon(R.drawable.liked);
} else {
item.setIcon(R.drawable.not_liked);
}
return super.onCreateOptionsMenu(menu);
}
You can also change the visibility of an item calling item.setVisible(false); or even inflate different xml for different configurations.
Try this one will help you:
You just crate menu item in your Activity then you can implement multiple icons.
Create a menu folder in your existing resource res folder.
Create a main.xml file in menu folder.
main.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/action_item_one"
android:title="Camera"
android:icon="#drawable/ic_menu_camera"
app:showAsAction="always" />
<item
android:id="#+id/action_item_two"
android:title="Send"
android:icon="#drawable/ic_menu_send"
app:showAsAction="always" />
</menu>
In your activity, override onCreateOptionsMenu() and onOptionsItemSelected() to work with option menus.
ActionBarActivity.java
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_camera) {
// Do something
return true;
}
if (id == R.id.action_send) {
// Do something
return true;
}
return super.onOptionsItemSelected(item);
}
I am new to java and android development, have only 3 apps under my belt.
The Problem:
When I click a navigation tab to take me to another activity, it says unfortunately app has stopped and brings me back to the launcher page..
Things I have already looked at fixing:
Grandle Console doesn't reference any errors in the code.
Grandle Console doesn't reference any missing dependancies.
I have ensured all versioning is correct.
I am not using depreciated methods.
The app works if I create my own navigation with buttons, this simple functionality will really streamline the app. It is an app for hiking to log gps with journal entries while mapping a hike using fine-location.
This is the first I have built with the navigation drawer. I have spent days on this problem alone and have finally decided to ask for help, hopefully someone can help out.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
String myurl="file:///android_asset/index.html";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(myurl);
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.activity_main) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
switch (id){
case R.id.activity_main:
Intent h= new Intent(MainActivity.this,MainActivity.class);
startActivity(h);
break;
case R.id.activity_entry:
Intent e= new Intent(MainActivity.this,EntryActivity.class);
startActivity(e);
break;
case R.id.activity_list:
Intent l= new Intent(MainActivity.this,ListActivity.class);
startActivity(l);
break;
case R.id.activity_bluetooth:
Intent b= new Intent(MainActivity.this,BluetoothActivity.class);
startActivity(b);
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
First of All, If you put the error log, then that would be very helpful to get quick help. However, as a new developer,
Gradle is a build system, it actually build your project according to
the configuration (managing versions, artifacts, dependencies, etc).
Gradle Console won't show the Errors, you'll find that from Android Monitor/Logcat.
You may mistakenly do some common mistakes
Declaration of activities in the manifest.
looking for an Id of Resources that not belongs to the current layout.
Null Value Exception.
I have created a program, I have the base nav bar extended like so:
public class navBar extends AppCompatActivity {}
The navbar code itself is too long to include, but it sets a toolbar, and sets a navbar and works great when I use it in most parts of my app.
However, when I try to add another navbar in the extended class it does something odd, when I press the points of interest button to open it and it runs this code:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.POI)
mDrawerLayout.openDrawer(mDrawerList);
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
It opens the drawer as requested, but leaves no dimming on the screen. Like so:
When I close the screen with the back button/points of interest button for example it lags a bit, dragging the white slowly but closes.
Even worse, when I had opened the drawer with a
However, when I use my finger to drag from right to left to open the drawer, it opens and functions perfectly.
Because my map class is rather long as well, I've tried to include only the important code:
public class Map2 extends navBar {
private Toolbar toolbar;
ActionBarDrawerToggle mDrawerToggle;
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.activity_maptest, frameLayout);
toolbar = (Toolbar) findViewById(R.id.nav_base_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
toolbar,
R.string.drawer_open,
R.string.drawer_close
) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
mDrawerList.clearChoices();
super.onDrawerClosed(view);
//getActionBar().setTitle(mTitle);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//getActionBar().setTitle(mDrawerTitle);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.right_drawer);
mNavigationDrawerItemTitles= getResources().getStringArray(R.array.navigation_drawer_items_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.right_drawer);
POIAdapter_without_image adapter2 = new POIAdapter_without_image(this, R.layout.points_of_interest, drawerItem2);
mDrawerList.setAdapter(adapter2);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggleChild.syncState();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_skill_view, menu);
return true;
}
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
}
else
super.onBackPressed();
}
Edit:
Whenever I select a point on the points of interest list, the dragging white line can be seen here moments after the actual drawer has closed:
The black flash mentioned appears to be android both setting the dim and unsetting the dim as soon as I close the drawer.
This has thoroughly confused me for a few nights now. Thank you all!
you setDrawerShadow to cast shadow to drawer
drawer_layout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
Update :
drawer_shadow is .png file you put in drawable folder.
START : when drawer opens from left side.
END : when drawer opens from right side
In the end, the answer was to call:
final FrameLayout mapView = (FrameLayout) getLayoutInflater().inflate(R.layout.activity_maptest, frameLayout);
and when I wanted to find anything use
mDrawerLayout2 = (DrawerLayout) mapView.findViewById(R.id.drawer_layout);
for example.
it was then necessary to call
mDrawerLayout2.closeDrawer(GravityCompat.END);
rather than
mDrawerLayout2.closeDrawer(mDrawerList);
To obtain the needed functionality
How to check whether the left button in action bar is menu button or back button? To enable back button we use following:
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
I want to know which button is enable at a particular time. I tried by using stack count but it didn't worked for me.
Menu icon
Back icon
Thanks
You mean this?
set ID:
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add((int groupId, int itemId, int order, charsequence title) .setIcon(drawable ID)
return true;
}
get ID:
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case Menu.First+1 :
break;
case Menu.First+2 :
break;
}
return true;
}
If you are using navigation drawer, then you can do this by knowing state of navigation drawer either open or closed.
Assuming you have defined a drawerlayout in XML:
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if(mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
//drawer is open
}
if your drawer is open it means you have back icon, else you have bar icon on action bar
I am trying to implement DrawerLayout. The drawer layout is working fine. But the icon displayed in Top Left corner is android.R.id.home. However I have mentioned my custom icon in ActionBarDrawerToggle as follows:
// Getting reference to the DrawerLayout
drawerLayout = (android.support.v4.widget.DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.drawer_list);
// Getting reference to the ActionBarDrawerToggle
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
**R.drawable.icon_top_menu**, R.string.drawer_open,
R.string.drawer_close) {
/** Called when drawer is closed */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
/** Called when a drawer is opened */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
// Setting DrawerToggle on DrawerLayout
drawerLayout.setDrawerListener(drawerToggle);
getActionBar().setIcon(R.drawable.icon_top_menu);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
What's the problem?
You should check your onoptionsItem selected, should missing there...............
as shown below:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
in place of '.action_settings' try 'icon_top_menu' your icon.......
you should use visibility true/false for the icon menu in each activity where you want to show your icon...and please describe your question properly what you want to do.
To
user1182217
okay i got your problem some what, hope this will help you.
as for icon id you are using "android.R.id.home" which is 'android id' by default,
in place of that use your icon id as R.id.home/ R.id.your_icon_id hope the only error as
per your problem description..........