I try to show a PNG file in my activity with little success.
I have a file called arrow.png
in my res/drawable
, 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 manuals / examples I read, all of them gave me the same instructions as what I created above. But when I run this program, nothing appears (only a thin image is displayed in the TextView). I am rather puzzled why this is happening since there are no error messages.
I checked if the PNG file is damaged, but this is also not the case. I cleaned up the project and restarted it, but it still doesn't help. What am I missing / not noticing?
source share