androidcardlayout

Card View Click on Card Move To New Activity


I am new to Android programming and was working on a card layout. I was wondering, how do I make it clickable?

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"

I have that on my card widget and then I wanted to know where to put an on clickable action? I want to be able to click the card, it gets the id of the card, and then displays a new intent activity

This is my code for the activity to load the adapter

setContentView(R.layout.activity_my);


    RecyclerView recList = (RecyclerView) findViewById(R.id.cardList);
    recList.setHasFixedSize(true);
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    recList.setLayoutManager(llm);

    ContactAdapter ca = new ContactAdapter(createList(30));

    recList.setAdapter(ca);

Solution

  • If you used the implementation correctly, your code should go like this:

    card - is the card view you instantiated to display on your ui
    
    
    card.setOnClickListener(...);
    

    In your implementation of the onClickListener, you should have this:

    @Override
    public void onClick(Card c ,View v) {
        Intent intent = new Intent(MyActivity.this, NextActivity.class);
        startActivity(intent);
    }
    

    that is pretty much all you need to start a new activity from the card