0

I have some code which I run in normal activities in method onFocusChanges, it is important to run it just there as the code requires the activity to be loaded first as it get images width and heights from the view:

in MainActivity

public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        SizeModifier sizeModifier = SizeModifier.getInstance(this);
        sizeModifier.adjust
    }

so all this is working just fine, the problem is this main view contains a fragment changes according to button press like that

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.main_view_fragment,fragment).commit();

the fragment of course has activity and xml, I want now when the fragment open to run the previous code when focus changes but the problem is fragments does not have onFocusChange, I tried all onStart onResume onCreateActivity but non seems to work as I want, of course the method is called but all images widths are returned 0, so is there away to be sure that images will return correct width or some alternative to onFocusChanges

Aya
  • 120
  • 1
  • 2
  • 8

1 Answers1

0

Ok, finally I found what could do the purpose, simply for fragments we add this simple code to the onCreateView after inflating the view like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater .inflate(R.layout.activity_courses_fragment, container, false);
    // >>>> this is the thread which will run when every thing is ready 
    rootView.post(new Runnable() {
        @Override
        public void run() {
            // >>> this method now can get width with no problems
            SizeModifier.getInstance(getContext()).adjust(rootView);
        }
    });
    return rootView;
}

special thanks to @Staffan for this idea as solution for another question

Aya
  • 120
  • 1
  • 2
  • 8