color/opacity in selected view

Asked

Viewed 701 times

0

I implemented a Navigation Drawer (no modifications to the Android Developers example) I only changed the background colors of Listview and 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 comes my Navigation Drawer:

Navigation Drawer

The problem, as you can see, is that when the item is selected, the blue color superimposes the Icone leaving it more erased, and the text color is also superimposed having a slight shade change.

I would like my icone and my text to have no color change, to be blue only in the white areas of the view. I’ve tried my Xmls settings and tried doing other things programmatically too, but nothing solved.

  • I believe your icons are with alpha, so the blue color overlap, try to put a color in the background of ImageView, possibly equal to the background of the item, to see if it contrasts.

  • no use adding color to background of ImageView, continues with the same problem.

  • Try to make a separate selector and put it in the background of your list_item Relative instead of ? android:attr/activatedBackgroundIndicator. .

  • in my RelativeLayout of drawer_list_item I already put the attribute android:background="?android:attr/activatedBackgroundIndicator", but I don’t make a separate selector. I use mDrawerList.setSelection(position); to make selector.

  • just tried to make separate selector with the function: mRelativeLayout.setBackground(getResources().getColor(R.color.blue)); but it didn’t work. The text I was able to solve, but the icon, it won’t work at all! :(

1 answer

1


I managed to solve the problem. Here is 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);

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

The solution was contracted by recharging the Adapter using the function mDrawerList.setAdapter(adapter); and on my Adapter I was able to manually do Selection.

Then my code went 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;
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.