I'm using navigation drawer for a project, everything is working fine but one small problem, I always that arrow displayed not home button.
I tried everything found in the documentations, forums and here nothing is actually working.
What I tried so far:
mDrawerToggle.syncState();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
#Override
public void onPostCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onPostCreate(savedInstanceState, persistentState);
mNavigationDrawerFragment.getmDrawerToggle().syncState();
}
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setTitle(mTitle);
NavigationDrawerFragment:
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
i have been busy making a simple app with navigation drawer..don't use template there are deprecated methods..
This is completely working.. i created this example.. try this..
your MainActivity
public class MainActivity extends ActionBarActivity {
Toolbar toolbar;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.app_toolbar);
setSupportActionBar(toolbar);
Profile profile = (Profile) getSupportFragmentManager().findFragmentById(R.id.profileFragmentInMain);
profile.setUp((DrawerLayout) findViewById(R.id.drawer),toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
This is your Fragment..
public class Profile extends Fragment {
ActionBarDrawerToggle drawerToggle;
DrawerLayout mDrawerLayout;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.profile, container, false);
return view;
}
public void setUp(DrawerLayout drawerLayout, Toolbar toolbar) {
mDrawerLayout = drawerLayout;
drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, toolbar, R.string.open, R.string.close){
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActivity().invalidateOptionsMenu();
}
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item != null && item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) {
mDrawerLayout.closeDrawer(Gravity.RIGHT);
} else {
mDrawerLayout.openDrawer(Gravity.RIGHT);
}
return true;
}
return false;
}
};
mDrawerLayout.setDrawerListener(drawerToggle);
mDrawerLayout.post(new Runnable(){
#Override
public void run(){
drawerToggle.syncState();
}
});
}
}
this is all in classes..
now xml layouts..
activity_main.xml
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/drawer"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/app_toolbar"
layout="#layout/toolbar"/>
</LinearLayout>
<fragment
android:id="#+id/profileFragmentInMain"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/profile"
android:name="com.example.ajay.myapplication.Profile"
tools:layout="#layout/profile"/>
</android.support.v4.widget.DrawerLayout>
your profile.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:theme="#android:style/Theme.Holo.Light.DialogWhenLarge.NoActionBar">
</LinearLayout>
your toolbar.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="wrap_content"
android:id="#+id/app_toolbar" />
previews :
open the drawer
closed drawer
Additional Info : I have added toolbar which is Material Design implemetation of ActionBar. Its latest way to create Actionbar. You can use that. just in case, select Theme with NoActionbBar in your styles.xml file.
So I finally found the solution. I just took of this R.drawable.ic_drawer from:
drawerToggle = new ActionBarDrawerToggle(getActivity(), drawerLayout, R.drawable.ic_drawer, R.string.open, R.string.close);
And it worked like a charm.
Related
I have toolbar and framelayout(for fragments) in my main activity layout. I have to show the toolbar in every fragments. But the issue is each fragment has different background image that is dynamically changing. Toolbar menu icon is used for navigation drawer open and close operation. How I will handle these issue?
Before posting the code block let's first understand what are we are going to do.
1.Create a HomeActivity that holds all the fragments.
Here is the activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_home"
app:menu="#menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is the app_bar_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
the other xmls of navigationview -- activity_home_drawer.xml contains navigation drawer contents and nav_header_home.xml is the navigation drawer header layout.
2.Let's come to java part of the HomeActivity.java
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
private HomeBean bean;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
ButterKnife.bind(this);
initListener();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//this menu contains all the common menu item actions
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
//common options bar item click here i have notifications icon common for all the fragments
#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.
switch (item.getItemId()){
case R.id.action_notifications:
AppUtils.launchActivity(HomeActivity.this, NotificationActivity.class,intentData);
return false;
default:
return super.onOptionsItemSelected(item);
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
Fragment selectedFragment = null;
switch (item.getItemId()){
case R.id.nav_dashboard:
//you can cache this fragment in case of repeated creations
selectedFragment = new DashBoardFragment();
}
break;
}
if(selectedFragment != null){
displayFragment(selectedFragment);
DrawerLayout drawer = ButterKnife.findById(this,R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
return true;
}
private void displayFragment(Fragment fragment) {
if(fragment !=null){
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
}
#Override
public void initToolBar() {}
public void setToolbar(Toolbar toolbar){
setSupportActionBar(toolbar);
setActionBarToggle(toolbar);
}
private void setActionBarToggle(Toolbar toolbar) {
DrawerLayout drawer = ButterKnife.findById(this, R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
}
#Override
public void initListener() {
NavigationView navigationView = ButterKnife.findById(this,R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//as the first item is show by default
navigationView.getMenu().getItem(0).setChecked(true);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
Now as an example let see how the fragments are written.
public class SettingsFragment extends Fragment {
#BindView(R.id.toolbar)
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings,container,false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((HomeActivity)getActivity()).setToolbar(toolbar);
}
}
Corresponding fragment xml fragment_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="Fragment Name">
<android.support.design.widget.AppBarLayout
android:id="#+id/notification_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/settings"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_settings" />
</android.support.design.widget.CoordinatorLayout>
the corresponding xml content_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="#dimen/min_widget_padding"
android:id="#+id/nsv_settings_content"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
// your content goes here
</<android.support.v4.widget.NestedScrollView>
I hope this gives clear picture on how to do in case if you have any other doubts please comment.
Use this code. I helped
public class MyFragment extends Fragment {
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);// To enable actionbar in fragments
super.onCreate(savedInstanceState);
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
//Here it is necessary to connect the layout menu
inflater.inflate(R.menu.fragment2, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
My BaseDrawerActivity extends BaseActivity and I have a Toolbar in BaseActivity.
The toolbar shows with no issues in classes that extend BaseActivity, but ALL classes that extend BaseDrawerActivity just show a blank toolbar. The nav drawer works fine.
When I had one main activity class (BaseActivity + BaseDrawerActivity) the toolbar showed no problem and the nav drawer worked.
Why is my implementation here not showing the Toolbar? I debugged and getToolbar() is returning the toolbar for sure.
activity_base_drawer.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?android:windowContentOverlay">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include layout="#layout/call_to_action_banner"/>
</FrameLayout>
<ListView
android:id="#+id/navigation_drawer"
android:layout_width="#dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
style="#style/NavDrawerListView" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:theme="#style/ToolbarOverlay"
android:popupTheme="#style/ToolbarOverlay"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:minHeight="?attr/actionBarSize"
android:elevation="10dp"/>
BaseDrawerActivity
public abstract class BaseDrawerActivity extends BaseActivity {
private static final int LAYOUT_ID = R.layout.activity_base_drawer;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private NavDrawerAdapter mNavDrawerAdapter;
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public void onBackPressed() {
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)){
mDrawerLayout.closeDrawers();
} else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
} else {
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(LAYOUT_ID);
setupNavDrawer();
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
private void setupNavDrawer() {
final ListView navDrawerListView = (ListView) findViewById(R.id.navigation_drawer);
View header = getLayoutInflater().inflate(R.layout.nav_drawer_header, null, false);
mNavDrawerAvatarImageView = (ImageView) header.findViewById(R.id.avatar);
mNavDrawerUsernameTextView = (CustomTextView) header.findViewById(R.id.username);
navDrawerListView.addHeaderView(header);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
getToolbar(),
R.string.navigation_drawer_open,
R.string.navigation_drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
updateNavDrawerUserInfo();
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mNavDrawerAdapter = new NavDrawerAdapter(this);
navDrawerListView.setAdapter(mNavDrawerAdapter);
navDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
mDrawerLayout.closeDrawer(GravityCompat.START);
onNavigationDrawerItemSelected(position);
}
});
updateNavDrawerUserInfo();
}
private void updateNavDrawerUserInfo() {
final DatabaseHelper db = DatabaseHelper.getInstance(this);
if (db.doesUserExist(SharedPrefs.getUserId())) {
final User currentUser = db.getUser(SharedPrefs.getUserId());
if (currentUser != null) {
if (currentUser.getAvatarType() != null) {
try {
mNavDrawerAvatarImageView.setImageDrawable(getResources().getDrawable(
ViewUtil.getAvatarHeadDrawableId(this,
currentUser.getAvatarType())));
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
}
if (currentUser.getUsername() != null) {
mNavDrawerUsernameTextView.setText(currentUser.getUsername());
}
}
}
}
}
BaseActivity
private void setupToolbar() {
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle("");
getSupportActionBar().setSubtitle("");
getSupportActionBar().setLogo(R.drawable.logo_toolbar);
}
mUpdatingSpinner = (ProgressBar) getLayoutInflater().inflate(
R.layout.toolbar_updating_spinner, null, false);
int dpInPixels = (int) getResources().getDimension(R.dimen.toolbar_updating_spinner);
Toolbar.LayoutParams spinnerLp = new Toolbar.LayoutParams(dpInPixels, dpInPixels,
Gravity.END);
mToolbar.addView(mUpdatingSpinner, spinnerLp);
}
I found the answer although I don't fully understand it.
I basically called setupToolbar() again in my BaseDrawerActivity right before I call setupNavDrawer() and for some reason this fixed it.
I think the reference to the Toolbar from BaseActivity wasn't retrieving correctly (even though I debugged and it had a reference to it). Another thing I tried is setSupportActionBar(mToolbar) right before setting up the nav drawer but that didn't work.
If anyone has any ideas as to why I have to setup the toolbar again in order for it to show I'd be glad to hear it!
go to the AndroidManifest.xml file and replace
android:theme="#style/AppTheme"
with
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"
you have to use setSupportActionBar(Toolbar toolbar).
This question should help.
Getting wired output
When i click on TextView of Navigation Drawer click goes to fragment in back (Main Content Fragment)
Please someone help what wrong i am doing.
layout code
<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">
<!-- The toolbar -->
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="#string/app_name" />
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<!-- The main content view -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/content_main" />
</LinearLayout>
<!-- The navigation drawer -->
<fragment
android:id="#+id/left_drawer"
android:name="app.compiler.fragment.FragmentDrawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_drawer"
/>
</android.support.v4.widget.DrawerLayout>
Java code
public class ActivityMain extends AppCompatActivity {
DrawerLayout mDrawerLayout;
ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ide);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle= new ActionBarDrawerToggle(this, mDrawerLayout,toolbar, R.string.app_name, R.string.app_name)
{
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
FragmentMain fragmentMain = new FragmentMain();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.container, fragmentMain);
transaction.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = new MenuInflater(this);
inflater.inflate(R.menu.menu_main,menu);
return true;
}
#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);
}
#Override
public void onBackPressed() {
if(mDrawerLayout.isDrawerOpen(Gravity.START | Gravity.LEFT)){
mDrawerLayout.closeDrawers();
return;
}
super.onBackPressed();
}
}
Check code output in gif image below
Make sure that everything within your left-drawer-layout is clickable. Otherwise, clicks will be passed to the underlying view, in this case to your main-content. You can do so by setting an OnClickListener to the rootview of your FragmentDrawer:
myFragmentInsideTheDrawer.getView().setOnClickListener(new OnClickListener() {
#Override
public void onClick(View pView) {
// do nothing here, just intercept click-events
}
});
In inDrawerOpen method you can use yourcontentlayout.setEnabled(false) and in onDrawerClosed method yourcontentlayout.setEnabled(true)
Hope this helps!
Im using Android Studio 1.2
The icon drawer in actionbar is give me an error when I click then but is work fine If I open it sliding with the hand from the left to the rigth.
this is my layout where I have my drawer list from the left; the list of options is in listView "mimenu"
<RelativeLayout xmlns:android="xxxxxx"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:background="#ffffff"
tools:context="xxxx">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffffff">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="#+id/listaxx"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
></ListView>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="left"
>
<ListView
android:id="#+id/mimenu"
android:layout_width="match_parent"
android:layout_below="#+id/profileBox"
android:choiceMode="singleChoice"
android:layout_height="match_parent"
android:background="#ffffff"
android:divider="#eee"
android:dividerHeight="1dp"
/>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
In my class java
public class ListaPat extends Activity {
public ArrayList<Pat> lstPat= new ArrayList<Pat>();
private Pat_Adaptador adaptador;
private LinearLayout linearLayout;
ArrayAdapter<CharSequence> navigationDrawerAdapter;
ListView leftDrawerList;
String[] leftSliderData = new String[]{"test1","test2"};
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_patx);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
leftDrawerList = (ListView)findViewById(R.id.mimenu);
navigationDrawerAdapter= new ArrayAdapter<CharSequence>( ListaPat .this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
RellenarNoticias();
InicializarLista();
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayShowHomeEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle.syncState();
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
mDrawerLayout.closeDrawer(leftDrawerList);
}
else {
mDrawerLayout.openDrawer(leftDrawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You need to extend either ActionbarActivity, or AppCompatActivity, and use a Toolbar widget to be in the beginning of your Activity. In your activities on create, do the following:
public class YourActivity extends ActionBarActivity{
...
...
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
//Set toggle with toolbar now!
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolbar,
R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
And for your Toolbar that lives in your activity xml (the very first element for your layout!)
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:background="#color/primary_color"
android:title="#string/app_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:theme="CreateSomeStyleHereIfYouNeed"
app:popupTheme="#android:style/Theme.Holo.Light"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
</android.support.v7.widget.Toolbar>
I fix the problem changing this function
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if(mDrawerLayout.isDrawerOpen(leftDrawerList)) {
mDrawerLayout.closeDrawer(leftDrawerList);
}
else {
mDrawerLayout.openDrawer(leftDrawerList);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
New function that fix the problem
#Override public boolean onOptionsItemSelected(MenuItem item) {if (mDrawerToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); }
I'm trying to use the DrawerLayout with ActionBarDrawerToggle.
see my code below:
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getName();
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
FeedsFragment feeds = new FeedsFragment();
if (findViewById(R.id.main) != null) {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportFragmentManager().beginTransaction().add(R.id.main, feeds).commit();
} else if (findViewById(R.id.content) != null) {
NavDrawerFragment nav = new NavDrawerFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.nav, nav)
.add(R.id.content, feeds)
.commit();
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration config) {
super.onConfigurationChanged(config);
mDrawerToggle.onConfigurationChanged(config);
}
}
and my view:
<?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" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="#layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="#layout/nav" />
</android.support.v4.widget.DrawerLayout>
But when I run my project, the left menu (DrawerLayout) just show on my screen doesn't make the slider event.
Finaly works:
I just added the gravity="start" at my second view
<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" >
<FrameLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!--<include layout="#layout/nav" />-->
<fragment
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.primeirochute.com.primeirochute.fragment.NavDrawerFragment"
tools:layout="#layout/nav" />
</android.support.v4.widget.DrawerLayout>
Uses the function onOptionsItemSelected and control the drawer event:
public boolean onOptionsItemSelected(MenuItem item) {
int itemID = item.getItemId();
switch (itemID) {
case android.R.id.home: {
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
if (drawerLayout.isDrawerOpen(Gravity.LEFT)) {
drawerLayout.closeDrawers();
}
else {
drawerLayout.openDrawer(Gravity.LEFT);
}
}
break;
return true;
}
I hope it works for your case.