1

I'm developing an application for android 2.3.3 platform and importing Compatibility package for using Fragment class. First of all I wasn't able to run the first trivial example here http://developer.android.com/guide/topics/fundamentals/fragments.html

the application crashes when trying to inflate fragment.

I "solved" hardcoding the content of fragment instead of using xml file - I create an ImageView inside onCreateView:

public static class SampleFragment extends Fragment 
{
  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
   {        
     ImageView image = new ImageView(getActivity());
     image.setImageResource(R.drawable.sample_image);
     image.setLayoutParams(new FrameLayout.LayoutParams(120, 30, Gravity.CENTER));
     return image;
  }
}

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
<fragment 
    android:name="org.blackimp.ListenActivity$SampleFragment"
    android:id="@+id/noise_meter_fragment"
    android:layout_weight="0"           
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
...
</LinearLayout>

the issue is that it completely ignores the layout width and height set by code - the actual code converts dp in px before calling setLayoutParams. it extends image as to fill the whole fragment width.

  1. How can I set image width and height - meaning to scale it - ?
  2. is there a way to make it work by inflating and describing fragment content by xml with compatibility package - yes I extended FragmentActivity but it doesn't work anyway -?

thank you

1 Answers1

0

You have to understand the use of fragments here. You include the fragment in your XML with the width set to "fill_parent". The fragment in this case just returns the ImageView. It is as you would put the ImageView directly into your layout. You have to set the pixel information in the XML of the fragment or you add the fragment programmatically.

You can find more information about scaling ImageViews here.

Community
  • 1
  • 1
Franziskus Karsunke
  • 4,948
  • 3
  • 40
  • 54
  • thank you: I use a fragment as I need to replace it with a different content by an animation as application goes on. dividing the layout by fragments was a requirement too: fragments must stretch for different resolutions, while images should scale inside them. Beside if I explicit the image inside fragment in xml layout file, the image is always put on the top of the fragment thou I specify I want it in the center. –  Oct 12 '11 at 12:36
  • It is important to set the android:scaleType="centerCrop" option on the ImageView if you want it to be scaled. Can you post the code where you change the fragment? Then i can tell you what you have to fix in order to scale the image correctly. – Franziskus Karsunke Oct 12 '11 at 12:46
  • I'm still not changing fragment. By now I have simply to solve the issue to set specific dimension for image inside a fragment by code: if I don't create an ImageView by code inside the OnCreateView and use only xml, the application crashes. if I create the ImageView by code, I'm not able to set a different size than the one that fills the fragment. my idea was to leave some space between fragment border and image, setting explicitly image dimension by dp translated in px. –  Oct 12 '11 at 13:02
  • Have you tried using image.setScaleType(ImageView.ScaleType.CENTER_CROP) in the onCreateView? – Franziskus Karsunke Oct 12 '11 at 14:01
  • I've put my image inside a LinearLayout and now I can set its width and height by the same code I've written above. By the way now there 's no way I can show the image vertically centered inside its fragment. Fragment in xml has width that fills parent while fixed height to 60dp. The linear layout fills parent both vertically and horizontally. the image gravity is set to "center". –  Oct 13 '11 at 09:03
  • try ImageView.ScaleType = FIT_XY, it will scale the image to fit to the width and height of image view. – Deep P Mar 21 '17 at 23:04