Navigation Drawer's click event not working - android

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>

Related

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>

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>

Android DrawerLayout not responding to button click event

I have a DrawerLayout component in my activity, but I'm not using ActionBar.
This is my main_activity.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/mainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="#+id/drawer"
android:layout_width="650dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:divider="#null"
android:choiceMode="singleChoice"
android:visibility="gone"/>
</android.support.v4.widget.DrawerLayout>
<ImageButton
android:id="#+id/categories_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="20dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:background="#drawable/button_category"/>
In my MainActivity I try to open/close the DrawerLayout by clicking on my Button.
Here is my Activity:
public class MainActivity extends Activity {
private ImageButton categoriesButton;
private DrawerLayout drawerLayout;
private ListView drawerMenu;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
categoriesButton = (ImageButton)findViewById(R.id.categories_button);
categoriesButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(drawerLayout.isDrawerOpen(drawerMenu)) {
drawerLayout.closeDrawer(drawerMenu);
}else {
drawerLayout.openDrawer(drawerMenu);
}
}
});
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
drawerMenu = (ListView) findViewById(R.id.drawer);
MyAdapter myAdapter = new MyAdapter(context, getCategories());
drawerMenu.setAdapter(myAdapter);
}
}
When I click the Button the first time it does not respond, but after I open the DrawerLayout manually it works perfect.
Does anybody know what is wrong here?
Thanks!
I found the solution! In the xml, the Listview visibility needs to be set to visible. It was set to gone (I took this code from the oficial documentation).
<ListView
android:id="#+id/drawer"
android:layout_width="650dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
android:divider="#null"
android:choiceMode="singleChoice"
android:visibility="visible"/>
Anyway now it works. Thanks for your help!

DrawerLayout prevents touch events on EditText

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.

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>

Categories

Resources