-4

I am working on a project in android and i just learn about card view class. I made a card who generates a toast when user clicks on it.
But i also want my card to call another activity when user clicks on it.
I am posting part of my code below.

 btnProceed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            showToast("Proceed to the next step");
            Intent intent = new Intent(MyLocationUsingLocationAPI.this, click_picture.class);
            startActivity(intent);
        }
    });

I have made changes in my code as you said but when i click on proceed button my app crashes.What is wrong with code?

Shashank
  • 1
  • 2
  • 3
    Possible duplicate of [How to start new activity on button click](https://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click) – m0skit0 Oct 02 '18 at 12:56
  • It would be the same as for a button. m0skit0's link has your answer. – Stef Oct 02 '18 at 13:10
  • My question is totally different from this question @m0skit0 and Stef because i am using card instead of button.I know how to start new activity by clicking on button and also tried that and that won't work. – Shashank Oct 02 '18 at 13:11
  • What you want to do with cardview? – Archu Mohan Oct 02 '18 at 13:16
  • I'm confused, starting an activity with a click from anything is no different. How you catch the click can differ, but you already have the toast showing, so that means you know how to get the click, so startActivity(myIntent) is all you need. What exactly are you asking for help with if the link supplied by @m0skit0 is not what you are looking to do? – Sam Oct 02 '18 at 13:17
  • In fact your question is absolutely the same since you already can capture the click, all you need is put the same code for the button onClick listener in your card onClick listener. – m0skit0 Oct 02 '18 at 13:20
  • Possible duplicate of [How to add onClick event on card view](https://stackoverflow.com/questions/46428927/how-to-add-onclick-event-on-card-view) – Tirth Patel Oct 02 '18 at 13:21
  • I have changed my code like you said but my app crashed when i clicked on **proceed** button. – Shashank Oct 02 '18 at 13:28
  • Hi, any stacktrace? Please edit your question. – Christopher Oct 02 '18 at 13:33
  • If your app crashes then you look in your log to see what caused the crash... It's not that difficult – Zun Oct 02 '18 at 13:37
  • https://www.youtube.com/watch?v=d6CfaWW7G5Q This video helped me and solved my problem.If anyone in future face same problem then check out this video. – Shashank Oct 02 '18 at 15:01

1 Answers1

-1

the main idea here is to define your actionClickListener

1. create a custom recycleView Adapter

public class AdapterCustomList extends RecyclerView.Adapter<RecyclerView.ViewHolder>

2. define onItemClickListener interface

public interface OnItemClickListener {
    void onItemClick( whateverArgsYouWant );
}

3. make an attribute of the interface and define a setter for it

    private OnItemClickListener mOnItemClickListener;

public void setOnItemClickListener(final OnItemClickListener mItemClickListener) {
    this.mOnItemClickListener = mItemClickListener;
}

4. append a listener to each item while its created in the adapter class

 @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

   ...
        OriginalViewHolder vItem = (OriginalViewHolder) holder;
        vItem.baseCard.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick( whateverArgsYouWant );
                }
            }
        });

}

this method will be called when items of recycle view is created (in case you use card view inside a recycle view)

5. use your onClickListener in the Activity you want

AdapterCustomList  mAdapter = new AdapterCustomList (getActivity(), recyclerView,yourListItemsHere));
    recyclerView.setAdapter(mAdapter);

// on item list clicked
mAdapter.setOnItemClickListener(new AdapterPostList.OnItemClickListener() {
    @Override
    public void onItemClick( whateverArgsYouWant ) {

        ...
        statements
        ...

    }
});
Namini40
  • 100
  • 7