Navigation Drawer in multiple activities does not work - android

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.

Related

Navigation Drawer on all activities - my app does not show the map as an activity from drawer

I am trying to create an android app that uses Google Maps and tried to add it into an Navigation Drawer on all activities app but the map doesn't show. I am trying to develop an app that uses the drawer to navigate between activities, one of them being created using Google Maps. The map activity starts, a toast message with "Map is ready" is present, and when I click on the grey screen(where the map should appear), I get the toast message telling me the longitude and latitude, which means the map is working somehow, on the back of the layout. I believe the situation is caused by some miss-written layout text, but I couldn't manage to find the cause so I will add the xml's files here, maybe someone could spot a mistake that I didn't see. I also tried the put the fragment containing the map last on the xml, as I read that this may be the cause of not showing the map, but still no change. In the MapsActivity.java i also have a linearlayout.setVisibility(View.GONE), which if I change to VISIBLE, it appears on the screen, over the inexistent map. Thanks!
EDIT: I have tried a simplified method to just show the map, without any other functions but the situation is the same, the map won't load. The manifest has all the permissions needed and also dependencies. I used those files
:
FirstActivity.java
package com.example.navigationdrawer2;
import android.content.res.TypedArray;
import android.os.Bundle;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
public class FirstActivity extends BaseActivity implements OnMapReadyCallback {
private String[] navMenuTitles;
private TypedArray navMenuIcons;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync( this);
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
set(navMenuTitles, navMenuIcons);
}
#Override
public void onMapReady(GoogleMap googleMap) {
}
}
Activity_first.xml:
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context="com.example.navigationdrawer2.SecondActivity"
class="com.google.android.gms.maps.SupportMapFragment"/>
</LinearLayout>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#e5e5e5"
android:dividerHeight="1dp"
android:background="#d6d6d6"/>
</androidx.drawerlayout.widget.DrawerLayout>
As the FirstActivity extends BaseActivity, i will add that .java too.
BaseActivity.java:
public class BaseActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
protected RelativeLayout _completeLayout, _activityLayout;
private CharSequence mDrawerTitle;
private Menu menuObject;
private CharSequence mTitle;
Toolbar toolbar;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer);
}
public void set(String[] navMenuTitles, TypedArray navMenuIcons) {
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
if (navMenuIcons == null) {
for (int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i]));
}
} else {
for (int i = 0; i < navMenuTitles.length; i++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[i],
navMenuIcons.getResourceId(i, -1)));
}
}
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
setupDrawerToggle();
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.menu,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
switch (position) {
case 0:
Intent intent = new Intent(this, FirstActivity.class);
startActivity(intent);
finish();
break;
case 1:
Intent intent1 = new Intent(this, SecondActivity.class);
startActivity(intent1);
finish();
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
void setupDrawerToggle(){
mDrawerToggle = new androidx.appcompat.app.ActionBarDrawerToggle(this,mDrawerLayout, R.string.app_name, R.string.app_name);
mDrawerToggle.syncState();
}
}
#Override
protected void onCreate(Bundle
savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.help, null, false);
mDrawer.addView(contentView, 0);
}
in onCreate of YourActivity don't call setContentView instead do above snippet:
FrameLayout convertView = inflator.inflate(R.layout.child_rows, parent, false);
you must be change "parent"="framLayout" you need to add framLayout in drawer activity and give reference.
In case of Activity you have to use setContentView instead of Inflater. Try like below:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
....
}

Navigation Drawer with toolbar Implementation in multiple activites latest code

i created navigation drawer in base activity and extends in main activity but there is no result in main activity
Here is my code:
BaseActivity Java code:
public class BaseActivity extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ListView mDrawerList;
String[] items;
ActionBarDrawerToggle mDrawerToggle;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.drawer_list);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
items = getResources().getStringArray(R.array.menu);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, items));
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close){
};
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
});
}
public void selectItem(int position){
Intent intent;
switch (position)
{
case 0:
intent = new Intent(this,OneActivity.class);
startActivity(intent);
break;
case 1:
intent = new Intent(this,TwoActivity.class);
startActivity(intent);
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
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);
}
}
And here is my Xml Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:id="#+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorAccent"
toolbar:navigationIcon="#drawable/ic_navigation">
</android.support.v7.widget.Toolbar>
<android.support.v4.widget.DrawerLayout
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" />
<ListView
android:id="#+id/drawer_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/colorAccent"
android:choiceMode="singleChoice"
android:divider="#android:color/holo_blue_dark"
android:dividerHeight="1dp">
</ListView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
And here is MainActivity:
public class MainActivity extends BaseActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_main);
}
}
suggest me the right answer
create a constructor in baseActivity class and call your defined functions of baseActivity class in constructor and than call that constructor in your MainActivity class function onCreate(Bundle savedInstanceState).

The issue with toolbar and navigation View

I have one activity, inside I have toolbar and navigationView.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_page_activity);
getFragment(new MainFragment());
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mNavigationView = (NavigationView) findViewById(R.id.main_drawer);
mNavigationView.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_widget);
drawerToggle
= new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.drawer_open, R.string.drawer_close);
mDrawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
Log.i("StartPageActivity", "onPostCreate");
drawerToggle.syncState();
}
When starting activity at first called Main Fragment.
When I select item in NavigationView opens new Fragment.
when appear second fragment, inside I use
ActionBar a = ((AppCompatActivity) getActivity()).getSupportActionBar();
a.setDisplayHomeAsUpEnabled(true);
Up arrow appeared, but when I click on this Up arrow opens NavigationVIEW!!!
in fragment inside onOptionsItemSelected I HAVE
case android.R.id.home:{
Log.i("One", "Dude");
}
The program does not comply with this code. It does not come to it. I think the problem inside onCreate (activity). Maybe it conflict between ActionBarDrawerToggle, NavigationView and Toolbar.
ADD second fragment
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.reminder_list_fragment, container, false);
getActivity().setTitle("Fragment1");
mRecyclerView = (RecyclerView)v. findViewById(R.id.my_recycler_view);
//{RecyclerAdapter}
ActionBar a = ((AppCompatActivity) getActivity()).getSupportActionBar();
a.setDisplayHomeAsUpEnabled(true);
a.setHomeButtonEnabled(true);
return v;
}
#Override
public void onResume() {
super.onResume();
mRecyclerView.getAdapter().notifyDataSetChanged();
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
inflater.inflate(R.menu.reminder_toolbar, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.d("Testing", "ID == " + id);
switch (id) {
case R.id.add_information : {
Forget f = new Forget();
ForgetLab.get(getActivity()).addForget(f);
FragmentManager fm = getFragmentManager();
Fragment mFragment = fm.findFragmentById(R.id.fragment_container);
Bundle bundle = new Bundle();
bundle.putSerializable(ReminderFragment.EXTRA_FORGET_ID, f.getId());
if (mFragment != null) {
mFragment = new ReminderPagerFragment();
mFragment.setArguments(bundle);
fm.beginTransaction().addToBackStack(null)
.replace(R.id.fragment_container, mFragment)
.commit();
}
return true;
}
case android.R.id.home:{
Log.i("One", "Dude");
}
}
return super.onOptionsItemSelected(item);
}
You should overRide onOptionsItemSelected(MenuItem item) and setHasOptionsMenu(true) should be called inside onCreate(Bundle savedInstanceState)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(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 == android.R.id.home) {
getActivity().onBackPressed();
}
return super.onOptionsItemSelected(item);
} I hope this works....
Use this complete example.
public abstract class BaseActivity extends AppCompatActivity{
protected Toolbar toolbar;
protected User mMe;
protected DrawerLayout mDrawerLayout;
protected ActionBarDrawerToggle drawerToggle;
#Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
mMe = SessionUtils.getUser();
if (mMe == null) {
return;
}
initToolbar();
initInstances();
}
private void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
private void initInstances() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
if (mDrawerLayout != null) {
final NavigationView navigationView = (NavigationView) findViewById(R.id.navigation);
ImageView profileImage = (ImageView) navigationView.findViewById(R.id.profileImage);
profileImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ActivityUtils.launchProfileActivity(BaseActivity.this, SessionUtils.getUser());
}
});
TextView name = (TextView) navigationView.findViewById(R.id.name);
final String fullName = StringUtils.getFullName(mMe.firstName, mMe.lastName);
name.setText(fullName);
GlideUtils.load(profileImage, mMe.getImage(), GlideUtils.getCircularFallbackDrawable(this, fullName), new CircleTransformGlide(this));
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int itemId = menuItem.getItemId();
switch (itemId) {
case R.id.messages:
ActivityUtils.launchMessagesFragment(BaseActivity.this);
LogUtils.LOGD(">>Menu", "Messages");
break;
// case R.id.notification:
// LogUtils.LOGD(">>Menu", "Notification");
// break;
case R.id.friends:
LogUtils.LOGD(">>Menu", "find friends");
ActivityUtils.launchFriendsActivity(BaseActivity.this);
break;
case R.id.termsOfService:
ActivityUtils.launchTermsOfServiceFragment(BaseActivity.this);
break;
case R.id.friendsRrequests:
LogUtils.LOGD(">>Menu", "friendsRrequests");
ActivityUtils.launchFriendsRequests(BaseActivity.this);
break;
case R.id.logout:
LogUtils.LOGD(">>Menu", "Logout");
DialogUtils.LogoutConfirmDialogFragment logoutConfirmDialogFragment = new DialogUtils.LogoutConfirmDialogFragment();
logoutConfirmDialogFragment.show(getSupportFragmentManager(), "Logout Confirmation Fragment");
// LoginManager.reset(BaseActivity.this);
break;
}
// menuItem.setChecked(true);
// if (R.id.friends == itemId) {
// mDrawerLayout.postDelayed(new Runnable() {
// #Override
// public void run() {
// mDrawerLayout.closeDrawers();
// }
// }, 1000);
// } else {
// mDrawerLayout.closeDrawers();
// }
return true;
}
});
drawerToggle = new DrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name, R.string.app_name, new DrawerToggleListener() {
#Override
public void OnDrawerClose() {
}
#Override
public void OnDrawerOpen() {
}
});
mDrawerLayout.setDrawerListener(drawerToggle);
final ActionBar supportActionBar = getSupportActionBar();
if (supportActionBar != null) {
supportActionBar.setHomeButtonEnabled(true);
supportActionBar.setDisplayHomeAsUpEnabled(true);
}
}
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
if (drawerToggle != null)
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (drawerToggle != null)
drawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return (drawerToggle != null && drawerToggle.onOptionsItemSelected(item)) || super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
if (mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
private static class DrawerToggle extends ActionBarDrawerToggle {
DrawerToggleListener mToggleListener;
public DrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int openDrawerContentDescRes, int closeDrawerContentDescRes, DrawerToggleListener toggleListener) {
super(activity, drawerLayout, toolbar, openDrawerContentDescRes, closeDrawerContentDescRes);
mToggleListener = toggleListener;
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
mToggleListener.OnDrawerClose();
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
mToggleListener.OnDrawerOpen();
}
}
private static abstract class DrawerToggleListener {
public abstract void OnDrawerClose();
public abstract void OnDrawerOpen();
}
}
You Main activity must extends BaseActivity
public class MainActivity extends BaseActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (!LoginManager.isFirstFlowFinished(this)) {
LoginManager.launchCurrentFragment(this);
finish();
return;
}
Intent intent = getIntent();
handleIntent(intent);
setContentView(R.layout.activity_main_new);
LogUtils.LOGD(">>Intent", "onCreate");
}
#Override
public void onResume() {
super.onResume();
LocationTracker.promptIfNeededForEnableLocation(this);
CommonUtils.checkIfPlayServicesNeedToUpdate(this);
AppEventsLogger.activateApp(this);
}
#Override
protected void onPause() {
super.onPause();
AppEventsLogger.deactivateApp(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_search, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_search_icon:
ActivityUtils.launchSearchActivity(this);
break;
}
return true;
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
LogUtils.LOGD(">>Intent", "on handle Intent");
handleIntent(intent);
}
}
And here is activity_main_new.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?actionBarSize"
android:background="?colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" >
<TextView
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
android:text="Friends Locator"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>
<fragment
android:orientation="vertical"
android:id="#+id/viewPager"
android:name="com.macrotechnologies.friendslocator.ui.NearBySuggestionsFragment"
android:tag="com.macrotechnologies.friendslocator.ui.NearBySuggestionsFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:itemIconTint="#color/nav_item_icon_tint_color"
app:itemTextColor="#color/nav_item_text_color"
app:menu="#menu/navigation_drawer_items" />
and Navigation Menu item is as:
<?xml version="1.0" encoding="utf-8"?>
<group android:checkableBehavior="none">
<item
android:id="#+id/friends"
android:icon="#drawable/ic_navigation_chevron_right"
android:title="#string/friends_caps" />
<item
android:id="#+id/messages"
android:icon="#drawable/ic_navigation_chevron_right"
android:title="#string/messages" />
<item
android:id="#+id/friendsRrequests"
android:icon="#drawable/ic_navigation_chevron_right"
android:title="#string/friendsRequests" />
<!-- <item
android:id="#+id/notification"
android:icon="#drawable/ic_navigation_chevron_right"
android:title="#string/notifications" />-->
<!--<item-->
<!--android:id="#+id/about"-->
<!--android:icon="#drawable/ic_navigation_chevron_right"-->
<!--android:title="#string/about" />-->
<item
android:id="#+id/termsOfService"
android:icon="#drawable/ic_navigation_chevron_right"
android:title="#string/terms_of_service" />
<item
android:id="#+id/logout"
android:icon="#drawable/ic_navigation_chevron_right"
android:title="#string/log_out" />
</group>

Android Navigation Drawer not adding ActionBar menu button

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>

Navigation drawer not opening with extended BaseActivity

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>

Categories

Resources