I am extending my NavigationObject class to my other activity classes but the drawer would not come out when I click on the menu.
Here is my NavigationObject:
public class NavigationObject extends ActionBarActivity implements OnClickListener{
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
ListView listView;
List<RowItem> rowItems;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.navdrawer);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
System.out.println("ERROR 1");
addDrawerItems();
setupDrawer();
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
return super.onCreateOptionsMenu(menu);
}
//junk
public void addDrawerItems() {
System.out.println("ERROR 2");
String[] drawer_Array = { "User Profile", "Setting", "Contact us", "Help", "Logout" };
String[] desc_Array = { "Private", "Change settings", "Ask us anything", "Simple Guide", "Logs out user" };
Integer[] image_Array = { R.drawable.user, R.drawable.setting, R.drawable.contactme, R.drawable.help, R.drawable.logout };
//mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, drawer_Array);
//mDrawerList.setAdapter(mAdapter);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < drawer_Array.length; i++) {
RowItem item = new RowItem(image_Array[i], drawer_Array[i], desc_Array[i]);
rowItems.add(item);
}
System.out.println("ERROR 3");
listView = (ListView) findViewById(R.id.navList);
CustomDrawerBaseAdapter adapter = new CustomDrawerBaseAdapter(this, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0){
Intent toProfile = new Intent(getApplicationContext(), userprofActivity.class);
startActivity(toProfile);
//Toast.makeText(getMsgActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
else if(position == 1){
//Toast.makeText(getMsgActivity.this, "Time for an Wake!", Toast.LENGTH_SHORT).show();
}
else if(position == 2){
//Toast.makeText(getMsgActivity.this, "Time for an Eat!", Toast.LENGTH_SHORT).show();
}
else if(position == 3){
//Toast.makeText(getMsgActivity.this, "Time for an Dance!", Toast.LENGTH_SHORT).show();
}
else if(position == 4){
//Toast.makeText(getMsgActivity.this, "Time for an Sleep!", Toast.LENGTH_SHORT).show();
}
}
});
System.out.println("ERROR 4");
}
public void setupDrawer() {
//getActionBar().setDisplayHomeAsUpEnabled(true);
//getActionBar().setHomeButtonEnabled(true);
System.out.println("ERROR 5");
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
// enabling action bar app icon and behaving it as toggle button
public void onDrawerOpened(View drawerView) {
System.out.println("ERROR 5");
super.onDrawerOpened(drawerView);
listView.bringToFront();
mDrawerLayout.requestLayout();
getSupportActionBar().setTitle("User Menu Option");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerClosed(View view) {
System.out.println("ERROR 6");
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
System.out.println("ERROR 6.5");
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
System.out.println("ERROR 7");
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(listView);
menu.findItem(R.id.action_write).setVisible(!drawerOpen);
menu.findItem(R.id.action_grabmsg).setVisible(!drawerOpen);
menu.findItem(R.id.action_mydiary).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
System.out.println("landscape");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
System.out.println("portrait");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//bundle = getIntent().getExtras();
//String language_chosen = bundle.getString("lang_pick");
if (id == R.id.action_write) {
Intent writeNewMsg = new Intent(getApplicationContext(), writeMsgActivity.class);
//writeNewMsg.putExtra("lang_pick", language_chosen);
startActivity(writeNewMsg);
}
else if(id == R.id.action_grabmsg){
Intent grabNewMsg = new Intent(getApplicationContext(), getMsgActivity.class);
//grabNewMsg.putExtra("lang_pick", language_chosen);
finish();
startActivity(grabNewMsg);
}
else if(id == R.id.action_mydiary){
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
System.out.println("ERROR 8"); //This is where the error is. It is returned but never used.
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
My problem is here:
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
System.out.println("ERROR 8"); //This is where the error is. It is returned but never used.
return true;
}
and here these methods were never called:
public void onDrawerOpened(View drawerView) {
System.out.println("ERROR 5.5");
super.onDrawerOpened(drawerView);
listView.bringToFront();
mDrawerLayout.requestLayout();
getSupportActionBar().setTitle("User Menu Option");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerClosed(View view) {
System.out.println("ERROR 6");
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
Here is the xml for the drawer called navdrawer.xml of NavigationObject class:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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">
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
</android.support.v4.widget.DrawerLayout>
UPDATED - STILL NEED ASSISTANCE
Here is where I use the extend on my other classes (This class do not have the drawer xml within their xml because I am inheriting from NavigationObject which has its own xml called navdrawer):
public class MainActivity extends NavigationObject implements OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.get_msg);
Problem: Everytime I click the option menu bar on the drawer it prints "8" from the NavigationObject class but it does nothing after that. There was no error in the code because all of the system out print I placed worked except for System.out.println("ERROR 5.5") and System.out.println("ERROR 6") which are used to open and close the drawer. The swipe does not work.
This was working when I shoved all of NavigationObject class inside my MainActivity activity class but now it's not working when I made the drawer as a separate class (NavigationObject class) for inheritance.
The menu bar is working fine. But the optionselected at "ERROR 8" does not return anything but still prints out so it's working but its doing nothing either.
Any help is very much appreciated, this is for my final year project and it's due very soon :) .