Search code examples
androidandroid-listview

How do I set the alpha of all items in a listview?


As I am developing for API level 7 and upwards setAlpha(float) is unavailable to me. As such I have implemented a method that traverses all child elements of a given ViewGroup and tries to find a way to set the alpha so that the element is almost transparent. Unfortunately I cannot come up with a way to make the ListView and its items transparent. How do I proceed?

My method:

public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
    int childCount = viewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View view = viewGroup.getChildAt(i);

        view.setEnabled(enabled);
        if (!enabled) {
            if (view instanceof TextView) {
                int curColor = ((TextView) view).getCurrentTextColor();
                int myColor = Color.argb(ALPHA_NUM, Color.red(curColor),
                        Color.green(curColor),
                        Color.blue(curColor));

                ((TextView) view).setTextColor(myColor);
            } else if (view instanceof ImageView) {
                ((ImageView) view).setAlpha(ALPHA_NUM);
            } else if (view instanceof ListView) {
                // How do I set the text color of the subitems in this listview?
            } else {
                try {
                    Paint currentBackgroundPaint =
                            ((PaintDrawable) view.getBackground()).getPaint();
                    int curColor = currentBackgroundPaint.getColor();
                    int myColor = Color.argb(ALPHA_NUM, Color.red(curColor),
                            Color.green(curColor), Color.blue(curColor));
                    view.setBackgroundColor(myColor);
                } catch (NullPointerException e) {
                    Log.d("viewNotFound", "View " + view.getId() + " not found..");
                    e.getStackTrace();
                }
            }
            if (view instanceof ViewGroup) {
                enableDisableViewGroup((ViewGroup) view, enabled);
            }
        }
    }
}

Solution

  • Is it an option to set this directly in the XML for your ListView row?

    For example, if your layout is a LinearLayout

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">
    
        <!-- stuff -->
    </LinearLayout>
    

    I'm not sure of your setup, but you may be able to set it programatically like this if you really want your ListView to be transparent.

    ListView listView = (ListView) view;
    listView.setBackgroundColor(android.R.color.transparent);
    

    where default color android.R.color.transparent equals:

    <!-- Fully transparent, equivalent to 0x00000000 -->
    <color name="transparent">#00000000</color>
    

    In this case, you'll need to set your ListView rows to be transparent in the XML anyway.

    If you want to control the transparency of the Views inside your ListView rows, your best bet is to create a custom ArrayAdapter and handle this there.