color / opacity in selected view

0

I've implemented a Navigation Drawer (without many modifications to the Android Developers example) I changed only the background colors of the ListView and the Text of the items:

layout / 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. A solid background is used for contrast
     with the content view. -->
<ListView
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:dividerHeight="0dp"
    android:background="#ffe1e1e1"/>

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

layout / drawer_list_item.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="?android:attr/activatedBackgroundIndicator">

<ImageView
    android:id="@+id/icon"
    android:layout_width="25dp"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:contentDescription="@string/list_item_icon"
    android:src="@drawable/ic_logo_icone"
    android:layout_centerVertical="true"/>


<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:paddingLeft="45dp"
    android:paddingRight="16dp"
    android:textColor="#ff555555"
    android:textStyle="bold"
    android:hint="@string/item_desc"
    android:background="?android:attr/activatedBackgroundIndicator"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />

</RelativeLayout>

And here's my Navigation Drawer:

The problem, as you can see, is that when the item is selected, the blue color overlaps the Icon, making it more erased, and the color of the text is also overlaid with a slight shade change.

I would like my icon and my text to have no color change, which would turn blue only in the white areas of the view. I already tried many settings in the XMLs and tried to do other things programatically as well, but nothing solved.

    
asked by anonymous 22.07.2014 / 17:09

1 answer

1

I was able to solve the problem. Here's the solution:

In the Android Developers sample app, the following functions are used to select an item:

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    setTitle(mTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

It was suggested that I manually make the selection, but even I commenting on the methods mDrawerList.setItemChecked(position, true); and mDrawerList.setSelection(position); was still selected due to the configuration: android:choiceMode="singleChoice" that could not be removed as this has impacted on other problems in the program.

The solution was found when reloading the adapter using the mDrawerList.setAdapter(adapter); function and on my adapter I could manually make the selection.

Then my code looks like this:

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    adapter.setSelectedItem(position);
    for(int i = 0; i < DrawerItems.size(); i++){
        DrawerItems.set(i, new DrawerItem(mTitles[i], getResources().obtainTypedArray(R.array.nav_drawer_icons_light).getResourceId(i, -1)));
    }
    DrawerItems.set(position, new DrawerItem(mTitles[position], getResources().obtainTypedArray(R.array.nav_drawer_icons).getResourceId(position, -1)));

    mDrawerList.setAdapter(adapter);
    setTitle(mTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

And my adapter like this:

public class DrawerAdapter extends BaseAdapter {

 (...)
 private int mSelectedItem = -1;

 (...)

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.drawer_list_item, null);
    }

    RelativeLayout container = (RelativeLayout) convertView.findViewById(R.id.drawer_item_container);
    ImageView imgIcon = (ImageView) convertView.findViewById(R.id.icon);
    TextView txtTitle = (TextView) convertView.findViewById(R.id.text1);

    if(position == mSelectedItem){
        txtTitle.setTextColor(context.getResources().getColor(R.color.white));
        container.setBackgroundColor(context.getResources().getColor(R.color.blue));
    } else {   
       txtTitle.setTextColor(context.getResources().getColor(R.color.drawer_text_unselected));             
       container.setBackgroundColor(context.getResources().getColor(R.color.drawer_background));
    }

    imgIcon.setImageResource(DrawerItems.get(position).getIcon());
    txtTitle.setText(DrawerItems.get(position).getTitle());

    return convertView;
  }

  public void setSelectedItem(int position){
      mSelectedItem = position;
  }
}
    
22.07.2014 / 21:15