Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to go next Activity click On CardView Item in android

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 24.9k
    Comment on it

    In the below example I have created a RecyclerView, In RecyclerView I have added cardView item, so when you click on cardview item it will open next activity.

    In below code, first I have added RecyclerView and CardView library , In second step, In actvity_main.xml layout I have created RecyclerView and included Toolbar. In third step I have created toolbar.xml layout here I have added toolbar. In forth step I have created row.xml layout here I have added CardView , ImageView and TextView.

    In fifth step I have created Item class here I have defined image title and subtitle. In six step I have created MyAdapter class and extend this class with RecyclerView and adapter, here I have also created MyViewHolder class and I have used onClick method to get position item.

    In MainActivity I have set adapter and created prepareItem class to add items, image title and subtitle value. In next step I have created a ClickListener interface. In Last step I have created Main2Actvity here I have used OnClickListener() and Intent method for retuning previous activity. You can see below example code it clearly describe you How to go next Activity click On CardView Item in android.

     

    Step(1)-I have added RecyclerView, CardView and design library in build.gradle file -

    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'com.android.support:design:22.2.1'

     

    Step(2)-activity_main.xml-

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eed9faf1">
    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"></include>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical" />
    </LinearLayout>
    
    


    Step(3)-Created a toolbar.xml layout-

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:local="http://schemas.android.com/apk/res-auto" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:minHeight="?attr/actionBarSize" android:background="?attr/colorPrimary" local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" local:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

     

    Step(4)-Created a new row.xml layout-

    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/card"
        android:layout_marginTop="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        card_view:cardCornerRadius="5dp">
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main"
        android:focusable="true"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:clickable="true"
        android:background="?android:attr/selectableItemBackground"
        android:orientation="vertical">
        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:id="@+id/icon"
            android:paddingRight="10dp"
            android:paddingLeft="10dp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/title"
            android:layout_toRightOf="@+id/icon"
            android:paddingBottom="10dp"
            android:paddingLeft="10dp"
            android:textColor="#CC0033"
            android:textSize="24dp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/subtitle"
            android:layout_toRightOf="@+id/icon"
            android:layout_below="@+id/title"
            android:paddingLeft="14dp"
            android:textColor="#3399FF"
            android:textSize="14dp"/>
    </LinearLayout>
    </android.support.v7.widget.CardView>

     

    Step(5)-Created a new Item class-

    public class Item {
        private int imageId;
        private String title;
        private String subtitle;
        public Item(Integer imageId,String title,String subtitle){
            this.imageId=imageId;
            this.title =title;
            this.subtitle=subtitle;
        }
        public int getImageId(){
            return imageId;
        }
        public void setImageId(int imageId){
    
            this.imageId=imageId;
        }
        public String getTitle(){
            return title;}
        public void setTitle(String title){
            this.title=title;
        }
        public String getSubtitle(){
            return subtitle;}
        public void setSubtitle(String subtitle){
            this.subtitle=subtitle;
        }
    }

     

    Step(6)-Created a new MyAdapter class-

    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
        private List<Item> itemList;
        private  ClickListener clicklistener = null;
        public MyAdapter(List<Item>itemList){
            this.itemList = itemList;
        }
    
    
        public class MyViewHolder extends RecyclerView.ViewHolder  {
            public TextView title, subtitle;
            public ImageView icon;
            private LinearLayout main;
    
    
            public MyViewHolder(final View parent) {
                super(parent);
    
                title = (TextView) parent.findViewById(R.id.title);
                subtitle = (TextView) parent.findViewById(R.id.subtitle);
                icon = (ImageView) parent.findViewById(R.id.icon);
                main = (LinearLayout) parent.findViewById(R.id.main);
                main.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(itemView.getContext(), "Position:" + Integer.toString(getPosition()), Toast.LENGTH_SHORT).show();
                        if(clicklistener !=null){
                            clicklistener.itemClicked(v,getAdapterPosition());
                        }
                    }
                });
            }
        }
        public void setClickListener(ClickListener clickListener){
            this.clicklistener = clickListener;
        }
    
        @Override
        public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row,parent,false);
            return new MyViewHolder(itemView);
        }
        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
    
            Item row = itemList.get(position);
            holder.title.setText(row.getTitle());
            holder.subtitle.setText(row.getSubtitle());
            holder.icon.setImageResource(row.getImageId());
    
        }
        @Override
        public int getItemCount() {
            return itemList.size();
        }
    }

     

    Step(7)- Created a new Interface ClickListener-

    public interface ClickListener {
        public void itemClicked(View view ,int position);
    
    }

     

    Step(8)-MainActivity-

    public class MainActivity extends Activity implements ClickListener {
        private List<Item> itemList = new ArrayList<>();
        private RecyclerView recyclerview;
        private MyAdapter mAdapter;
        private LinearLayout main;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            recyclerview=(RecyclerView)findViewById(R.id.recycler_view);
    
            prepareItem();
    
            mAdapter = new MyAdapter(itemList);
            mAdapter.setClickListener(this);
    
            RecyclerView.LayoutManager mLayoutManger = new LinearLayoutManager(getApplicationContext());
    
            recyclerview.setLayoutManager(mLayoutManger);
    
            recyclerview.setItemAnimator(new DefaultItemAnimator());
    
            recyclerview.setAdapter(mAdapter);
    
        }
    
    
        private void prepareItem() {
            Item item = new Item(R.drawable.two,"Cake1","price is 20");
            itemList.add(item);
            item = new Item(R.drawable.three,"Cake2","price is 50");
            itemList.add(item);
            item = new Item(R.drawable.four,"Cake3","price is 30 ");
            itemList.add(item);
            item= new Item(R.drawable.one,"Cake4","price is 70");
            itemList.add(item);
    
            itemList.add(item);
            item = new Item(R.drawable.three,"Cake2","price is 50");
            itemList.add(item);
            item = new Item(R.drawable.four,"Cake3","price is 30 ");
            itemList.add(item);
            item= new Item(R.drawable.one,"Cake4","price is 70");
            itemList.add(item);
            //mAdapter.notifyDataSetChanged();
    
        }
    
        @Override
        public void itemClicked(View view, int position) {
    
            if(position==2) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("ItemPosition", position);
                startActivity(intent);
            }
              else if (position ==4){
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    intent.putExtra("ItemPosition", position);
                    startActivity(intent);
                }
            else if (position==1){
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("ItemPosition", position);
                startActivity(intent);
            }
             else {
                System.out.println("position...."+position);
            }
    
            }
    
    
        }
    
    

     

    Step(9)Main2Activity-

    public class Main2Activity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent();
                    finish();
    
                }
            });
        }
    
    }

     

    Step(10)activity_main2.xml layout-

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.tiwari.rajshekhar.recyclertitleimage.Main2Activity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />
    
        </android.support.design.widget.AppBarLayout>
    
        <include layout="@layout/content_main2" />
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />
    
    </android.support.design.widget.CoordinatorLayout>

     

     

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: