I start the intent to pick an image from gallery, then scale the image and dinamically add ImageView to the layout.
I want this image to be reloaded on orientation change. I set setRetainInstance(true) in onCreate. However, after orientation change, when I try to get appropriate width for bitmap to be scaled to by calling getWidth() on FrameLayout which is supposed to be its container, I get 0.
public class NewWallpaper extends Fragment {
private static final int PICK_BACKGROUND = 1;
private BitmapFactory.Options bitmapFactoryOptions;
// Data
private Uri backgroundURI = null;
private WeakReference<Bitmap> backgroundBitmap = null;
// Views
FrameLayout backgroundContainer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_new_wallpaper, container, false);
// BitmapFactory options
bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inPurgeable = true;
Button pickBackground = (Button) view.findViewById(R.id.buttonPickBackground);
pickBackground.setOnClickListener(pickBackgroundListener);
backgroundContainer = (FrameLayout) view.findViewById(R.id.frameLayoutbackgroundContainer);
if (backgroundURI != null)
setBackgroundPreview();
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
private Button.OnClickListener pickBackgroundListener = new Button.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, PICK_BACKGROUND);
}
};
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_BACKGROUND && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {
backgroundURI = data.getData();
setBackgroundPreview();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void setBackgroundPreview() {
try {
backgroundContainer.removeAllViews();
Bitmap background = BitmapHelper.loadScaledBitmap(getActivity(), backgroundURI, backgroundContainer.getWidth());
backgroundBitmap = new WeakReference<Bitmap>(background);
// returns 0
int containerWidth = backgroundContainer.getWidth();
ImageView backgroundView = new ImageView(getActivity());
backgroundView.setLayoutParams(new LinearLayout.LayoutParams(
backgroundContainer.getWidth(),
BitmapHelper.calculateHeight(
background.getWidth(),
background.getHeight(),
backgroundContainer.getWidth())
));
backgroundView.setAdjustViewBounds(true);
backgroundView.setScaleType(ImageView.ScaleType.FIT_XY);
backgroundView.setImageBitmap(background);
backgroundContainer.addView(backgroundView);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I can't seem to find a way to repopulate views after orientation change. Could someone tell me what I'm doing wrong?
Don't call
setBackgroundPreview()
fromonCreateView
. You can't measure the view if it hasn't finished being created.Call
setBackgroundPreview()
inonActivityCreated
instead.