i am using page view in android and i view in this page viewer an images.
so i want to make event for on click. however, i call the image from another class like root view and i want the response of event from main class openoptionmenu() so i is there any way to call the event from second class when click the image?

my code for mainactivity

public class ScreenSlideActivity extends FragmentActivity  {



    public static String Pac_name="";
    public static boolean me_r=false;

    private ViewPager mPager;


    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_screen_slide);

        Pac_name=getApplicationContext().getPackageName();
        // Instantiate a ViewPager and a PagerAdapter.
        mPager = (ViewPager) findViewById(R.id.pager);
        mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
        mPager.setAdapter(mPagerAdapter);

        cu_sura=StoreManager.getCurrentSora(ScreenSlideActivity.this);
        if(cu_sura == 0)
        {
            mPager.setCurrentItem(604);
        }
        else
        {
            mPager.setCurrentItem(cu_sura);
        }

        mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                // When changing pages, reset the action bar actions since they are dependent
                // on which page is currently active. An alternative approach is to have each
                // fragment expose actions itself (rather than the activity exposing actions),
                // but for simplicity, the activity provides the actions in this sample.

                invalidateOptionsMenu();

            }
        });

        mPager.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                // TODO Auto-generated method stub
                openOptionsMenu();
                return false;

            }
        });

       +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    }



in the place of ++++++ i want to call event of onclick for image in the second class





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout containing a title and body text.
        ViewGroup rootView = (ViewGroup) inflater
                .inflate(R.layout.fragment_screen_slide_page, container, false);


        ScreenSlideActivity.cu_sura=mPageNumber;
        int num=(mPageNumber -605)*-1;
        String name="p"+num;

        int imgId = getResources().getIdentifier(name, "drawable",com.bilal.read_quran.ScreenSlideActivity.Pac_name );
      //    Bitmap b = BitmapFactory.decodeResource(getResources(), imgId);
        ((ImageView) rootView.findViewById(R.id.imageView1)).setImageResource(imgId);
        ((ImageView) rootView.findViewById(R.id.imageView1)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                final Handler mHandler = new Handler();

            }
        });
        // Set the title view to show the page number.
      /*  ((TextView) rootView.findViewById(android.R.id.text1)).setText(
                getString(R.string.title_template_step, mPageNumber + 1));
*/
        return rootView;
    }

Dani AI

Generated

is on the right track: use a listener to keep the fragment/second class decoupled from the activity. Two safe options follow — the recommended listener approach (best for maintainability and passing data) and a quick direct call if you only need to open the options menu.

Listener pattern (recommended)

// in the fragment file
public interface OnImageClickListener {
    void onImageClicked(int pageNumber);
}

private OnImageClickListener callback;

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnImageClickListener) {
        callback = (OnImageClickListener) context;
    } else {
        throw new ClassCastException(context.toString()
            + " must implement OnImageClickListener");
    }
}

// when image is clicked
imageView.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        if (callback != null) callback.onImageClicked(mPageNumber);
    }
});
// in ScreenSlideActivity
public class ScreenSlideActivity extends FragmentActivity
        implements OnImageClickListener {
    @Override
    public void onImageClicked(int pageNumber) {
        // keep UI calls on UI thread
        runOnUiThread(new Runnable() {
            @Override public void run() { openOptionsMenu(); }
        });
    }
}

Quick alternative (less decoupled)

// inside fragment click handler
if (isAdded() && getActivity() != null) {
    getActivity().openOptionsMenu();
}

Troubleshooting and cautions

  • Guard casts to avoid ClassCastException (use instanceof).
  • Use isAdded()/getActivity() checks to avoid NPEs when fragment is detached.
  • Avoid keeping Activity references in static fields (memory leaks).
  • Prefer the listener when you need to pass data or keep logic testable.

This addresses ’s goal: let the image click in the fragment trigger the activity’s menu logic while keeping the design clean and safe.

  • Create custom listener
  • Register listener with component to receive update info
  • Call listener on touch and pass data to be received by listeneing component
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.