DrawerLayout prevents touch events on EditText - android

I have a DrawerLayout, which has an EditText and a ListView in it.
The problem is, that I cannot touch my EditText or ListView items. Instead of it
DrawerLayout recieves all the touch events. Clicking on any item in the DrawerLayout causes DrawerLayout to close (which I guess is the default onTouch method in the drawerlayout class)
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background" >
<-- my main layouts here -->
</FrameLayout>
<LinearLayout
android:id="#+id/left_linear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:orientation="horizontal" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/noborder"
android:clickable="true"
android:hint="search box 1"
android:padding="8dp"
android:singleLine="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="#android:drawable/ic_menu_search" />
</LinearLayout>
<ListView
android:id="#+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Try drawerlayout.requestDisallowInterceptTouchEvent(true) in your oncreate

Set Both edit text and text view explicitly as clickable .. then implement something like this for list view
/******************************Listener for Drawer***************/
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
//your code here based on posotion
}
}
you can set the listener using something like this
DrawerList.setOnItemClickListener(new DrawerItemClickListener());

i found a similar problem i have update sdk and specially Extra Folder and it work fine.
I've test your code and it's working fine on my device.
Thanks i hope it help.

i have done something like this,
i have updated your code.
edittext can now take focus
create main.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:background="#drawable/background"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<-- my main layouts here -->
</FrameLayout>
<FrameLayout
android:id="#+id/navigation_frame"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
create navigation.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/left_linear"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:layout_gravity="start">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:orientation="horizontal">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="true"
android:background="#drawable/noborder"
android:hint="search box 1"
android:padding="8dp"
android:singleLine="true" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:padding="5dp"
android:src="#android:drawable/ic_menu_search" />
</LinearLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</LinearLayout>
//create activity like
public class HomeActivity extends actionbaractivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// add menu to navigation drawer
addMenu();
}
private void addMenu()
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.navigation_frame,
new NavigationFragment());
fragmentTransaction.commit();
}
}
// create new fragment class that add menu to drawer layout
public class NavigationFragment extends Fragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.navigation_view, null);
return view;
}
}
// its working for me..

Try this one.
package com.example.drawersample;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawerlist, drawerListViewItems));
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_launcher, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
// 2.3 enable and show "up" arrow
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
// true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this, ((TextView) view).getText(),
Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}
}
drawerlist.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#fff"
android:textSize="20sp"
android:gravity="center_vertical"
android:paddingStart="14.5sp"
android:paddingEnd="14.5sp"
android:minHeight="35sp"
/>
main.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">
<!-- 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:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:background="#333"
android:paddingLeft="15sp"
android:paddingRight="15sp"
/>
</android.support.v4.widget.DrawerLayout>
strings.xml
<resources>
<string name="app_name">DrawerSample</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="items">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
<item>Item 5</item>
<item>Item 6</item>
</string-array>
<string name="drawer_open">Open navigation drawer</string>
<string name="drawer_close">Close navigation drawer</string>
</resources>
Try to add your Edittext and textview of the layout in below lines
android:focusableInTouchMode="true"
android:focusable="true"
or
oncreate methos you use requestfocus(your element id);
thank you.

Related

AndroidStudio: Why does LayoutInflater switch back to old activity?

I've been looking for similar questions here on Stack Overflow, but with no luck. I have my first activity, MapsActivity. I want my second activity, AboutActivity, to appear in the FrameLayout of the MapsActivity layout, so I used LayoutInflater. When I run the app, I see my AboutActivity for about half a second, then it quickly changes back to the MapActivity. I don't have any errors on logcat and both activities are listed in the Manifest file.
Here is my MapsActivity (with just the relevant parts). My Google Map is inside the fragment (where I hope my second activity will appear). I have a menu button, and one of the buttons leads to the second activity.:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private String[] menuOptions;
private DrawerLayout myDrawerLayout;
private ListView myList;
// private Fragment menuFragment;
private MapFragment mMapFragment;
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (findViewById(R.id.content_frame)!=null){
if (savedInstanceState!=null){
return;
}
// to add fragment dynamically, create new instance- add to frame layout
mMapFragment= MapFragment.newInstance();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.content_frame, mMapFragment);
fragmentTransaction.commit();
//make menu from array indices
menuOptions = getResources().getStringArray(R.array.menu_array);
myDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
myList= (ListView)findViewById(R.id.left_drawer);
//adapter for list view
myList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, menuOptions));
//list's click listener
myList.setOnItemClickListener(new DrawerItemClickListener());
Button menuButton = (Button) findViewById(R.id.menuButton);
menuButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
myDrawerLayout.openDrawer(Gravity.LEFT);
}
});
...
private void selectItem(int position){
switch(position) {
case 0:
//Highlight selected item
myList.setItemChecked(position, true);
myDrawerLayout.closeDrawer(myList);
break;
case 1:
//about
Intent switchIntent = new Intent(getApplicationContext(), AboutActivity.class);
startActivity(switchIntent);
break;
Here is my AboutActivity, the second activity. The activity_about only appears for one second. I want it to appear where the map was.
public class AboutActivity extends MapsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_about);
FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame);
getLayoutInflater().inflate(R.layout.activity_about, frame);
}
Here is my activity_maps.xml, it features a map and a FrameLayout where I want my other activities to appear (this way I can also have the ListView menu in each activity):
<?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 main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/menuButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Menu" />
</FrameLayout>
<!-- 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">
</ListView>
</android.support.v4.widget.DrawerLayout>
If you want your Button and ListView in both activities, why not use AboutFragment instead of AboutActivity. And when you click the aboutButton in ListView, just replace the fragment shows in the FrameLayout.You have to let you menuButton outside the FrameLayout.
activity_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 main content view -->
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp" />
<Button
android:id="#+id/menuButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Menu" />
</LinearLayout>
<!-- 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">
</ListView>
</android.support.v4.widget.DrawerLayout>
public class AboutActivity extends FragmentActivity, and try to use fragments.
In "main" layout create one:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/menuButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Menu" />
</FrameLayout>
in mainActivity inflate your fragment created into frameLayout.

Android- app crashes while adding a navigation drawer to a custom toolbar

I've made a custom toolbar and its Right-To-Left. and it looks like this:
I want a navigation drawer appears when I click on the right side icon. But when I add the navigation drawer to app, it crashes. I've tried many things and also searched a lot, but it didn't work.
It'd be GREAT it someone would help me.
Here is my code:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.playpersia.mycustomizedtoolbar.MainActivity"
>
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|end" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
right_drawer.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.playpersia.mycustomizedtoolbar;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
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.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.zip.Inflater;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
public ListView listView;
public String[] pages;
ArrayAdapter<String> arrayAdapter;
public ImageView menu_icon;
public ImageView back_arrow;
public boolean mSlideState;
private DrawerLayout drawerLayout;
public ActionBarDrawerToggle _mDrawerTg;
#TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
menu_icon = (ImageView) findViewById(R.id.menu_icon);
back_arrow = (ImageView) findViewById(R.id.back_button);
drawerLayout =(DrawerLayout) findViewById(R.id.right_drawer);
pages =getResources().getStringArray(R.array.pages);
_mDrawerTg =new ActionBarDrawerToggle(this, drawerLayout,
null, R.string.drawer_open, R.string.drawer_close){
public void onDrawerClosed(View view){
mSlideState=false;
}
public void onDrawerOpened (View drawerView){
super.onDrawerOpened(drawerView);
mSlideState=true;
} };
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(this,R.layout.drawer_list_item,pages));
drawerLayout.setDrawerListener(_mDrawerTg);
_mDrawerTg.syncState();
menu_icon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mSlideState){
drawerLayout.closeDrawer(GravityCompat.END);
}else{
drawerLayout.openDrawer(GravityCompat.END); } } }); }
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
_mDrawerTg.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
_mDrawerTg.onConfigurationChanged(newConfig);
}
#Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (_mDrawerTg.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
}
UPDATE
logcat:
01-18 12:34:25.386 29949-29949/com.playpersia.mycustomizedtoolbar
E/AndroidRuntime: FATAL EXCEPTION: main Process:
com.playpersia.mycustomizedtoolbar, PID: 29949 java.lang.RuntimeException:
Unable to start activityComponentInfo{com.playpersia.mycustomizedtoolbar/com.playpersia.mycustomizedtool
bar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)at
android.app.ActivityThread.access$800(ActivityThread.java:156)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)at
android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference at com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
at android.app.ActivityThread.access$800(ActivityThread.java:156) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:211) 
at android.app.ActivityThread.main(ActivityThread.java:5373) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815) 
UPDATE 2
now I have this toolbar when I click on the right side icon the drawer opens & closes,
right side drawer
Caused by: java.lang.NullPointerException: Attempt to invoke virtual
method 'void
android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a
null object reference at
com.playpersia.mycustomizedtoolbar.MainActivity.onCreate(MainActivity.java:53)
In onCreate method, your listView object is null.
You need to add
listView = (ListView) findViewById(R.id.list_view);
before
listView.setAdapter(new ArrayAdapter<String (this,R.layout.drawer_list_item,pages));
UPDATE:
Change your activity_main.xml to:
<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">
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/toolbar"/>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="end"
android:fitsSystemWindows="true"
android:id="#+id/right_drawer">
<!-- Main layout -->
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/toolbar"
android:text="Hello"/>
</FrameLayout>
<!-- Nav drawer -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="left|start" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/header_drawer"
android:src="#drawable/my_image" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:id="#+id/list_view"
android:entries="#array/pages" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
This should be your MainLayout since you are using DrawerLayout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/right_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- Your contents, for example, CoordinatorLayout with material design -->
<!-- And after main Contents, here you can use NavigationDrawer also -->
</android.support.v4.widget.DrawerLayout>
And instead of commented code, use your contents.
You did it in wrong way, Take a look:
http://developer.android.com/training/implementing-navigation/nav-drawer.html#DrawerLayout
To add a navigation drawer, declare your user interface with a
DrawerLayout object as the root view of your layout. Inside the
DrawerLayout, add one view that contains the main content for the
screen (your primary layout when the drawer is hidden) and another
view that contains the contents of the navigation drawer.
For example, the following layout uses a DrawerLayout with two child
views: a FrameLayout to contain the main content (populated by a
Fragment at runtime), and a ListView for the navigation drawer.
Example:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- 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.widget.DrawerLayout>
You may want to take a look at the Material Design Guidelines for this implementation of course:
https://www.google.com/design/spec/patterns/navigation-drawer.html

Listview on click event not working

i am trying to add on click listener in a listview but when clicked, no action. can you help me find what i did wrong?
This is adapted from one of the example in android tutorial
public class Main extends Activity {
private ListView menuListView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Inflate the menu items for use in the action bar
//getMenuInflater().inflate(R.menu.menu_main,menu);
//Get the Menu List from resources
menu = getResources().getStringArray(R.array.menu);
//get the drawer Layout
menuDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ArrayAdapter<String> itemAdapter = new ArrayAdapter<String>(this,R.layout.custom_menu_list,menu);
//get the list view
menuListView = (ListView) findViewById(R.id.listview);
menuListView.setAdapter(itemAdapter);
menuListView.setOnItemClickListener(new menuItemClickListener());
}
private class menuItemClickListener implements ListView.OnItemClickListener{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.d("Check","clicked");
}
}
}//
}
This is the xml of the listview textview
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
android:focusable="false"
android:focusableInTouchMode="false"
/>
This is the main 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"
android:background="#FFFFFF">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"/>
<!-- The navigation drawer -->
<ListView android:id="#+id/listview"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toastMsg"
android:text="Hello"/>
</android.support.v4.widget.DrawerLayout>
Modify you're main.xml file, remove the TextView and run the code. It works
<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"
android:background="#FFFFFF">
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"/>
<!-- The navigation drawer -->
<ListView android:id="#+id/listview"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"/>
</android.support.v4.widget.DrawerLayout>

Navigation Drawer's click event not working

This is my layout . I have a Navigation Drawer and a button in the activity buttons Click event is working but the Click event for Drawer is not working.
<android.support.v4.widget.Drawer Layout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<Frame Layout
android:id="#+id/content_frame"
android:layout_width="wrap_content"
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="#666"
android:dividerHeight="1dp"
android:background="#333"
android:paddingLeft="15sp"
android:paddingRight="15sp"
/>
<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:background="#e5e5e5"
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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
The code i have implemented:
public class MainActivity extends Activity {
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// 2. App Icon
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// 2.1 create ActionBarDrawerToggle
actionBarDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
// 2.2 Set actionBarDrawerToggle as the DrawerListener
drawerLayout.setDrawerListener(actionBarDrawerToggle);
// 2.3 enable and show "up" arrow
getActionBar().setDisplayHomeAsUpEnabled(true);
// just styling option
// drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actionBarDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, ((TextView)view).getText(), Toast.LENGTH_LONG).show();
drawerLayout.closeDrawer(drawerListView);
}
}
}
In XML layout You have provided following attribute to Button tag
android:onClick="actionbtn"
This means method actionbtn will be invoked when user will click Button.
Create method called actionbtn under MainActivity like below with parameter as object of View class.
public void actionbtn(View view){
<< Put your code for drawer action here
drawerLayout.openDrawer(drawerListView)>>;
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
Please note that I am not sure about code to put in method, but creating above method in MainActivity class will solve your issue.
Update -
Please try using following XML layout
<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"
tools:context=".MainActivity" >
<!-- The main content view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#e5e5e5"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#333"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp" />
</android.support.v4.widget.DrawerLayout>
public void onDrawerOpened(View drawerView) {
drawerListView.bringToFront();}
Your java code is perfect. Problem here is with your xml file.
You need to remove this part from android.support.v4.widget.DrawerLayout of the xml code and try then it'll run like a champ.
<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:background="#e5e5e5"
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=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
Remember you cant include the layout items with navigation drawer items.
Try This
Xml:-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!--
As the main content view, the view below consumes the entire
space available using match_parent in both dimensions.
-->
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/relative_week"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:onClick="actionbtn"
android:text="Button" />
</RelativeLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/left_drawer"
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="0.5dp" />
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>

The ListView Items in my navigation drawer are there but I can't see the list

The list is there and it goes to whatever activity I send it to but the list the names on the list don't show. I've changed the color of the font and the drawer background and it doesn't show. The item gets highlighted alright and it redirects to the proper activity as it should but there's nothing written in the drawer.
activity_main:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="#+id/menu_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.widget.DrawerLayout>
drawer_list_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android: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:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>
MainActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listMenu = getResources().getStringArray(R.array.menuArray);
dLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
lv1 = (ListView) findViewById(R.id.menu_drawer);
lv1.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, listMenu));
I had set my array like this:
<item name="section" value="Section"/>
when I should have done this:
<item name="section">Section</>

Categories

Resources