0

I've also tried some other stackoverflow and google-solutions but they didn't work (How to properly design/style a Android Navigation Drawer, Navigation Drawer – Part 1)

I want to style the NavigationDrawer but I don't understand, which code I should change. I want to set following style (background/foreground):

  • Standard
  • Active-Item
  • Pressed/Focused-Item

and I also want to add an Icon before the List-Items!

Here is the activity where the content is loaded in and drawer is shown:

activity_main_window.xml:

<!-- 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
    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="bernd.slider">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="bernd.slider.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>

And here the fragment_navigation_drawer.xml:(Where the ListView is stored in)

<ListView 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:choiceMode="singleChoice"
   android:divider="@color/darkred"
    android:dividerHeight="1dp"
    android:background="@color/white"
    tools:context="bernd.slider.NavigationDrawerFragment"
    android:id="@+id/navigation_drawer" />

I should mention this code:

from main_window.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_area);
    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();
    onSectionAttached(1); // Set the title to Homepage
    // Set up the drawer.
    mNavigationDrawerFragment.setUp(
            R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));
}

from NavigationDrawerFragment.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    mDrawerListView = (ListView) inflater.inflate(
            R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    });
    mDrawerListView.setAdapter(new ArrayAdapter<String>(
            getActionBar().getThemedContext(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            new String[]{
                    getString(R.string.title_section1),
                    getString(R.string.title_section2),
                    getString(R.string.title_section3),
            }));
    mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
    return mDrawerListView;
}

Important is that I use an FragmentManager to load the content from a specific page into R.id.container (activity_main_window.xml).

So how can I change the style? I don't understand where...

Community
  • 1
  • 1
Bernd
  • 730
  • 1
  • 5
  • 13

1 Answers1

1

You can change the style in your ListViewAdapter. I would consider making a custom adapter and extending it from BaseAdapter. You then can go:

mDrawerListView.setAdapter(new myCustomAdapter);

Or, what I did once, don't use a listview for your drawerfragment:

mDrawerListView = (ListView) inflater.inflate(
        R.layout.your_custom_fragment, container, false);

In "R.layout.your_custom_fragment" you can put whatever you want to. If the items in the ListView aren't going to be dynamic, just put a couple TextViews in a vertical LinearLayout.

Hope this helped.

Wim Berchmans
  • 271
  • 2
  • 12