0

I have a 400 x 206 image that I'm trying to show below my menu in my navigation drawer, but it isn't showing when I open the navigation drawer. Oddly enough in the AS design preview it looks like the image is getting converted into a line. I also now find my open drawer has an extra strip of transparent space on the far right roughly half an inch wide. I'm thinking the two are related, but my current priority is figuring out how to get the image to show below the menu items.

Android Studio design preview of activity_main.xml. The top blue outline that goes all the way to the right is left_drawer, the object that's in it that doesn't go quite as to the right is left_drawer_listview, and the line below is drawer_buttom_image:

enter image description here

And for the record I've looked at Background image and image view in Navigation Drawer which is much more complex, so I might have made a mistake in simplifying the solution for my needs.

activity_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">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         The drawer is given a fixed width in dp and extends the full height of
         the container.  -->
    <LinearLayout
        android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:orientation="vertical">

        <ListView
            android:id="@+id/left_drawer_listview"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:background="@color/secondary_blue_transparent"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />


        <ImageView
            android:id="@+id/drawer_bottom_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/testimage"
            android:contentDescription="@string/testdescr"></ImageView>

    </LinearLayout>
</android.support.v4.widget.DrawerLayout>

drawer_list_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp">

    <ImageView
        android:id="@+id/drawer_icon"
        android:layout_width="40dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:background="@drawable/selector"
        android:contentDescription="@string/app_name"
        android:paddingLeft="10dp"
        android:paddingRight="5dp" />

    <TextView
        android:id="@+id/drawer_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/drawer_icon"
        android:background="@drawable/selector"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingLeft="10dp"
        android:paddingRight="5dp"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="#fff" />

</RelativeLayout>

MainActivity code:

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

        dataList = new ArrayList<DrawerItem>();
        mTitle = mDrawerTitle = getTitle();
        mDrawerTitles = getResources().getStringArray(R.array.drawer_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLinear = (LinearLayout) findViewById(R.id.left_drawer);
        mDrawerListView = (ListView) findViewById(R.id.left_drawer_listview);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        for (int i = 0; i < mDrawerTitles.length; ++i) {
            dataList.add(new DrawerItem(mDrawerTitles[i], mDrawerIcons[i]));
        }

        // set up the drawer's list view with items and click listener
        drawerAdapter = new CustomDrawerAdapter(this, R.layout.drawer_list_item,
                dataList);
        mDrawerListView.setAdapter(drawerAdapter);
/*        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mDrawerTitles));*/
        mDrawerListView.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(1);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, do x
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear);

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }
        // Handle action buttons
        switch (item.getItemId()) {
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    private void selectItem(int position) {
        // update the main content by replacing fragments
        //Fragment fragment = new PlanetFragment();
        Fragment fragment = new EventFragment();
        Bundle args = new Bundle();

/*        // Pass in variables
        args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
        fragment.setArguments(args);*/

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerListView.setItemChecked(position, true);
        //setTitle(mDrawerTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerLinear);
    }
Community
  • 1
  • 1
Kurt Wagner
  • 3,295
  • 13
  • 44
  • 71

1 Answers1

2

This is because the ListView is already using all the available height. Change the ListView height to 0dp and add a weight of 1. This results in it using all the available height EXCEPT what is already used.

    <ListView
        android:id="@+id/left_drawer_listview"
        android:layout_width="240dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/secondary_blue_transparent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
SimonSays
  • 10,867
  • 7
  • 44
  • 59
  • Thanks! Do you have any suggestions of how to modify the ImageView to get right of that extra transparent slip of the drawer on the far right? – Kurt Wagner Sep 05 '14 at 20:27