Navigation drawer not opening with extended BaseActivity - android

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>

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

Extended navigation drawer can't change activity. What's going on?

I have created a BaseActivity class with navigation drawer and the other class CheckList extends the BaseActivity to have a navigation drawer.
In BaseActivity, I can change the activity to CheckList through navigation drawer.
But in CheckList. Even I can see the drawer, it is not work for changing class.
BaseActivity:
public class BaseActivity extends AppCompatActivity{
protected DrawerLayout drawerLayout;
protected ListView drawerList;
protected ActionBarDrawerToggle drawerToggle;
protected CharSequence mDrawerTitle;
protected CharSequence mTitle;
protected String[] drawer_menu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initActionBar();
initDrawer();
initDrawerList();
}
protected void initActionBar() {
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
}
protected void initDrawer() {
setContentView(R.layout.drawer);
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerList = (ListView)findViewById(R.id.left_drawer);
mTitle = mDrawerTitle = getTitle();
drawerToggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle(mTitle);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle(mDrawerTitle);
}
};
drawerToggle.syncState();
drawerLayout.setDrawerListener(drawerToggle);
}
protected void initDrawerList() {
drawer_menu = this.getResources().getStringArray(R.array.drawer_menu);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.drawer_list_item, drawer_menu);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
}
//respond when click the ic_drawer
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
protected class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
protected void selectItem(int position) {
//Fragment fragment = null;
switch (position) {
case 0:
break;
case 1:
Intent main = new Intent();
main.setClass(getApplicationContext(), MainActivity.class);
startActivity(main);
finish();
break;
case 2:
Intent checklist = new Intent();
checklist.setClass(getApplicationContext(), CheckList.class);
startActivity(checklist);
finish();
break;
default:
break;
}
/*FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();*/
drawerList.setItemChecked(position, true);
setTitle(drawer_menu[position]);
drawerLayout.closeDrawer(drawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
CheckList:
public class CheckList extends BaseActivity implements OnClickListener {
Button addBtn;
ListView listView;
ArrayList<String> checkListItem;
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.checklist, drawerLayout);
addBtn = (Button)findViewById(R.id.addBtn);
listView = (ListView)findViewById(R.id.listItems);
addBtn.setOnClickListener(this);
checkListItem = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, checkListItem);
listView.setAdapter(adapter);
checkListItem.add(getString(R.string.cl_passport));
checkListItem.add(getString(R.string.cl_charger));
checkListItem.add(getString(R.string.cl_camera));
registerForContextMenu(listView);
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.checklist_contextmenu, menu);
}
#Override
public boolean onContextItemSelected(MenuItem item) {
final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.cl_contextmenu_delete:
checkListItem.remove(info.position);
adapter.notifyDataSetChanged();
return true;
case R.id.cl_contextmenu_edit:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckList.this);
alertDialog.setTitle(getString(R.string.cl_editItem));
final EditText edit = new EditText(this);
edit.setText(checkListItem.get(info.position));
edit.setSelectAllOnFocus(true);
alertDialog.setView(edit);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkListItem.set(info.position, edit.getText().toString());
adapter.notifyDataSetChanged();
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.create();
alertDialog.show();
return true;
default:
return super.onContextItemSelected(item);
}
}
#Override
public void onClick(View v) {
if(v==this.addBtn) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(CheckList.this);
alertDialog.setTitle(getString(R.string.cl_input));
final EditText checkListInput = new EditText(this);
alertDialog.setView(checkListInput);
alertDialog.setCancelable(false);
alertDialog.setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
checkListItem.add(checkListInput.getText().toString());
}
});
alertDialog.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.create();
alertDialog.show();
}
}
}
I can't simply change the class to MainActivity in CheckList. But in BaseActivity, I can change to both MainActivity and CheckList.
Many thanks !!
Updated
This is my drawer.xml file
<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">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- Add content here -->
</FrameLayout>
<!-- The navigation drawer -->
<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"/>
</android.support.v4.widget.DrawerLayout>
You're unable to click on the drawer's list items because you're inflating the CheckList Activity's layout directly into the DrawerLayout of the BaseActivity. This is causing it to be "on top" of all of the DrawerLayout's child Views, and your clicks won't propagate through to the drawer ListView underneath. Instead, you want to inflate CheckList's layout into the content_frame FrameLayout within the DrawerLayout.
In the BaseActivity class add a protected field for the FrameLayout, and initialize it just like the other Views.
public class BaseActivity extends AppCompatActivity {
protected DrawerLayout drawerLayout;
protected ListView drawerList;
protected FrameLayout contentFrame;
...
protected void initDrawer() {
setContentView(R.layout.drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerList = (ListView) findViewById(R.id.left_drawer);
contentFrame = (FrameLayout) findViewById(R.id.content_frame);
...
}
}
Then change the inflate() call in CheckList as follows:
getLayoutInflater().inflate(R.layout.checklist, contentFrame);

Navigation Drawer in multiple activities does not work

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.

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>

Categories

Resources