Search code examples
androidandroid-layoutandroid-fragmentsmaterial-designcardlayout

Card Layout Action Button


I have created a card layout with four cards.

Sample of my card can be found in the image.enter image description here Code for dynamically creating them is below:

public class four_future extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters


    public four_future() {
        // Required empty public constructor
    }
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        RecyclerView recyclerView = (RecyclerView) inflater.inflate(
                R.layout.recycler_view, container, false);
        ContentAdapter adapter = new ContentAdapter(recyclerView.getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        return recyclerView;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView picture;
        public TextView name;
        public TextView description;
        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.four_future, parent, false));
            picture = (ImageView) itemView.findViewById(R.id.card_image);
            name = (TextView) itemView.findViewById(R.id.card_title);
            description = (TextView) itemView.findViewById(R.id.card_text);
            // Adding Snackbar to Action Button inside card
            Button button = (Button)itemView.findViewById(R.id.action_button);
            button.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    Snackbar.make(v, "Action is pressed",
                            Snackbar.LENGTH_LONG).show();
                }
            });


        }
    }
    /**
     * Adapter to display recycler view.
     */
    public static class ContentAdapter extends RecyclerView.Adapter<ViewHolder> {
        // Set numbers of List in RecyclerView.
        private static final int LENGTH = 3
                ;

        private final String[] mPrograms;

        private final Drawable[] mProgramPictures;

        public ContentAdapter(Context context) {
            Resources resources = context.getResources();
            mPrograms = resources.getStringArray(R.array.programs);

            TypedArray a = resources.obtainTypedArray(R.array.program_picture);
            mProgramPictures = new Drawable[a.length()];
            for (int i = 0; i < mProgramPictures.length; i++) {
                mProgramPictures[i] = a.getDrawable(i);
            }
            a.recycle();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.picture.setImageDrawable(mProgramPictures[position % mProgramPictures.length]);
            holder.name.setText(mPrograms[position % mPrograms.length]);

        }

        @Override
        public int getItemCount() {
            return LENGTH;
        }
    }
}

I have action button in every card. How to open different activities on clicking action buttons on different cards. As of now every action button just gives out a snack bar. If I click action button on first card, it must open first_activity.xml. How can I achieve this?


Solution

  • In your onBindViewHolder implementation store the information about which Activity to open in the Button (you can do something as simple as setting the button's tag with the proper Activity tag, or storing an Intent). Then In the ViewHolder where you do

    Button button = (Button)itemView.findViewById(R.id.action_button);
    

    you can retrieve the tag and use that to open the proper activity.

    EDIT: Try this:

    Change your ViewHolder:

        public static class ViewHolder extends RecyclerView.ViewHolder {
            public ImageView picture;
            public TextView name;
            public TextView description;
            public Button button;
            public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
                super(inflater.inflate(R.layout.four_future, parent, false));
                picture = (ImageView) itemView.findViewById(R.id.card_image);
                name = (TextView) itemView.findViewById(R.id.card_title);
                description = (TextView) itemView.findViewById(R.id.card_text);
                // Adding Snackbar to Action Button inside card
                button = (Button)itemView.findViewById(R.id.action_button);
                button.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {
                        if("activity1".equals(button.getTag()){
                            startActivity1();
                        }else if("activity2".equals(button.getTag()){
                            startActivity2();
                        }else{
                        Snackbar.make(v, "Action is pressed",
                                Snackbar.LENGTH_LONG).show();
                        }
                    }
                });
            }
        }
    

    and then modify the onBindViewHolder call to set the tag:

    @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.picture.setImageDrawable(mProgramPictures[position % mProgramPictures.length]);
            holder.name.setText(mPrograms[position % mPrograms.length]);
            if(position == 0) holder.button.setTag("activity1");
            else if(position == 1) holder.button.setTag("activity2");
        }