I am trying to implement a simple Navigation Drawer, with a custom drawer icon.
What I want - To use the custom drawer icon instead of the up caret.
What is the problem- The custom icon doesn't show up. It's the 'Up' caret, which is visible all the time.
What I have done-
Used Android studio to create a NavDrawer project.
Searched for similar probs on Stackoverflow. I've got references for onPostCreate() method, but i've taken care of that.
Used Android sample app to run the project.
See image- See the top left corner. The up caret is what I would like to replace with my icon
MainActivity
package com.deep.wealthtrackernew;
import android.content.res.Configuration;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import java.util.List;
public class MainActivity extends ActionBarActivity {
private ListView listView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.left_drawer);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer,
R.string.open_drawer, R.string.close_drawer) {
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
// getActionBar().setTitle();
supportInvalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getSupportActionBar().setTitle("Wealth Tracker");
supportInvalidateOptionsMenu();
}
};
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if(actionBarDrawerToggle.onOptionsItemSelected(item)){ return true; }
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
in your main activity's on create right after the setContentView do this:
//if you use support action bar it is
ActionBar actionBar=getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.newIcon);
//if you are not using support action bar it is
ActionBar actionBar=getActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.newIcon);
This solved my problem (as I had a BaseActivity, and declared the following in the MainActivity, which extends BaseActivity). Inside onCreate(Bundle savedInstanceState) method set the app icon as shown below:
android.support.v7.app.ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_drawer);
Related
package com.example.jaffer.firstapp;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
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();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I want it so that the buttons on the navigation drawer take me to an activity instead of a fragment, i tried so many things but couldn't make it work. can someone please help me, it will be greatly appreciated.
Here is an example based off of my comment:
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// some toast
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
// some position
if (position == 0) {
startActivity(new Intent(YOUR_ACTIVITY)); // put your activity here
}
}
});
I get this nullPointerException on runtime:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)'
on a null object reference
code from mainActivity:
package com.example.vasilis.spangreek;
import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import adapter.ExpandableListViewAdapter;
import model.NavDrawerItem;
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ExpandableListView mExpandableListView;
private ActionBarDrawerToggle mActionBarDrawerToggle;
//nav drawer Title
private CharSequence mDrawerTitle;
//used to store app titles
private CharSequence mTitles;
//slide menu items
private String[] navMenuItems;
private String[] navSubMenuItems;
private TypedArray navMenuIcons;
private List<NavDrawerItem> groupList;
private List<NavDrawerItem> childList;
private Map<NavDrawerItem, List<NavDrawerItem>> mapList;
private ExpandableListViewAdapter mAdapter;
ActionBar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitles = mDrawerTitle = getTitle();
//nav drawer icons
navMenuIcons = getResources().obtainTypedArray(R.array.nav_icons);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
createGroupList();
CreateChildList();
// Recycle the typed array
navMenuIcons.recycle();
mExpandableListView = (ExpandableListView)findViewById(R.id.list_slideMenu);
mAdapter = new ExpandableListViewAdapter(this, mapList, groupList);
mExpandableListView.setAdapter(mAdapter);
mActionBar = getActionBar();
// enabling action bar app icon and behaving it as toggle button
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setHomeButtonEnabled(true);
//toggle
mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.string.app_name, R.string.app_name) {
public void onDrawerClosed(View drawerView) {
mActionBar.setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
mActionBar.setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mActionBarDrawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
if(savedInstanceState == null) {
//displayView(0);
}
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mExpandableListView);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public void setTitle(CharSequence title) {
mTitles = title;
getActionBar().setTitle(mTitles);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mActionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mActionBarDrawerToggle .onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mActionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createGroupList() {
//load slide menu items
navMenuItems = getResources().getStringArray(R.array.nav_items);
groupList =new ArrayList<NavDrawerItem>();
for (int i = 0 ; i < navMenuItems.length ; i++ ) {
groupList.add(i , new NavDrawerItem(navMenuItems[i], navMenuIcons.getResourceId(i, -1)));
}
}
private void CreateChildList() {
mapList = new LinkedHashMap<NavDrawerItem, List<NavDrawerItem>>();
navSubMenuItems = getResources().getStringArray(R.array.nav_sub_items);
childList = new ArrayList<>();
for ( NavDrawerItem item : groupList) {
if(item.getTitle().equalsIgnoreCase("learning Spanish")) {
for (int i = 0 ; i < navSubMenuItems.length ; i ++) {
childList.add(i, new NavDrawerItem(navSubMenuItems[i]));
}
}
mapList.put(item,childList);
}
}
private void setGroupIndicatorToRight() {
/* Get the screen width */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
mExpandableListView.setIndicatorBounds(width - getDipsFromPixel(35), width
- getDipsFromPixel(5));
}
// Convert pixel to dip
public int getDipsFromPixel(float pixels) {
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
return (int) (pixels * scale + 0.5f);
}
}
the lines of code that have problem are :
mActionBar = getActionBar();
// enabling action bar app icon and behaving it as toggle button
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setHomeButtonEnabled(true);
i use : style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"
I have used the getSupportActionBar() but i haven't fount any solution
The cause of your issue is using MainActivity extend Activity with support theme style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar". It's incompatible things.
Which min sdk you need?
In your code having MainActivity extends Activity you don't need AppCompatTheme. Use name="AppTheme" parent="android:Theme.Light"
If you are using Theme.AppCompat.Light.DarkActionBar, you should extend your Activity from AppCompatActivity, and use getSupportActionBar().
Instead of:
public class MainActivity extends Activity {
use:
public class MainActivity extends AppCompatActivity {
and instead of:
getActionBar().setTitle(mTitles);
use:
getSupportActionBar().setTitle(mTitles);
This problem might be caused by your theme. Check it again, and make sure that it parent with Theme.AppCompat.Light.DarkActionBar.
<style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">true</item>
...
</style>
If your activity extends AppCompatActivity or ActionBarActivity, call getSupportActionBar().
Put assert getActionBar () != null; after mActionBar = getActionBar();
In my case, I forgot to init my Toolbar, so, before using getSupportActionBar, I had to do this:
appbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(appbar);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_menu);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
If you check this answer in 2019 like me , the problem is about your android manifest:
<application
android:theme="#style/Theme.AppCompat.Light.NoActionBar"/>
Check the documentation here:
https://developer.android.com/training/appbar/setting-up.html
You also take in mind the previous answer from TetianaDev:
Instead of:
public class MainActivity extends Activity {
use:
public class MainActivity extends AppCompatActivity {
and instead of:
getActionBar().setTitle(mTitles);
use:
getSupportActionBar().setTitle(mTitles);
This works for me.
You should try this one. I think it will work.
Toolbar toolbar = findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout = findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.rectagle_with_black_outer,
GravityCompat.START);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
toolbar, R.string.navigation_drawer_close,
R.string.navigation_drawer_close) {
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
This is clearly not the problem of OP, but other people may find it useful: for me the solution was to call setContentView(R.layout.activity_main) before the configuration of actionbar.
So I have a navigation drawer that handles my fragments. In those fragments I want to implement tabbed navigation. I have searched high and low to find out if this is possible. But I cannot find if it si. From looking at things I don't think it is as the tabbed fragment implementation requires a FragmentActivity and my navigation drawer cannot use FragmentActivitys.
NavDrawers can inflate classes that extend Fragment but not FragmentActivity, which is why I'm thinking this is unachievable. The swipe views on the android developers documentation (http://developer.android.com/training/implementing-navigation/lateral.html) here is why I'm not convinced I can do this. Unless there is a different way to implement navdrawers than that of what I'm doing.
But I also know that Gmail implements something similar. They have a NavDrawer implementation with a feature of tabbed navigation -- you can swipe between emails. Which gives me some hope...
Any direction would be awesome.
Here is my navdrawer implementation, the biggest thing to note is that I'm not using the v4.support library, I'm using the more recent library:
package us.theotts.apps.italiangrammar;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.support.v4.widget.DrawerLayout;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import us.theotts.apps.italiangrammar.NavDrawer.NavDrawerItem;
import us.theotts.apps.italiangrammar.NavDrawer.NavDrawerListAdapter;
/**
* http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/
* http://blog.kitesapp.co/moments-of-delight-navigation-drawer-depth/
* git test
*/
public class Home extends Activity {
//implements NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
//private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String TAG = "Home";
private ArrayList<NavDrawerItem> mNavDrawerItems = new ArrayList<NavDrawerItem>();
private NavDrawerListAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
this.mTitle = "Italian Grammar";
// Initialize our drawer
this.mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
this.mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for our drawer
this.mDrawerList.setAdapter(
new ArrayAdapter<String>(
this,
R.layout.redundant_text_view,
getResources().getStringArray(R.array.drawer_titles)
)
);
// Set the click listener
this.mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
for(String s : getResources().getStringArray(R.array.drawer_titles)) {
this.mNavDrawerItems.add(new NavDrawerItem(s));
}
// setting the nav drawer list adapter
this.mAdapter = new NavDrawerListAdapter(getApplicationContext(),
this.mNavDrawerItems);
mDrawerList.setAdapter(this.mAdapter);
this.mDrawerToggle = new ActionBarDrawerToggle(
this,
this.mDrawerLayout,
R.drawable.ic_drawer,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mTitle);
}
};
this.mDrawerLayout.setDrawerListener(this.mDrawerToggle);
// enabling action bar app icon and behaving it as toggle button
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
public void onSectionAttached(int number) {
mTitle = getResources().getStringArray(R.array.drawer_titles)[number-1];
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
if (this.mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// Highlight the selected item, change the title and close the drawer
this.mDrawerList.setItemChecked(position, true);
//setTitle(getResources().getStringArray(R.array.drawer_titles)[position]);
setTitle(getResources().getString(R.string.app_name));
getFragmentManager().beginTransaction()
.replace(R.id.content_frame, Fragment.instantiate(
this, getResources().getStringArray(R.array.drawer_classes)[position]))
.addToBackStack(null).commit();
this.mDrawerLayout.closeDrawer(this.mDrawerList);
}
public void setTitle(CharSequence title) {
this.mTitle = title;
getActionBar().setTitle(title);
}
#Override
public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
this.mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
is there any example project which use only Android Support Library (revision 18) to implement Navigation Drawer on Android +2.2 without using any additional library (ABS etc.) ? Here is the example which use ASL but it's work only on API +14 : http://developer.android.com/training/implementing-navigation/nav-drawer.html .
Here is my MainActivity:
package com.example.android.navigationdrawerexample;
import java.util.Locale;
import android.app.Activity;
//import android.app.Fragment;
//import android.app.FragmentManager;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.app.SearchManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] mPlanetTitles;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = mDrawerTitle = getTitle();
mPlanetTitles = getResources().getStringArray(R.array.planets_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mPlanetTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
/* ActionBarDrawerToggle ties together the the proper interactions
between the sliding drawer and the action bar app icon */
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.action_websearch:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.planets_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}
}
}
and of course i change minSdk to 8 but it still doesn't work.
First you need to extend ActionBarActivity instead of FragmentActivity
Second change your App Theme to #style/Theme.AppCompat or #style/Theme.AppCompat.Light
Third change every call to getActionBar() for getSupportActionBar() and invalidateOptionsMenu() for supportInvalidateOptionsMenu()
That should do, here is the google example using AppCompat-v7 i did to test it, I also used a custom adapter for the listview just ignore that :)
https://github.com/BradleyRL/NavDrawerExampleAppCompat-v7
all you have to do is just import the v4 support library in your project and you can just follow the example in the docs that you linked.
its the same thing for any version of android 2.2 and up, just substitute any fragment use with support fragment usage from the library
What makes you think it only works on API 14+? If it's the minSDKVersion in the Manifest in the downloadable example, I guess that's just to have the Holo Theme they use.
Just change minSDKVersion to 8 or so and fix what needs fixing to match it. The NavigationDrawer itself should work with SDKVersions down to 4 as it's in the v4 version of the support library.
I'm trying to follow this tutorial on how to create a Navigation Drawer, but I don't want to use Fragments to show new content after the user select an item from the drawer list. What's the best aproach to get around this problem?
I'm using API 10 which doesn't implements Fragments.
First, API 10 has access to fragments via the same Android Support package that contains the DrawerLayout. This has been around for over two years, and you should not be trying to mess with new things like DrawerLayout if you are unfamiliar with what Android has had for the past two years.
Second, there is nothing with DrawerLayout that is tied to fragments. Quoting the Web page that you linked to:
When the user selects an item in the drawer's list, the system calls onItemClick() on the OnItemClickListener given to setOnItemClickListener(). What you do in the onItemClick() method depends on how you've implemented your app structure.
If you read those two sentences closely, you will see that the word "fragment" appears in neither of them. That is because DrawerLayout is not tied to fragments. The sample code they show uses fragments, but that is merely sample code.
Hence, you are welcome to update your UI however you want:
execute a FragmentTransaction using the Android Support package's backport of fragments, or
start an activity, or
call setContentView() again on your existing activity, or
otherwise modify the UI of the existing activity (e.g., hide/show some widgets)
You may use also LayoutInflater class.
Create xml layout file.
Find the View to change using findViewById.
Remove all children from the found View using .removeAllViews() method.
Inflate xml layout content into found View using .inflate() method.
This is an example:
LinearLayout layoutToChange = (LinearLayout)findViewById(R.id.layout_to_change);
layoutToChange.removeAllViews();
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout newLayout = (LinearLayout)inflater.inflate(R.layout.new_layout, null);
layoutToChange.addView(newLayout);
basic drawer without fragment
package xxxxxx;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class loginhome extends AppCompatActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginhome);
Toolbar topToolBar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(topToolBar);
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
private void addDrawerItems() {
String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" };
mAdapter = new ArrayAdapter<String>(this, R.layout.drawer_items,R.id.label ,osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(loginhome.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
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();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_help) {
Toast.makeText(loginhome.this, "setting", Toast.LENGTH_LONG).show();
}
if(id == R.id.action_place){
Toast.makeText(loginhome.this, "Refresh App", Toast.LENGTH_LONG).show();
}
if(id == R.id.action_search){
Toast.makeText(loginhome.this, "Create Text", Toast.LENGTH_LONG).show();
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
draweritems.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="30sp"
android:background="#D3D3D3">
</TextView>
</LinearLayout>
toolbar.xml
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:id="#+id/toolbar"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
>
</android.support.v7.widget.Toolbar>
use this code
private void selectItem(int position) {
// Locate Position
switch (position) {
case 0:
startActivity(new Intent(this,TEST.class));
break;