My program currently allows me to open the navigation bar by sliding my finger but will not display a menu button so i can click the menu button to open it. I have the onPostCreate and onOptionsItem Overide functions but I do not believe they are ever called. How do I fix this problem.
My programs minimum API level is 8 so I don't know if that is a problem.Thank you!
Main Activity:
public class Home_Page extends ActionBarActivity implements AdapterView.OnItemClickListener{
NavigationDrawer drawerLayout;
ListView listViewLeft, listViewRight;
String selectedMenuItem;
MyListViewAdapter myListViewAdapter;
int[] images = {R.drawable.menu_icon, R.drawable.menu_icon, R.drawable.menu_icon, R.drawable.menu_icon};
String[] listViewLeftItems = {"Home", "Choice 2", "Choice 3", "Choice 4"};
Intent intent;
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home__page);
drawerLayout = new NavigationDrawer(this, (DrawerLayout) findViewById(R.id.drawerLayout), getSupportActionBar());
drawerLayout.createDrawer();
initializeVar();
myListViewAdapter = new MyListViewAdapter(this, images, listViewLeftItems);
listViewLeft.setAdapter(myListViewAdapter);
listViewLeft.setOnItemClickListener(this);
}
public void initializeVar(){
listViewLeft = (ListView) findViewById(R.id.drawerListLeft);
listViewRight = (ListView) findViewById(R.id.drawerListRight);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
drawerLayout.closeDrawers();
switch(position){
case 0:
selectListViewItemLeft(position);
new Thread() {
public void run() {
try {
intent = new Intent(Home_Page.this, Home_Page.class);
startActivity(intent);
finish();
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
break;
case 1:
selectListViewItemLeft(position);
new Thread() {
public void run() {
try {
Intent intent = new Intent(Home_Page.this, AddAthlete.class);
startActivity(intent);
finish();
}catch (Exception e){
e.printStackTrace();
}
}
}.start();
break;
case 2:
selectListViewItemLeft(position);
break;
case 3:
selectListViewItemLeft(position);
break;
}
}
public void selectListViewItemLeft(int position){
listViewLeft.setItemChecked(position, true);
selectedMenuItem = listViewLeftItems[position];
}
}
Navigation Drawer Class: (Custom class so can create new navigation bar in different activities)
public class NavigationDrawer extends Activity{
DrawerLayout drawerLayout;
ActionBarDrawerToggle drawerToggle;
Activity currentActivity;
android.support.v7.app.ActionBar actionBar;
NavigationDrawer(Context context, DrawerLayout drawerLayout, android.support.v7.app.ActionBar actionBar) {
this.currentActivity = (Activity) context;
this.drawerLayout = drawerLayout;
this.actionBar = actionBar;
}
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void createDrawer() {
drawerToggle = new ActionBarDrawerToggle(currentActivity, drawerLayout, R.drawable.menu_icon, R.string.drawer_open, R.string.drawer_closed) {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
actionBar.setTitle("Menu");
}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
actionBar.setTitle(getTitle());
}
};
drawerLayout.setDrawerListener(drawerToggle);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setIcon(android.R.color.transparent);
actionBar.setHomeButtonEnabled(true);
}
public void closeDrawers(){
drawerLayout.closeDrawers();
}
// Displays toggle button to expand drawer
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
Log.e("", "onPostCreate Run");
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(drawerToggle.onOptionsItemSelected(item)){
Log.e("", "onOptionsItemSelected Run");
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
Log.e("", "onConfigurationChanged Run");
}
}
In your activity's layout file, use drawer layout as your parent layout and add toolbar as child view. For ex:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical>
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"></include>
<!-- Add your Main Content Here -->
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left">
<!-- Add your Drawer layout content here -->
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
And your toolbar layout, tool_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar ` xmlns:android="http://schemas.android.com/apk/res/android"`
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/action_bar_color"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
</android.support.v7.widget.Toolbar>
Related
when i am opening navigation drawer by clicking hamburger icon present in toolbar it opens it but when i click the left arrow to close the navigation drawer nothing is happening it is not closing it .please help
here is my code
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
NavigationDrawerFragment navigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
toolbar=(Toolbar)findViewById(R.id.app_bar);
//findViewById(R.id.drawerlayout);
setSupportActionBar(toolbar);
navigationDrawerFragment= (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.nav_frag);
navigationDrawerFragment.setup(R.id.nav_frag,(DrawerLayout) findViewById(R.id.drawerlayout),(Toolbar)findViewById(R.id.app_bar));
getSupportActionBar().setDisplayShowHomeEnabled(true);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
/* if(actionBar!=null)
{
actionBar.setHomeAsUpIndicator(android.R.drawable.ic_dialog_dialer);
actionBar.setDisplayHomeAsUpEnabled(true);
}*/
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.cool_menu,menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id= item.getItemId();
if(id==android.R.id.home)
{ ((DrawerLayout)findViewById(R.id.drawerlayout)).openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
if(id==R.id.action_settings){
Toast.makeText(this, "this is setting", Toast.LENGTH_SHORT).show();
}
if(id==R.id.action_mail){
startActivity(new Intent(this,SubActivity.class));
}
if(id==R.id.action_help){
Toast.makeText(this, "this is action_help", Toast.LENGTH_SHORT).show();
}
return true;
}
}
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar"></include>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_frag"
android:name="com.example.adarsh.material.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
NavigationDrawerFragment.java
public class NavigationDrawerFragment extends Fragment {
private static final String FILE_NAME = "shared";
private static final String SHARED_KEY ="shared_key" ;
private DrawerLayout mDrawerLayout;
private View containerView;
private ActionBarDrawerToggle mDrawerToggle;
private boolean mUserLearnerDrawer;
private boolean mFromSaveInstances;
public NavigationDrawerFragment() {
// Required empty public constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); mUserLearnerDrawer=Boolean.valueOf(readFromSharedPreferences(getActivity(),SHARED_KEY,"false"));
if(savedInstanceState!=null){
mFromSaveInstances=true;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}
public void setup(int id,DrawerLayout drawerLayout, Toolbar toolbar) {
containerView=getActivity().findViewById(id);
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerLayout,R.string.open,R.string.close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//we are going to store the mUserLearnerDrawer in shared pref
//if it is saved then we will not show the drawer then we will show the drawer
//if the user has never seeen the drawer before
if(!mUserLearnerDrawer){
mUserLearnerDrawer=true;
saveToSharedPreferences(getActivity(),SHARED_KEY,mUserLearnerDrawer+"");
}
getActivity().invalidateOptionsMenu();// it going to make draw the action bar again
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
//never seen the drawer + never used it before
if(!mUserLearnerDrawer&&!mFromSaveInstances){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public void saveToSharedPreferences(Context context,String name,String value){
SharedPreferences sPrefs=context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sPrefs.edit();
editor.putString(name,value);
editor.apply();//apply is faster then the commit method .coz unlike commit it does not return that yes it is commited or not
}
public String readFromSharedPreferences(Context context,String name,String defaultValue){
SharedPreferences sPrefs=context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
return sPrefs.getString(name,defaultValue);
}
}
create a global variable
private boolean isDrawerOpen = false;
then in your onOptionItemSelected do something like this
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id= item.getItemId();
if(id==android.R.id.home)
{
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
if(isDrawerOpen){
isDrawerOpen = false;
drawaerLayout.closeDrawer(GravityCompat.START);
} else{
isDrawerOpen = true;
drawaerLayout.openDrawer(GravityCompat.START);
}
return true;
}
if(id==R.id.action_settings){
Toast.makeText(this, "this is setting", Toast.LENGTH_SHORT).show();
}
if(id==R.id.action_mail){
startActivity(new Intent(this,SubActivity.class));
}
if(id==R.id.action_help){
Toast.makeText(this, "this is action_help", Toast.LENGTH_SHORT).show();
}
return true;
}
My BaseDrawerActivity extends BaseActivity and I have a Toolbar in BaseActivity.
The toolbar shows with no issues in classes that extend BaseActivity, but ALL classes that extend BaseDrawerActivity just show a blank toolbar. The nav drawer works fine.
When I had one main activity class (BaseActivity + BaseDrawerActivity) the toolbar showed no problem and the nav drawer worked.
Why is my implementation here not showing the Toolbar? I debugged and getToolbar() is returning the toolbar for sure.
activity_base_drawer.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:windowContentOverlay">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include layout="#layout/call_to_action_banner"/>
</FrameLayout>
<ListView
android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
style="#style/NavDrawerListView" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/ToolbarOverlay"
android:popupTheme="#style/ToolbarOverlay"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:minHeight="?attr/actionBarSize"
android:elevation="10dp"/>
BaseDrawerActivity
public abstract class BaseDrawerActivity extends BaseActivity {
private static final int LAYOUT_ID = R.layout.activity_base_drawer;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private NavDrawerAdapter mNavDrawerAdapter;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawers();
} else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LAYOUT_ID);
setupNavDrawer();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
private void setupNavDrawer() {
final ListView navDrawerListView = (ListView) findViewById(R.id.navigation_drawer);
View header = getLayoutInflater().inflate(R.layout.nav_drawer_header, null, false);
mNavDrawerAvatarImageView = (ImageView) header.findViewById(R.id.avatar);
mNavDrawerUsernameTextView = (CustomTextView) header.findViewById(R.id.username);
navDrawerListView.addHeaderView(header);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
getToolbar(),
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
updateNavDrawerUserInfo();
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mNavDrawerAdapter = new NavDrawerAdapter(this);
navDrawerListView.setAdapter(mNavDrawerAdapter);
navDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
mDrawerLayout.closeDrawer(GravityCompat.START);
onNavigationDrawerItemSelected(position);
}
});
updateNavDrawerUserInfo();
}
private void updateNavDrawerUserInfo() {
final DatabaseHelper db = DatabaseHelper.getInstance(this);
if (db.doesUserExist(SharedPrefs.getUserId())) {
final User currentUser = db.getUser(SharedPrefs.getUserId());
if (currentUser != null) {
if (currentUser.getAvatarType() != null) {
try {
mNavDrawerAvatarImageView.setImageDrawable(getResources().getDrawable(
ViewUtil.getAvatarHeadDrawableId(this,
currentUser.getAvatarType())));
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
}
if (currentUser.getUsername() != null) {
mNavDrawerUsernameTextView.setText(currentUser.getUsername());
}
}
}
}
}
BaseActivity
private void setupToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("");
getSupportActionBar().setSubtitle("");
getSupportActionBar().setLogo(R.drawable.logo_toolbar);
}
mUpdatingSpinner = (ProgressBar) getLayoutInflater().inflate(
R.layout.toolbar_updating_spinner, null, false);
int dpInPixels = (int) getResources().getDimension(R.dimen.toolbar_updating_spinner);
Toolbar.LayoutParams spinnerLp = new Toolbar.LayoutParams(dpInPixels, dpInPixels,
Gravity.END);
mToolbar.addView(mUpdatingSpinner, spinnerLp);
}
I found the answer although I don't fully understand it.
I basically called setupToolbar() again in my BaseDrawerActivity right before I call setupNavDrawer() and for some reason this fixed it.
I think the reference to the Toolbar from BaseActivity wasn't retrieving correctly (even though I debugged and it had a reference to it). Another thing I tried is setSupportActionBar(mToolbar) right before setting up the nav drawer but that didn't work.
If anyone has any ideas as to why I have to setup the toolbar again in order for it to show I'd be glad to hear it!
go to the AndroidManifest.xml file and replace
android:theme="#style/AppTheme"
with
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
you have to use setSupportActionBar(Toolbar toolbar).
This question should help.
I added a navigation drawer to my toolbar and added the toolbar to my HomePage Activity. The Drawer seems to be working fine but only pulls out when I slide my finger below the hamburger icon. Otherwise, if I simply click on the icon nothing happens. Does anybody know why? I watched the following tutorial and ideally when the hamburger icon is clicked, the drawer should come out: https://www.youtube.com/watch?v=Lfc1q1dFB3E
Is there an onClick attribute that I'm missing? Here is my code:
public class HomePage extends AppCompatActivity implements View.OnClickListener {
private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
String[] drawerListItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBarHome);
drawerLayout = (DrawerLayout)findViewById(R.id.homePageDrawer);
drawerList = (ListView)findViewById(R.id.homePageList);
drawerListItems = getResources().getStringArray(R.array.activities);
drawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,drawerListItems));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0: {
Intent i = new Intent(HomePage.this, HomePage.class);
startActivity(i);
break;
}
case 1: {
Intent i = new Intent(HomePage.this, Allergy.class);
startActivity(i);
break;
}
}
drawerLayout.closeDrawer(drawerList);
}
});
drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,myToolbar,R.string.drawer_open,R.string.drawer_close)
{
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
syncState();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
invalidateOptionsMenu();
syncState();
}
};
drawerLayout.setDrawerListener(drawerToggle);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
drawerToggle.syncState();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}
default:return super.onOptionsItemSelected(item);
}
}
Here is the XML layout. I have one ImageView on my Homepage Activity. When the NavigationDrawer pulls out, I need it to cover the existing ImageView, so I placed it within a relative layout within my drawerlayout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/boardlayered"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBarHome"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
android:id="#+id/homePageDrawer"
android:layout_below="#+id/toolBarHome"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/dogPic"
android:background="#drawable/happy_dog"
android:layout_width="175dp"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp" />
</RelativeLayout>
<ListView
android:id="#+id/homePageList"
android:background="#android:color/white"
android:layout_gravity="start"
android:layout_width="305dp"
android:layout_height="match_parent"></ListView>
</android.support.v4.widget.DrawerLayout>
Found it. The issue is in this piece of code, which is directly responsible for the hamburger icon to perform an action:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.home:{
if (drawerLayout.isDrawerOpen(drawerList)){
drawerLayout.closeDrawer(drawerList);
}else{
drawerLayout.openDrawer(drawerList);
}
return true;
}
default:return super.onOptionsItemSelected(item);
}}
Specifically
case R.id.home:
Should be
case android.R.id.home:
This latter case syntax is what works
I think the problem here is with the lines:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
It seems this will try to use your toggle button as an 'up' navigation button instead, if you're trying to use the hamburger button to open and close your drawer you can remove these lines.
I'm trying to use NavigationDrawer on my activity (Home), but the content of activity is not shown.
home_activity.xml
<LinearLayout 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"
android:id="#+id/home_activity">
<Button
android:id="#+id/like_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="likeCounter"
android:background="#drawable/likecountgreen" />
</LinearLayout>
activity_drawer.xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="#+id/nav_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#fff"
android:choiceMode="singleChoice" />
HomeActivity.class
public class HomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
super.onCreateDrawer();
}
}
BaseActivity.class
public class BaseActivity extends ActionBarActivity implements AdapterView.OnItemClickListener {
private ListView drawerList;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private String[] options;
private TextView titleActionBar;
protected void onCreateDrawer() {
setContentView(R.layout.activity_drawer);
setupActionBar();
options = getResources()
.getStringArray(R.array.drawer_options);
drawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.nav_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options));
drawerList.setOnItemClickListener(this);
setupDrawer();
}
private void setupActionBar() {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.action_bar);
titleActionBar = (TextView) findViewById(R.id.title_action_bar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
titleActionBar.setText("App");
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
titleActionBar.setText("Options");
invalidateOptionsMenu();
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 2:
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
drawerLayout.closeDrawer(drawerList);
break;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.home_menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_search) {
return true;
}
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
What I've discovered is that drawer_activity is overlaying home_activity.
How can I solve it?
In onCreateDrawer you are setting a new layout, so your layout that you set in your MainActivity is not shown anymore. Here is how you can solve this:
HomeActivity:
public class HomeActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreateDrawer(R.layout.home_activity);
}
}
And in your BaseActivity:
protected FrameLayout content;
protected void onCreateDrawer(final int layoutResID) {
setContentView(R.layout.activity_drawer);
content = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, content, true);
setupActionBar();
options = getResources()
.getStringArray(R.array.drawer_options);
drawerLayout =
(DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.nav_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options));
drawerList.setOnItemClickListener(this);
setupDrawer();
}
Now the FrameLayout in your activity_drawer layout contains the layout of your HomeActivity.
I'm having an issue where my navigation drawer will not open, and for the life of me, I can't figure out what's causing it. Could someone take a look and possibly see something I'm missing?
public class BaseActivity extends Activity
{
public DrawerLayout drawerLayout;
public ListView drawerList;
public String[] layers;
private ActionBarDrawerToggle drawerToggle;
Intent twitch = new Intent(this, TwitchActivity.class);
Intent community = new Intent(this, CommunityActivity.class);
Intent esports = new Intent(this, ESportsActivity.class);
Intent home = new Intent(this, MainActivity.class);
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_layout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
final ActionBar actionBar = getActionBar();
drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_launcher, 0, 0)
{
public void onDrawerClosed(View view)
{
actionBar.setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView)
{
actionBar.setTitle(R.string.menu);
}
};
drawerLayout.setDrawerListener(drawerToggle);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
layers = getResources().getStringArray(R.array.layers);
drawerList = (ListView) findViewById(R.id.left_drawer);
drawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, android.R.id.text1,
layers));
drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
String selected = arg0.getItemAtPosition(pos).toString();
if(selected.equals("Twitch"))
startActivity(twitch);
if(selected.equals("Community"))
startActivity(community);
if(selected.equals("ESports"))
startActivity(esports);
if(selected.equals("Home"))
startActivity(home);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
My MainActivity
public class MainActivity extends BaseActivity {
private Spinner spinner;
public static String region;
public static String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
//..........
My drawer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Nav Drawer -->
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111111"/>
</android.support.v4.widget.DrawerLayout>