Closing navigation drawer smoothly - android
I have implemented navigation drawer in a slightly different way in my app. But the drawer doesn't open or close smoothly. It lags in between. In Google apps, the navigation drawer is so smooth. How do I achieve it? This question is already asked here but it didn't meet my needs. Please help!
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:background="#000000"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Swipe from the left to open the drawer"
android:id="#+id/textView"
android:layout_gravity="center" />
</FrameLayout>
<LinearLayout
android:id="#+id/left_drawer_layout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<EditText
android:id="#+id/searchbar"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textColor="#bfc2d1"
android:singleLine="true"
android:padding="10dp"
android:background="#drawable/search_bar"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:imeOptions="flagNoExtractUi"
android:hint="Search" >
</EditText>
<RelativeLayout
android:id="#+id/empty"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_marginLeft="-65dp"
android:background="#111"
android:orientation="vertical"
android:layout_gravity="start" >
<TextView
android:id="#+id/empty_text"
android:text="#string/notfound"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:textColor="#ffababab"
android:textSize="15dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center">
</TextView>
</RelativeLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
Java
public class MainActivity extends FragmentActivity {
final String[] data ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Flourine","Noen","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium",
"Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einstenium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium",
"Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
final EditText searchBar = (EditText) findViewById(R.id.searchbar);
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.left_drawer);
final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.closeDrawer(linearLayout);
}
});
//filter list view after search instantly
searchBar.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
String searchedquery = cs.toString().replaceAll(" ", "");
MainActivity.this.adapter.getFilter().filter(searchedquery);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
//hide the keyboard after search on touch list view
navList.setOnScrollListener(new AbsListView.OnScrollListener() {
public void onScrollStateChanged(AbsListView view, int scrollState) {
//hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
}
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
navList.setEmptyView(findViewById(R.id.empty));
}
}
Here is what you can do to make the sliding smooth
Create the layout of the navigation Drawer separately
Call the Drawer from the fragment
Here is the code for better understanding
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
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:id="#+id/custom_drawer_layout">
<fragment
//this contains the layout of your fragment
android:id="#+id/drawer_fragement"
android:layout_width="260dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:name="adress to navigationdrawer class"
tools:layout="#layout/drawer_frag
//this line tells which layout to inflate ontop of the fragment
/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Now Create a loyout with name as drawer_frag which contains all your code that you want to display in you drawer
Hope it helps
Related
Add Navigation Drawer fragment to activity
I want to add my Navigation Drawer fragment to activity, but I want that the activity layout "behind" Navigation Drawer will stay the same. What i got Without open Navigation Drawer What I want Navigation Drawer fragment activity XML <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" > <RelativeLayout android:layout_width="280dp" android:layout_height="match_parent" android:id="#+id/drawerPane" android:layout_gravity="start"> <RelativeLayout android:id="#+id/logoBox" android:layout_width="match_parent" android:layout_height="100dp" android:background="#color/material_blue_grey_800" android:padding="8dp" > <ImageView android:id="#+id/g4aLogo" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="15dp" android:layout_centerInParent="true"/> </RelativeLayout> <ListView android:id="#+id/settingsList" android:layout_width="280dp" android:layout_height="match_parent" android:layout_below="#+id/logoBox" android:choiceMode="singleChoice" android:background="#ffffffff" /> </RelativeLayout> </android.support.v4.widget.DrawerLayout> activity XML <LinearLayout 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:layout_height="match_parent" android:id="#+id/activity_main" android:orientation="vertical" android:weightSum="10"> <LinearLayout android:layout_weight="1" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="0dp" android:id="#+id/searchBarLay" android:focusable="true" android:focusableInTouchMode="true"> //CONTENTS and layouts... </LinearLayout> </LinearLayout> Activity code part when I implement Navigation Drawer private void initSettingsDrawer() { Button settingsDrawerButton =(Button)findViewById(R.id.settingsDrawer); settingsDrawerButton.setOnClickListener(new View.OnClickListener() { #Override public void onClick(View v) { SettingsDrawer settingsDrawer = new SettingsDrawer(); FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction().add(R.id.activity_main, settingsDrawer).commit(); } }); }
android.support.v4.widget.DrawerLayout should be the parent/root layout. Your xml should be like this, <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" > <RelativeLayout android:layout_width="280dp" android:layout_height="match_parent" android:id="#+id/drawerPane" android:layout_gravity="start"> <RelativeLayout android:id="#+id/logoBox" android:layout_width="match_parent" android:layout_height="100dp" android:background="#color/material_blue_grey_800" android:padding="8dp" > <ImageView android:id="#+id/g4aLogo" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginTop="15dp" android:layout_centerInParent="true"/> </RelativeLayout> <ListView android:id="#+id/settingsList" android:layout_width="280dp" android:layout_height="match_parent" android:layout_below="#+id/logoBox" android:choiceMode="singleChoice" android:background="#ffffffff" /> </RelativeLayout> </android.support.v4.widget.DrawerLayout>
Add this library to your build.gradle file compile('com.mikepenz:materialdrawer:5.1.3#aar') { transitive = true } And then add this code in your Activity where you want the navigation drawer. public class MainActivity extends AppCompatActivity { private Drawer result = null; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); AccountHeader headerResult = new AccountHeaderBuilder() .withActivity(this) // .withHeaderBackground(R.drawable.nav_header) .addProfiles( new ProfileDrawerItem().withEmail("abc#gmail.com").withIcon(getResources(). getDrawable(R.mipmap.ic_launcher)) ) .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() { #Override public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) { return false; } }) .build(); result = new DrawerBuilder() .withActivity(this) .withToolbar(toolbar) .withActionBarDrawerToggleAnimated(true) .withAccountHeader(headerResult) .build(); result.addItem(new PrimaryDrawerItem() .withName(("Home")).withIcon(R.mipmap.ic_launcher). withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { #Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { return false; } })); result.addItem(new PrimaryDrawerItem() .withName(("Settings")).withIcon(R.mipmap.ic_launcher). withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { #Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { return false; } })); } }
Common Navigation Menu drawer In Android
I need to add navigation drawer menu in my android application. my application has many modules so i can't code each activity to display navigation menu. so i decided to put the navigation menu code in my base activity. so each activity extends this base activity. navigation drawer menu's are working properly but the problem is activity components are not working. i don't know what is happening. is there any changes needed? Thanks in advance. baseActivity.java public class BaseActivity extends ActionBarActivity { RelativeLayout fullLayout; DrawerLayout dLayout; #Override public void setContentView(int layoutResID) { fullLayout = (RelativeLayout) getLayoutInflater().inflate( R.layout.activity_base, null); FrameLayout frameLayout = (FrameLayout) fullLayout .findViewById(R.id.content_frame); ListView dList = (ListView) fullLayout.findViewById(R.id.left_drawer); dLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout); getLayoutInflater().inflate(layoutResID, frameLayout, true); setContentView(fullLayout); String[] menu = new String[] { "Home", "Android", "Windows", "Linux", "Raspberry Pi", "WordPress", "Videos", "Contact Us" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menu); dList.setAdapter(adapter); dList.setSelector(android.R.color.holo_blue_dark); dList.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView<?> arg0, View v, int position, long id) { dLayout.closeDrawers(); } }); }}; activity_base.xml <RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="#dimen/activity_vertical_margin" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" tools:context="sas.mobi.lakshmi.main.BaseActivity" > <FrameLayout android:id="#+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.v4.widget.DrawerLayout android:id="#+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="#+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#fff" android:choiceMode="singleChoice" android:divider="#android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout> homeActivity.java public class HomeAndSettingActivity extends BaseActivity { private Button btnAccount; private Button btnCollection; private Button btnOthers; private Button btnTempExit; private Button btnExit; private AdminDS adminDS; private AdminDO adminDO; private FinanceDS financeDS; private PartnerPaymentDS paymentDS; private GoogleCloudMessaging gcm; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home_and_setting); initializeComponents(); btnAccount.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), AccountRegHomeActivity.class); startActivity(intent); finish(); } }); btnCollection.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), CollectionHomeActivity.class); startActivity(intent); finish(); } }); btnOthers.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { Intent intent = new Intent(getApplicationContext(), OthersHomeActivity.class); startActivity(intent); finish(); } }); btnTempExit.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { finish(); } }); btnExit.setOnClickListener(new OnClickListener() { #Override public void onClick(View v) { financeDS = new FinanceDS(getApplicationContext()); boolean isUps = financeDS.closeActiveCurrentFinances(); if (isUps) { finish(); } } }); } /** * method to initialize components */ private void initializeComponents() { btnAccount = (Button) findViewById(R.id.btnAccount); btnCollection = (Button) findViewById(R.id.btnCollection); btnOthers = (Button) findViewById(R.id.btnOthers); btnTempExit = (Button) findViewById(R.id.btnTempExit); btnExit = (Button) findViewById(R.id.btnExit); }}; acitivity_home.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#android:color/white" android:orientation="vertical" > <Button android:id="#+id/btnAccount" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="?android:attr/dividerVertical" android:text="#string/btnAccount" android:textSize="14sp" /> <Button android:id="#+id/btnCollection" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="?android:attr/dividerVertical" android:text="#string/btnCollection" android:textSize="14sp" /> <Button android:id="#+id/btnOthers" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="?android:attr/dividerVertical" android:text="#string/btnOthers" android:textSize="14sp" /> <Button android:id="#+id/btnTempExit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="?android:attr/dividerVertical" android:text="#string/btnTempExit" android:textSize="14sp" /> <Button android:id="#+id/btnExit" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="1dp" android:background="?android:attr/dividerVertical" android:text="#string/btnExit" android:textSize="14sp" /> this home activity and common navigation drawer menu's showing properly, but the component inside the home activity is not working.but the drawer components are working.
Change the main xml into this way and load the sidemenu into side menu frame: <?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" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="#dimen/activity_vertical_margin" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" tools:context="sas.mobi.lakshmi.main.BaseActivity" > <FrameLayout android:id="#+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> <FrameLayout android:id="#+id/side_menu" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.DrawerLayout>
Hi use fragments to Achive the Navigation Draw use cannot do so good with activities as its very simple you need replace fragment on each call of on your menu please find the below link http://www.androidhive.info/2015/04/android-getting-started-with-material-design/ or you can create new project in android studio by selecting nav ui from a new project
As mencioned here you need to move your FrameLayout with id content_frame inside DrawerLayout something like that <RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="#dimen/activity_vertical_margin" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" tools:context="sas.mobi.lakshmi.main.BaseActivity" > <android.support.v4.widget.DrawerLayout 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" /> <!-- The navigation drawer --> <ListView android:id="#+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#fff" android:choiceMode="singleChoice" android:divider="#android:color/transparent" android:dividerHeight="0dp" /> </android.support.v4.widget.DrawerLayout>
Swipe Refresh Layout onRefresh working but not displaying refresh indicator
I've implemented a swipe refresh layout onto one of my List Views however the view is not sliding down when I swipe and the icon is not visible. Even though it seems it doesn't work, it does actually fire off the OnRefresh listener. Also I think its worth mentioning that I'm using the ListView on a fragment so i'm initializing the listener in the fragment activity which is displayed below. Swipe Refresh XML <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/listView" android:layout_gravity="center_horizontal" android:dividerHeight="1sp" android:translationZ="5dp" android:background="#color/background_gray" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:clickable="true" /> </android.support.v4.widget.SwipeRefreshLayout> Fragment Activity #Override public void onViewCreated(View view, #Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh_layout); ListView lView = (ListView) getActivity().findViewById(R.id.listView); mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { #Override public void onRefresh() { mSwipeRefreshLayout.setRefreshing(true); ((MainActivity) getActivity()).refresh(); new Handler().postDelayed(new Runnable() { #Override public void run() { mSwipeRefreshLayout.setRefreshing(false); } }, 5000); } }); lView.setOnScrollListener(new AbsListView.OnScrollListener() { #Override public void onScrollStateChanged(AbsListView absListView, int i) { } #Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (firstVisibleItem == 0) mSwipeRefreshLayout.setEnabled(true); else mSwipeRefreshLayout.setEnabled(false); } }); }
I solved it, it appears that this is a current bug with the SwipeRefreshLayout. It will only work when Listview is wrapped inside a FrameLayout. <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/swipe_refresh_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:id="#+id/listView" android:layout_gravity="center_horizontal" android:dividerHeight="1sp" android:translationZ="5dp" android:background="#color/background_gray" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:clickable="true" /> </FrameLayout> </android.support.v4.widget.SwipeRefreshLayout>
Simply set the property [android:clickable="true"] for the Immediate clild layout of SwipeRefreshLayout which occupies complete screen area.i.e. whose width and height is match_parent. <?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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.support.v4.widget.SwipeRefreshLayout android:id="#+id/srlayout_Container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentBottom="true"> <RelativeLayout android:id="#+id/rl_MainContainer" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true"> <!-- Placeholder for Other View or ViewGroup as Clild --> </RelativeLayout> </android.support.v4.widget.SwipeRefreshLayout> </RelativeLayout>
setOnItemClickListener not working with DrawerLayout ListView
I am trying to set a left Drawer with ListView. On item click, I want to take some action but OnItemClick() doesnt get called. Heres my code. MainActivity.java protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); mNewsCategories = getResources().getStringArray(R.array.category_arrays); mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); mDrawerList = (ListView) findViewById(R.id.left_drawer); // Set the adapter for the list view mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mNewsCategories)); // Set the list's click listener mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { #Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Log.i("POSITION", "onItemClick:"+position); mDrawerLayout.closeDrawer(mDrawerList); } }); } activity_main.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"> <!-- The navigation 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="#111" /> <android.support.v4.view.ViewPager android:id="#+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.v4.widget.DrawerLayout> drawer_list_item.xml <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceListItemSmall" android:gravity="center_vertical" android:paddingLeft="16dp" android:paddingRight="16dp" android:textColor="#fff" android:focusable="false" android:background="?android:attr/activatedBackgroundIndicator" android:minHeight="?android:attr/listPreferredItemHeightSmall"/> P.S. I have looked into OnItemCLickListener not working in listview and nothing seems to be working. I tried every combination.
The problem is the z ordering. The issue is drawer list is not in front of view hierarchy. So need to bring it to front. To do that we can use following code snippet. mDrawerList.setOnScrollListener(new OnScrollListener() { #Override public void onScrollStateChanged(AbsListView view, int scrollState) { if (scrollState == SCROLL_STATE_IDLE) { mDrawerList.bringToFront(); mDrawerLayout.requestLayout(); } } #Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }); Use "setDrawerListener" of DrawerLayout and onDrawerOpened put: mDrawerList.bringToFront(); mDrawerLayout.requestLayout(); Following link will explain the problem more and how to fix it: http://vardhan-justlikethat.blogspot.com.es/2014/05/android-custom-navigation-drawer-not.html
Android Navigation Drawer Fail
I'm working on a chat-app and wanted to add a Navigation Drawer like in the GMail-App. I'm new to Java so pls don't kill me :D I tried to add it but it crashes :( This is some of the code of my Chat.java public class Chat extends FragmentActivity { final String[] data ={"Login","Register"}; final String[] fragments ={ "com.linkr.chat.Login", "com.linkr.chat.Register"}; String username; BufferedReader reader; PrintWriter writer; ArrayList<String> userList = new ArrayList(); public TextView chatTextArea; TextView inputTextArea; Button onClickButton; Boolean isConnected = false; #Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setTitle("Linkr"); /*<-- NAVDRAWER -->*/ setContentView(layout.activity_chat); ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data); final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); final ListView navList = (ListView) findViewById(R.id.drawer); navList.setAdapter(adapter); navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ #Override public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){ drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){ #Override public void onDrawerClosed(View drawerView){ super.onDrawerClosed(drawerView); FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); tx.replace(R.id.main, Fragment.instantiate(Chat.this, fragments[pos])); tx.commit(); } }); drawer.closeDrawer(navList); } }); FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); tx.replace(R.id.main,Fragment.instantiate(Chat.this, fragments[0])); tx.commit(); /*<-- NAVDRAWER END -->*/ I also don't get how to use the NavigationDrawer. This is my activity_chat.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp" android:layout_height="800dp" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Chat" android:text = "Linkr"> <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="300dp" android:layout_height="800dp" tools:context=".MainActivity" > <ListView android:id="#+id/drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#FFF" android:choiceMode="singleChoice"/> </android.support.v4.widget.DrawerLayout> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/inputTextArea" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="#+id/onClickButton"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:id="#+id/onClickButton" android:layout_alignBaseline="#+id/inputTextArea" android:layout_alignBottom="#+id/inputTextArea" android:layout_alignParentRight="true"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textMultiLine" android:ems="10" android:id="#+id/chatTextArea" android:layout_above="#+id/onClickButton" android:layout_alignLeft="#+id/inputTextArea" android:layout_alignParentTop="true" android:layout_alignRight="#+id/onClickButton"/> Do i need another XML for it? Can someone tell me how this works? Thank you guys! :)
The android.support.v4.widget.DrawerLayout is designed to be the top ViewGroup and can have two childs. The first one is your Content View (The View which is visible), like the normal Activity layout. The second one is your Drawer View (The View you can pull from the side). The Content View and the Drawer View can both be a ViewGroup like a RelativeLayout or a single view in your case the ListView. So your layout should look like this: <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="300dp" android:layout_height="800dp" tools:context=".MainActivity" > <!-- YOUR CONTENT VIEW--> <RelativeLayout xmlns:tools="http://schemas.android.com/tools" android:layout_width="300dp" android:layout_height="800dp" android:paddingLeft="#dimen/activity_horizontal_margin" android:paddingRight="#dimen/activity_horizontal_margin" android:paddingTop="#dimen/activity_vertical_margin" android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".Chat" android:text = "Linkr"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/inputTextArea" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_toLeftOf="#+id/onClickButton"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:id="#+id/onClickButton" android:layout_alignBaseline="#+id/inputTextArea" android:layout_alignBottom="#+id/inputTextArea" android:layout_alignParentRight="true"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textMultiLine" android:ems="10" android:id="#+id/chatTextArea" android:layout_above="#+id/onClickButton" android:layout_alignLeft="#+id/inputTextArea" android:layout_alignParentTop="true" android:layout_alignRight="#+id/onClickButton"/> </RelativeLayout> <--! YOUR DRAWER VIEW --> <ListView android:id="#+id/drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#FFF" android:choiceMode="singleChoice"/> </android.support.v4.widget.DrawerLayout>