Anthony
Anthony

Reputation: 149

Views in Custom ListView item don't have apptheme

Problem:

I have a custom adapter for my listview which currently contains a checkbox and an edittext. I haven't changed any themes in the res/values/styles folder. For some reason the checkbox and edittext have a different style/theme than the normal style/theme ("AppTheme").

Automatically generated theme:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

But the checkbox is white, and can only be seen when it is checked. (color is not the colorAccent set above) And the edittext has the same color the checkbox has when it is checked (green/blueish).

colors:

<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>

What have I tried?

I tried setting the theme/style of the container of the item and the item itself, with the xml attributes android:theme and style. Each to no avail.

Is this normal? If so, how do I change the style/theme of the listitem? If not, what could be wrong?

My listitem.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent" >

<CheckBox
    android:id="@+id/cb_listitem"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"/>

<EditText
    android:id="@+id/editText_listitem"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="9"
    android:textColor="@color/black"/>

</LinearLayout>

getView in custom adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;

    if (convertView == null){
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.listitem_note_checkbox, null);
        holder = new ViewHolder();
        holder.checkBox = (CheckBox)convertView.findViewById(R.id.cb_listitem);
        holder.editText = (EditText)convertView.findViewById(R.id.editText_listitem);
        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.editText.setText(items.get(position).note);
    return convertView;
}

screenshots:

screenshot checkbox not checked screenshot checkbox checked

as we can see the radio buttons have the default layout, accentcolor "pink" and they have a grey outlining. but the checkbox outlining is white, and the accentcolor is blue/greenish.

Upvotes: 1

Views: 723

Answers (1)

Mike M.
Mike M.

Reputation: 39201

You need to use the Activity's Context for anything that requires styles and attribute values from its theme. Since your Adapter's context field is initialized in its constructor, you need to instantiate your Adapter with the Activity's Context, not the Application's, so the LayoutInflater ends up with the correct one. For example:

MyAdapter adapter = new MyAdapter(MainActivity.this, list);

Upvotes: 6

Related Questions