Anthony Anthony - 5 months ago 14
Android Question

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.

Answer

When you get a LayoutInflater from the system service, it doesn't use or have access to your Activity's theme, so the Views it inflates have just their basic styles. Instead, get your LayoutInflater from the Activity's Context, using the static LayoutInflater.from() method.

LayoutInflater inflater = LayoutInflater.from(context);

Also, assuming that your context field is initialized in the Adapter's constructor, be sure to instantiate your Adapter with the Activity's Context, not the Application's, so the Inflater ends up with the correct one. For example:

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