Home > Net >  How to set Intent in setOnClickListener in ViewPager2 in Android Studio?
How to set Intent in setOnClickListener in ViewPager2 in Android Studio?

Time:03-24

@Override public void onBindViewHolder(@NonNull ImageViewHolder holder, @SuppressLint("RecyclerView") int position) { holder.imageView.setImageResource(imageList.get(position));

    if(position == imageList.size() - 2){
        viewPager2.post(runnable);
    }
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(view.getContext(), "You clicked on : "   position, Toast.LENGTH_SHORT).show();
            if(position == 0){
                Toast.makeText(viewPager2.getContext(), "Welcome", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(view.getContext(), Activity_2.class);
                view.startActionMode((ActionMode.Callback) intent);

// startActivity(intent) , this class needs to be created it says

            }
        }
    });
}

Toasts addressed are shown but the intent in the same activity is not practised on the same position....the method of startActivity(intent) is also not recognised....please help

CodePudding user response:

Pass your activity to view pager adapter.

activity.startActivity(new Intent(activity,DestnationActivity,.class));

activity = variable name for hold your activity

CodePudding user response:

Try With This Code

Context context;
Intent intent = new Intent(view.getContext(), Activity_2.class);
context.startActivity(intent);
  • Related