Android: my custom spinner-item works, but not correct on Lollipop - why?

183 views Asked by At

I have a problem.

I made a custom spinner-item, which shows a text and a image.

this works on my devices: Android 2.3.7 and 4.0.4 - it shows the text and the image.

but doesn't work on my device: Android 5.0.2 - it shows only the text, without the image.

i don't get the reason why.. somebody an idea?

--

my Java-Code:

 SimpleCursorAdapter adapter = new SimpleCursorAdapter(
            getActivity(),
            R.layout.spinner_item,
            MainActivity.db.getList(),
            new String[]{DatabaseHandler.ENTRY_LANGUAGE.NAME, DatabaseHandler.ENTRY_LANGUAGE.TTS},
            new int[]{R.id.text, R.id.sound},
            0);

    adapter.setDropDownViewResource(R.layout.spinner_item);
    adapter.setViewBinder(new MyViewBinder());
    final Spinner spinner = (Spinner) view.findViewById(R.id.mlang);
    spinner.setAdapter(adapter);

my spinner_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<com.example.app.common.CheckedRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/spinner_bg">


    <CheckedTextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:paddingLeft="13dp"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@drawable/spinner_selected_text"
        android:textSize="18sp" />

        <ImageView
            android:id="@+id/sound"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:paddingRight="8dp"
            android:src="@drawable/speaker" />

</com.example.app.common.CheckedRelativeLayout>

my CheckedRelativeLayout:

package com.example.app.common;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Checkable;
import android.widget.RelativeLayout;

public class CheckedRelativeLayout extends RelativeLayout implements Checkable {

    private static final int[] CHECKED_STATE_SET = {
        android.R.attr.state_checked
    };

    private boolean checked = false;

    @SuppressLint("NewApi")
    public CheckedRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CheckedRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckedRelativeLayout(Context context) {
        super(context);
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void setChecked(boolean checked) {
        this.checked = checked;

        refreshDrawableState();

        //Propagate to childs
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if(child instanceof Checkable) {
                ((Checkable)child).setChecked(checked);
            }
        }
    }

    @Override
    protected int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

    @Override
    public void toggle() {
        this.checked = !this.checked;
    }

}
1

There are 1 answers

0
user3875103 On

i have solved it: (it was a consequential error)

after my update 4.4.4 to 5.0.2 the Text-To-Speak was delete :( ..after manually installing the tts, the code work too. :)