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.
- How can I set image width and height - meaning to scale it - ?
- 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