Search code examples
androidandroid-recyclerviewandroid-linearlayoutbackground-color

Linear layout background color not changing programmatically


I'm creating an Activity Log for Lock and Unlock times, and I need my background color sorted according to Lock or Unlock. However, I cant seem to change the background color of my linear layout no matter what I put. Any help is greatly appreciated thank you!

This is my main Activity

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;

private List<ListItem> listItems;

DatabaseReference database;
@Override
protected void onCreate(Bundle savedInstanceState) {
    LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
    View view = inflater.inflate(R.layout.list_item, null);
    final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    // SharedPreferences preferences=getSharedPreferences(LOCK_PREFS,MODE_PRIVATE);
    database = FirebaseDatabase.getInstance().getReference("Activity Log/device321");

    listItems = new ArrayList<>();

    database.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot usersnapshot : dataSnapshot.getChildren()) {
                Map<String, String> map = (Map<String, String>) usersnapshot.getValue();
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    if (entry.getKey().equals("LockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    if (entry.getKey().equals("UnlockTime")) {
                        System.out.println(entry.getKey());
                        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                    }
                    listItems.add(new ListItem(entry.getKey(), entry.getValue()));
                }


                //   ListItem listItem = usersnapshot.getValue(ListItem.class);
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            adapter = new MyAdapter(listItems, MainActivity.this);
            recyclerView.setAdapter(adapter);
        }


        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
  }
}

This is My XML file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<android.support.v7.widget.CardView

    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin">

    <LinearLayout
        android:id="@+id/damsi"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#baa40f"
        android:orientation="vertical"
        android:padding="16dp"
        android:visibility="visible">

        <TextView
            android:id="@+id/textViewHead"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#ffffff"
            android:text="Heading"

android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/textViewDesc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ffffff"
            android:text="Description" />
    </LinearLayout>

   </android.support.v7.widget.CardView>

</LinearLayout>

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private List<ListItem> listItems;
private Context context;

public MyAdapter(List<ListItem> listItems, Context context) {
    this.listItems = listItems;
    this.context = context;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_item, parent, false);
    return new ViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    ListItem listItem = listItems.get(position);


    holder.textViewHead.setText(listItem.getHead());
    holder.textViewDesc.setText(listItem.getDesc());

}

@Override
public int getItemCount() {
    return listItems.size();
}

public class ViewHolder extends RecyclerView.ViewHolder{
    public LinearLayout layout;
    public TextView textViewHead;
    public TextView textViewDesc;


    public ViewHolder(View itemView) {
        super(itemView);

        textViewHead = (TextView) itemView.findViewById(R.id.textViewHead);
        textViewDesc = (TextView) itemView.findViewById(R.id.textViewDesc);

    }
}


}

Solution

  • Remove this code from MainActivity

     LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
            View view = inflater.inflate(R.layout.list_item, null);
            final LinearLayout layout= (LinearLayout) view.findViewById(R.id.damsi);
    

    Remove these lines from OnDataChangedMethod

    if (entry.getKey().equals("LockTime")) {
                            System.out.println(entry.getKey());
                            layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                        }
                        if (entry.getKey().equals("UnlockTime")) {
                            System.out.println(entry.getKey());
                            layout.setBackgroundColor(Color.parseColor("#FFB71616"));
                        }
    

    In your ViewHolder class, add these lines in your viewholder constructor.

    layout= (LinearLayout) view.findViewById(R.id.damsi);
    

    Add these lines in onBindViewHolder method.

    String key = listItem.getHead();
    if(key.equals("LockTime"))
    {
        System.out.println(entry.getKey());
        layout.setBackgroundColor(Color.parseColor("#FFB71616"));
    }
    if(key..equals("UnlockTime")){
        System.out.println(entry.getKey());
        layout.setBackgroundColor(Color.parseColor("#FF001600"));
        //I changed the color as both were having same hexcodes :P
    }
    holder.textViewHead.setText(listItem.getHead());
    holder.textViewDesc.setText(listItem.getDesc());