Rajan M
Rajan M

Reputation: 5

how to add onClickListener to buttons inside CardLayout

I am using Gabrielemariotti's Cardslib library to implement card layout in my android application. I am using a custom layout for my cards. Below is the code for creating custom cards:

Card card = new Card(getActivity().getApplicationContext(), R.layout.status_card);
card.setTitle("sample title");

I have three buttons at the bottom of my card (like buttons in Facebook android app). I want to set onClickListener for these buttons. But I am not sure how to do that.

Please help me here.

Thanks,

Upvotes: 0

Views: 1181

Answers (2)

Gabriele Mariotti
Gabriele Mariotti

Reputation: 364451

You have to define your layout. Then create a Card with this layout, and override the setupInnerViewElements method. Inside this method you can define your OnClickListener on your buttons, and you can access to all card's values.

 public class CustomCard extends Card {

        /**
         * Constructor with a custom inner layout
         *
         * @param context
         */
        public CustomCard(Context context) {
            super(context, R.layout.carddemo_mycard_inner_content);
        }


        @Override
        public void setupInnerViewElements(ViewGroup parent, View view) {

            //Retrieve button 
            Button myButton = (Button) view.findViewById(R.id.myButton);
            if (myButton != null) {
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getContext(), "Click Listener card=" + getId(), 
                              Toast.LENGTH_LONG).show();
                    }
                });
            }
        }
    }

Upvotes: 1

Alex K
Alex K

Reputation: 8338

I have an easy solution for this.

So another way to add onClick listeners, which is a bit easier, is through the XML.

In the xml for the button, you add this line:

android:onClick="methodName"

Where 'methodName' is obviously the name of a method. This will call the method whenever the button is clicked. The next step is obvious - just go into your java activity and create the method that you want called, making sure to take the View as a parameter. So you'd have something like this in your activity class:

public void methodName(View view) {
    Log.v("appTag","BUTTON WAS PRESSED");
    //whatever you want to do here
}

This is a shortcut to creating a whole onClickListener.

Hope that helps. Good luck :)

EDIT:

Remember, you're passing in a view here, so you can get whatever you want off of that view. Since you commented that you need to get the text off of your card, I'll show you how to do that.

Here is your method for this case:

public void methodName(View view) {
    Log.v("appTag","BUTTON WAS PRESSED");
    TextView textFromCard = view.findViewById(R.id.THE_ID_YOU_GAVE_YOUR_TEXTVIEW_IN_THE_XML);
    String textFromTextView = textFromCard.getText().toString();
    //do whatever you want with the string here
}

Upvotes: 0

Related Questions