user14396608
user14396608

Reputation:

My linear layout only flashses a background color instead of persisting in Android

I have a recycler view with a list of linearlayouts that should be clickable. Once selected I want the background color to change, but I'm having trouble doing that. With the code I have right now, the selected linearlayout just flashes a color, and then reverts back to white/transparent. How can I make it so that once it's selected, the color stays?

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listcontentlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/selector">

selector background drawable (the primary dark color is getting flashed):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!--  Active Row-->
<item android:state_selected="true" android:state_focused="false"
    android:state_pressed="false" android:drawable="@color/colorAccent" />
<!--  Pressed Row -->
<item android:state_pressed="true" android:drawable="@color/colorPrimaryDark" /> />

The linear layout is inside of a recycler view for the master/detail flow as well, this is the onClick method

private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            DummyContent.DummyItem item = (DummyContent.DummyItem) view.getTag();
            if (mTwoPane) {
                Bundle arguments = new Bundle();
                arguments.putString(ItemDetailFragment.ARG_ITEM_ID, item.id);
                ItemDetailFragment fragment = new ItemDetailFragment();
                fragment.setArguments(arguments);
                mParentActivity.getSupportFragmentManager().beginTransaction()
                        .replace(R.id.item_detail_container, fragment)
                        .commit();
            } else {

Thanks! Let me know if you need more info.

(This is what I want it to look like)

Upvotes: 1

Views: 80

Answers (1)

Nanzbz
Nanzbz

Reputation: 209

On your onClick method of the LinearLayout set linearLayout.setSelected(true)

If you have set clicklistener to the LinearLayout then use the following snippet,

 @Override
 public void onClick(View view) {
      view.setSelected(true)
     //Your code
 }

Upvotes: 1

Related Questions