8BitYoda 8BitYoda -4 years ago 213
Android Question

Android Navigation Drawer: How to change the click area while using a custom layout?

I have successfully created my Navigation Drawer and it displays properly. However my issue is that clicking the items don't work like Id like them to. My problem is that in-order to perform the click event you have to click in the red area where as I want the user to click in the blue area (the textview).

screen example <- (Edit* not enough rep to post images)

This is true for all items in my navigation drawer. Iv tried multiple things but it just wont act properly. Heres' some of my code snippets:

drawer_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:descendantFocusability="blocksDescendants">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/drawer_item"
android:text="New Text"
android:clickable="true"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold"
android:id="@+id/drawerListItem"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:phoneNumber="true"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:gravity="center_vertical|center_horizontal" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="false"
android:focusableInTouchMode="false"
android:touchscreenBlocksFocus="true">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/chain"
android:layout_alignBottom="@+id/imageView4"
android:layout_marginTop="-2dp"
android:layout_weight="1" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:src="@drawable/chain"
android:layout_marginTop="-2dp"
android:layout_weight="1" />
</LinearLayout>

</RelativeLayout>


MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setUpMapIfNeeded();

mList = new String[]{"item1", "item2", "Settings", "Logout"};
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, R.id.drawerListItem, mList));
// Set the list's click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* 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 */
) {

/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
assert getSupportActionBar() != null;
//getActionBar().setTitle(mTitle);
}

/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
//getActionBar().setTitle(mTitle);
}
};

// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true); // Do not iconify the widget; expand it by default

return true;
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns
// true, then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
int id = item.getItemId();
if (id == R.id.homelookup) {
startActivity(new Intent(getApplicationContext(),HomeLookup.class));
return true;
}

return super.onOptionsItemSelected(item);
}

/**
* Swaps fragments in the main content view
*/
private void selectItem(int position) {
// Highlight the selected item, update the title, and close the drawer
mDrawerList.setItemChecked(position, true);
if(position==0){
startActivity(new Intent(this, ForSale.class));
}else if(position==1){
startActivity(new Intent(this, PotentialListings.class));
}else if(position==2){
startActivity(new Intent(this, Invitations.class));
}else if (position == 3){
Toast.makeText(getApplicationContext(),"Settings Clicked",Toast.LENGTH_LONG).show();
}else if(position==4){
ParseUser.logOut();
Toast.makeText(getApplicationContext(),"Successfully logged out",Toast.LENGTH_LONG).show();
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
mDrawerLayout.closeDrawer(mDrawerList);


setTitle(mList[position]);

}

//possibly delete
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
//

private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
}

Answer Source

The problem is that the TextView has the android:clickable="true". And that is capturing the click before passing it to the List of the drawer.

Also you might want to check this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download