0

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. -

enter image description here

But when I inflate the button in RecyclerView I get an unexpected color, which I haven't defined anywhere in the app -

enter image description here

(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

Community
  • 1
  • 1
PRYM
  • 513
  • 4
  • 12
  • The problem is in the theme. Possible solutions are available here : http://stackoverflow.com/questions/26519979/coloring-buttons-in-android-with-material-design-and-appcompat If the problem continues after checking there, please let me know it and solve it together. – Burak Cakir May 21 '16 at 22:10
  • I have tried the solution in the link too, when I apply the orange color, button is displayed as dark grey – PRYM May 21 '16 at 22:14
  • Can you post your code that inflates view? – Emre Aktürk May 21 '16 at 23:14
  • @EmreAktürk Added the relevant code – PRYM May 22 '16 at 07:25
  • It works now, found the issue – PRYM May 22 '16 at 07:42

2 Answers2

1

I found the problem, not sure about the reason. But the solution works.

While creating the adapter I was doing -

 adapter = new ListAdapter(getApplicationContext(), Posts);

and then the theme issue was there, but when i changed getApplicationContext() to getBaseContext(), it worked -

 adapter = new ListAdapter(getBaseContext(), Posts);

The problem appears to be that I was creating the adapter from a asynchronous thread

PRYM
  • 513
  • 4
  • 12
0

Can you please try to change your inflation code like

LayoutInflater.from(getContext()).inflate(R.layout.yourcell, parent/container, false);
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • inflater = LayoutInflater.from(getApplicationContext()); inflater.inflate(R.layout.home_individual_post, parent, false); I am doing this, which I think is the same as yours, still the issue – PRYM May 22 '16 at 07:32