Search code examples
androidpngdrawable

How to Display a PNG from drawable Folder?


I have been trying to display a PNG file in my activity with little success.

I have a file named arrow.png in my res/drawable folder and my code is as follows:

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/arrow" />

</RelativeLayout>

Java:

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imgView = (ImageView) findViewById(R.id.image);
        imgView.setImageResource(R.drawable.arrow);
    }
}

Of all the guides/examples I've read, they all gave me the same instructions as what I've created above. But when I run this program, there's nothing that shows up (the TextView displays just fine – image is nonexistent). I'm pretty baffled as to why this is happening, as there are no error messages.

I've checked to see if the PNG file is corrupt, but that's also not the case. I've cleaned the project and restarted, but that still doesn't help. What am I missing/overlooking?


Solution

  • Your image might be too large.