I have a parent view and a child view. The child views are inflated programmatically. The child view has a TextView
with some background. The TextView
has a onclick event on it. What I want is when the user clicks the first TextView its background color should change and when the user selects the second one the background of the first textview should come back to its default background and the second one should change.
I have changed the background color but I am having difficulty in restoring the backgrounds. Here's my code:
My Parent View:
<com.google.android.flexbox.FlexboxLayout
android:id="@+id/viewCategoriesLinearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch"
android:layout_marginTop="@dimen/_10sdp"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginStart="@dimen/_5sdp" />
My Child View:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:id="@+id/categoryChip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/category_tag_background"
android:text="TAG"
android:layout_marginTop="@dimen/_25sdp"
android:paddingLeft="@dimen/_5sdp"
android:paddingTop="@dimen/_5sdp"
android:paddingBottom="@dimen/_5sdp"
android:paddingRight="@dimen/_5sdp"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginRight="@dimen/_5sdp"
android:textColor="@color/textcolorLogin"
android:textSize="@dimen/_11ssp" />
<TextView
android:id="@+id/categorychipid"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"/>
</LinearLayout>
This is how I'm inflating and changing background color:
FlexboxLayout categoryInformationHeader = view.findViewById(R.id.viewCategoriesLinearlayout);
final View editChildView = getActivity().getLayoutInflater().inflate(R.layout.tag_layout, null);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10,2,10,2);
editChildView.setLayoutParams(layoutParams);
editParentLL.addView(editChildView);
final TextView editTvChip = editChildView.findViewById(R.id.chip12345);
final String shelfShareCategoryTitle = crsEditShelfShare.getString(crsEditShelfShare.getColumnIndex("title"));
editTvChip.setText(shelfShareCategoryTitle);
editTvChip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editTvChip.setBackgroundResource(R.color.colorPrimary);
}
});