Search code examples
androidandroid-fragmentsonbackpressed

Get id of fragment's view from Activity


I have a video view in fragment. When I press button it is showing as full view in landscape mode but when I press back it should be come to normal view. For this I am handling onBackPressed() in Activity but I can't get the id of that video view in Activity. So how to get the video view id in Activity's onBackPressed() which is there in Fragment class.

This is the code in Fragment to show video in full view:

fullScreen = (ImageView) controller.findViewById(R.id.fullscreen);
fullScreen.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, 8.0f);
        videoView.setLayoutParams(param);
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    }
});

This is what I need to handle in the Activity:

@Override
public void onBackPressed() {

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 5.0f);
    videoView.setLayoutParams(param);

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
        super.onBackPressed();
    }

}

But I need videoView id here. How to get it in Activity?


Solution

  • The brute force way would be to get that view from your fragment:

    
    @Override
    public void onBackPressed() {
    
        VideoView videoView = yourFragment.getView().findViewById(R.id.your_video_view);
        ...
    }