Search code examples
javaandroidandroid-layoutandroid-fragmentsandroid-imageview

ImageView In Fragment not showing


I have a strange issue that I have a workaround for but wondered if anyone could shed some light on why this is happening and a more elegant fix maybe?

I have a ViewPager with a series of Fragments. The Fragments use their own layout and when an ImageView is added to the layout the ImageView isnt rendered at runtime.

If I explicitly set the imageDrawable for the ImageView inside the Fragment's onCreateView method (even though the image is the same image as being referenced in the layout xml then the image shows fine.

Here is my Fragment xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/baseRelLayout"
    android:background="@color/colorPrimary">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/flamerite_remote_6"
        android:layout_marginStart="36dp"
        android:id="@+id/remote6ButtonImage"
        android:layout_alignParentBottom="false"
        android:layout_alignStart="@+id/textView3"
        android:scaleType="fitXY"
        android:background="@color/colorPrimary"
        android:layout_alignBottom="@+id/remote9ButtonImage" />
</RelativeLayout>

And here is where I am re-setting the image drawable inside onCreateView

public class WizardFragment extends Fragment {
    public static final String STEP_NUMBER = "STEP_NUMBER";

    public static  final WizardFragment newInstance(String message){
        WizardFragment f = new WizardFragment();
        Bundle bdl = new Bundle(1);
        bdl.putString(STEP_NUMBER,message);

        f.setArguments(bdl);
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.wizard_fragment_step_1, container, false);
        ImageView remoteImage9Button = (ImageView)v.findViewById(R.id.remote9ButtonImage);
        remoteImage9Button.setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.flamerite_remote_9, null)); //THIS IS THE LINE THAT MAKES THE IMAGE WORK AGAIN
        return v;
    }
}

I can of course keep this re-set of the imageDrawable in but think it is strange that the image shoudlnt be showing when only set in the xml layout

Any thoughts greatly appreciated!!


Solution

  • I do not know the reason, but in my case the same issue was resolved when I changed

    image src definition from

    app:srcCompat="@drawable/logo"
    

    to

    android:src="@drawable/logo"