Search code examples
androidlistadapterlayer-list

Changing layer-list shape solid color from list adapter


I have this cardview I made with a layer-list background. This cardview populates a listview. The cardview holds EditTexts, ImageViews, TextViews dans CheckBoxes. There's also and expand and collapse button to see more of the card. What I want to do is when the user click the button to collapse the view, if a certain checkbox is checked I want to set the background color of the card to another color

here's my layer.car_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
     <solid android:color="@android:color/darker_gray"/>
     <corners android:radius="8dp" />
    </shape>
</item>

<item
  android:id="@+id/item_list_background"
  android:left="0dp"
  android:right="0dp"
  android:top="0dp"
  android:bottom="2dp">
  <shape android:shape="rectangle">
     <solid android:color="@android:color/white"/>
     <corners android:radius="8dp" />
  </shape>
</item>
    </layer-list>  

here is my List Adapter

public class InspectionListAdapter extends ArrayAdapter<String>{

private final Activity context;
private final String[] inspectionTitles;
private final String[] description;


public InspectionListAdapter(Activity context, String[] inspectionTitles,
        String[] description) {
    super(context, R.layout.inspection_item_layout, inspectionTitles);
    this.context = context;
    this.inspectionTitles = inspectionTitles;
    this.description = description;
}

static class ViewContainer {
    public ImageView imageView, expandButton;
    public TextView txtTitle;
    public TextView txtDescription;
    public RelativeLayout expandLayout;
    public EditText ownDesc, estCost;
    public LayerDrawable listItem;
    public GradientDrawable color;
    public CheckBox checkRepair, checkDone;
}

@Override
public View getView(int position, View view, ViewGroup parent) {

    final ViewContainer viewContainer;
    View rowView = view;

    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.inspection_item_layout, null,
                true);

        viewContainer = new ViewContainer();

        viewContainer.txtTitle = (TextView) rowView
                .findViewById(R.id.textViewItemTitle);

        viewContainer.txtDescription = (TextView) rowView
                .findViewById(R.id.textViewTip);

        viewContainer.listItem = (LayerDrawable)rowView.getResources().getDrawable(R.drawable.layer_car_background);

        viewContainer.color = (GradientDrawable)(viewContainer.listItem.findDrawableByLayerId(R.id.item_list_background));



        viewContainer.imageView = (ImageView) rowView
                .findViewById(R.id.imageViewItem);

        viewContainer.expandButton = (ImageView) rowView
                .findViewById(R.id.imageViewTitle);

        viewContainer.ownDesc = (EditText)rowView.findViewById(R.id.editTextDescription);

        viewContainer.estCost = (EditText)rowView.findViewById(R.id.editTextEstCost);

        viewContainer.checkRepair = (CheckBox)rowView.findViewById(R.id.checkBox2);

        viewContainer.checkDone = (CheckBox)rowView.findViewById(R.id.checkBox1);

        viewContainer.checkRepair.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean arg1) {
                // TODO Auto-generated method stub

                if(view.isChecked()){
                    viewContainer.checkDone.setChecked(false);
                }
            }
        });
        viewContainer.checkDone.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton view, boolean arg1) {
                // TODO Auto-generated method stub

                if(view.isChecked()){
                    viewContainer.checkRepair.setChecked(false);


                }
            }
        });

        viewContainer.expandLayout = (RelativeLayout) rowView
                .findViewById(R.id.relativeLayoutExpandable);

        viewContainer.expandLayout.setVisibility(View.GONE);

        rowView.setTag(viewContainer);

        viewContainer.expandButton
                .setOnClickListener(new OnClickListener() {

                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        if (viewContainer.expandLayout.getVisibility() == View.VISIBLE) {
                            viewContainer.expandButton
                                    .setImageResource(R.drawable.ic_action_expand);
                            viewContainer.expandLayout
                                    .setVisibility(View.GONE);
                            if(viewContainer.checkDone.isChecked()){

                                viewContainer.color.setColor(Color.GREEN);
                            }


                        } else {
                            viewContainer.expandButton
                                    .setImageResource(R.drawable.ic_action_collapse);
                            viewContainer.expandLayout
                                    .setVisibility(View.VISIBLE);

                        }
                    }
                });
    } else {
        // ---view was previously created; can recycle---
        Log.d("InspectionListAdapter", "Recycling");
        // ---retrieve the previously assigned tag to get
        // a reference to all the views; bypass the findViewByID() process,
        // which is computationally expensive---
        viewContainer = (ViewContainer) rowView.getTag();
    }

    // ---customize the content of each row based on position---

    viewContainer.txtTitle.setText(inspectionTitles[position]);
    viewContainer.txtDescription.setText("Tip: \n" + description[position]);
    viewContainer.imageView.setImageResource(R.drawable.ic_launcher);
    viewContainer.expandButton
            .setImageResource(R.drawable.ic_action_expand);



    return rowView;
}

}

If you take a look at my onClick method, the call viewContainer.color.setColor does not work there.

Please help me out !

UPDATE: the background drawable is set in my item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/layer_car_background" >

....

Solution

  • I found my way, I've created another xml ressources name layer_car_background_green.xml

    I've referenced my relativelayout

    public RelativeLayout mainLayout;
    
    ....
    
    viewContainer.mainLayout = (RelativeLayout)rowView.findViewById(R.id.item_layout_main);
    

    and then change it's background resource in my onClick

    public void onClick(View v) {
                            // TODO Auto-generated method stub
    
                            if (viewContainer.expandLayout.getVisibility() == View.VISIBLE) {
                                viewContainer.expandButton
                                        .setImageResource(R.drawable.ic_action_expand);
                                viewContainer.expandLayout
                                        .setVisibility(View.GONE);
                                if (viewContainer.checkDone.isChecked()) {
    
                                    viewContainer.mainLayout.setBackgroundResource(R.drawable.layer_car_background_green);
    
                                }
    
                            } else {
                                viewContainer.expandButton
                                        .setImageResource(R.drawable.ic_action_collapse);
                                viewContainer.expandLayout
                                        .setVisibility(View.VISIBLE);
    
                            }
                        }
                    });