I am getting something weird while implementing a RecyclerView which I can't understand.
I am using a button with default style, which uses colorAccent from values/colors.xml
<Button
android:id="@+id/next_button"
style="@style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="true"
android:text="Next" />
When I use this button statically in layout XML I get the expected button color i.e. -
But when I inflate the button in RecyclerView I get an unexpected color, which I haven't defined anywhere in the app -
(The Button I am adding is the last element of the RecyclerView as explained here - How to create RecyclerView with multiple view type?
The code to inflate the view -
//ListAdapter constructer
public ListAdapter(Context context, List<postDetail> dataList1) {
this.context = context;
this.dataList = dataList1;
inflater = LayoutInflater.from(context);
}
public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View convertView;
if(viewType == 0){
convertView = inflater.inflate(R.layout.home_individual_post, parent, false);
ListViewHolder viewHolder = new ListViewHolder(convertView,0);
return viewHolder;
} else {
convertView = inflater.inflate(R.layout.prev_next,parent,false);
ListViewHolder viewHolder = new ListViewHolder(convertView,1);
return viewHolder;
}
}
//The button is in prev_next.xml
prev_next.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/next_prev_button"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:gravity="center_vertical"
>
<Button
android:id="@+id/prev_button"
style="@style/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="-4dp"
android:enabled="true"
android:textColor="#FFF"
android:text="Previous"
/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/next_button"
style="@style/MyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="-4dp"
android:textColor="#FFF"
android:enabled="true"
android:text="Next" />
</LinearLayout>
The background color issue is in both layout - prev_next.xml and home_individual_post.xml
I found the problem and has mentioned the same in an answer below