Android: custom icon not displayed in ActionBarDrawerToggle of DrawerLayout - android

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..........

Related

Navigation Drawer Click stops app and takes back to launch Page

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.

Android DrawerLayout second actionbar does not dim the screen when button is pressed

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 open Side bar on icon click in Android?

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);
}

Drawer Indicator does not show

I can't have the Drawer Indicator display. Currently I have either nothing or the "<" at the top left corner of the screen depending of the actionBar settings. But I want the Drawer Indicator of the Nagivation Drawer instead.
I use :
v4.widget.DrawerLayout
v7.app.ActionBarDrawerToggle
but android.app.ActionBar (not the support 7 one).
Here is snippet of the code :
#Override
protected void onCreate(Bundle savedInstanceState)
{
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
//I tried all combinations unsuccessfully
....
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerLV = (ListView) findViewById(R.id.left_drawer);
drawer_Linearlayout = (LinearLayout) findViewById(R.id.drawer_Linearlayout);
drawerLV.setAdapter(new ArrayAdapter<String>(
this,
R.layout.layout_main_drawer_list_item,
mDrawerItems));
drawerLV.setOnItemClickListener(new ListView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
});
drawerToggle = new ActionBarDrawerToggle(this, drawer, R.string.drawer_open,R.string.drawer_close) {
#Override
public void onDrawerClosed(View view) {
actionBar.setTitle(mTitle);
}
#Override
public void onDrawerOpened(View drawerView) {
actionBar.setTitle(mDrawerTitle);
}
};
drawerToggle.setDrawerIndicatorEnabled(true);
drawer.setDrawerListener(drawerToggle);
}
I eventually fixed my problem.
I forgot to add the following callback in my Acticity :
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
By the way, Lollipop upgrade of v7.app.ActionBarDrawerToggle adds a nice effect when the navigation drawer is opening or closing. I recommend it.
I think you also must specify android.R.id.home it in the onOptionsItemSelected to make the back button visible :
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//the onClick for your back button
return true;
default:
return super.onOptionsItemSelected(item);
}
}
And you just need to use actionBar.setDisplayHomeAsUpEnabled(true);
UPDATE :
Just did a quick googling, take a look at this :
Display back button on action bar
Try my answer. I think it will solve your problem.
Sorry for my previous answer, I misread it. Think this is what you need:
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer, /* The image drawable you're missing */
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
The image is custom, also known as the hamburger.

Android - Delay expand of SearchView

I want to delay the expand of SearchView. I use a drawerlayout. If the search item is clicked when drawer open, then layout of my views become distorted (a disaster), so I want to close drawer first before expanding search View. Any idea how to achieve this?
I use the following code. I tried to intercept menuitem selected event, triggering drawer to close. And in DrawerListener, call the serchMenu.expandActionView();
I expect the searchView to expand after drawer completed closed, but these two actions always happen simultaneously, and during the transition, the layout of my views become extremely distorted as well. That why I what delay the expand of search event.
public boolean onOptionsItemSelected(MenuItem item) {
SearchView searchView = (SearchView) mSearchItem.getActionView();
if(item.getItemId() == R.id.search){
mDarwerLayout.closeDrawer(mLeftDrawer);
mSearchViewShouldExpand = true;
}
return true;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
.......
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDarwerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
0,
0
){
/** Called when a drawer has settled in a completely closed state. */
#Override
public void onDrawerClosed(View drawerView) {
if(mSearchViewShouldExpand){
mSearchItem.expandActionView();
mSearchViewShouldExpand = false;
}
}
};
// Set the drawer toggle as the DrawerListener
mDarwerLayout.setDrawerListener(mDrawerToggle);
}
I found the way to intercept expand event, it is in MenuItem.OnActionExpandListener. Just need to return false in this method
#Override
public boolean onMenuItemActionExpand(MenuItem item) {
Log.d("*******","onMenuItemActionExpand");
mDarwerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
if(mDarwerLayout.isDrawerOpen(mRightDrawer) || mDarwerLayout.isDrawerOpen(mLeftDrawer) ) {
mDarwerLayout.closeDrawer(mRightDrawer);
mDarwerLayout.closeDrawer(mLeftDrawer);
mSearchViewShouldExpand = true;
return false;
}
return true;
}

Categories

Resources