Android - CardView backgroundcolor tiempo ejecución

 
Vista:

CardView backgroundcolor tiempo ejecución

Publicado por Luis Delgado (1 intervención) el 21/02/2016 00:49:53
Hola amigos de este foro.
Soy principiante en la programación en android studio.

Mi pregunta es la siguiente: Como le puedo cambiar el color de fondo dinámicamente a una tarjeta (cardview)? lo he intentado con el siguiente código.

Card.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:cardView="http://schemas.android.com/tools"
    android:id="@+id/idCardView"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_margin="8dp"
    android:layout_height="match_parent"
    card_view:cardBackgroundColor="#00CED1">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/txtTitle"
            android:padding="10dp"
            android:layout_width="match_parent"
            android:textStyle="bold"
            android:textSize="15dp"
            android:textColor="@android:color/black"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>



MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class MainActivity extends AppCompatActivity {
 
    ListView lvList;
    ArrayList<ListData> myList = new ArrayList<ListData>();
    ListAdapter listAdapter;
 
    CardView cardView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
 
        lvList = (ListView) findViewById(R.id.lvList);
        listAdapter = new ListAdapter(MainActivity.this, myList);
        lvList.setAdapter(listAdapter);
 
        cardView = (CardView) findViewById(R.id.idCardView);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ListData mLog = new ListData();
                mLog.setTitle("titulo");
                mLog.setDescription("descripcion");
 
                cardView.setCardBackgroundColor(Color.parseColor("#AAAAAA"));
 
                myList.add(0, mLog);
                listAdapter.notifyDataSetChanged();
            }
        });
    }

Me marca un NullPointerException el la linea cardView.setCardBackgroundColor(Color.parseColor("#AAAAAA"));

De antemano muchas gracias por su ayuda.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder