2

I have found many similar questions and answers, but I was unsuccessful with those fixes.

I want to set an ImageView to the background so that everything else I am doing is in front of it. (I do not want to use the xml background option because I will be changing my background within the activity and I cannot figure out how to prevent it from stretching.)

Thanks!

bg = (ImageView) findViewById(R.id.bg);
bg.setImageResource(R.drawable.marbles);

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

<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"
    tools:context=".Nim" >


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

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/tvTitle"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvTitle"
        android:layout_centerHorizontal="true"
        android:text="@string/tvInfo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <SeekBar
        android:id="@+id/sbMarbles"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/tvInfo" />

    <Button
        android:id="@+id/butMarbles"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:text="@string/butMarbles" />

    <EditText
        android:id="@+id/tvMarbles"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/butMarbles"
        android:layout_toRightOf="@+id/sbMarbles"
        android:ems="10"
        android:inputType="number" />

    <TextView
        android:id="@+id/tvInfo2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvInfo"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:text="@string/tvInfo"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/butYes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tvInfo2"
        android:layout_marginTop="83dp"
        android:layout_toLeftOf="@+id/tvTitle"
        android:text="@string/butYes" />

    <Button
        android:id="@+id/butNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/butYes"
        android:layout_alignBottom="@+id/butYes"
        android:layout_toRightOf="@+id/tvInfo2"
        android:text="@string/butNo" />

</RelativeLayout>

LogCat

01-07 21:00:00.453: E/AndroidRuntime(568): FATAL EXCEPTION: main
01-07 21:00:00.453: E/AndroidRuntime(568): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.gameofnim/com.evorlor.gameofnim.Nim}: java.lang.ClassCastException: android.widget.TextView
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.os.Looper.loop(Looper.java:123)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:00:00.453: E/AndroidRuntime(568):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 21:00:00.453: E/AndroidRuntime(568):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:00:00.453: E/AndroidRuntime(568):  at dalvik.system.NativeStart.main(Native Method)
01-07 21:00:00.453: E/AndroidRuntime(568): Caused by: java.lang.ClassCastException: android.widget.TextView
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.evorlor.gameofnim.Nim.listener(Nim.java:56)
01-07 21:00:00.453: E/AndroidRuntime(568):  at com.evorlor.gameofnim.Nim.onCreate(Nim.java:41)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:00:00.453: E/AndroidRuntime(568):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:00:00.453: E/AndroidRuntime(568):  ... 11 more
01-07 21:18:20.403: E/AndroidRuntime(704): FATAL EXCEPTION: main
01-07 21:18:20.403: E/AndroidRuntime(704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.gameofnim/com.evorlor.gameofnim.Opening}: java.lang.NullPointerException
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.os.Looper.loop(Looper.java:123)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:18:20.403: E/AndroidRuntime(704):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 21:18:20.403: E/AndroidRuntime(704):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:18:20.403: E/AndroidRuntime(704):  at dalvik.system.NativeStart.main(Native Method)
01-07 21:18:20.403: E/AndroidRuntime(704): Caused by: java.lang.NullPointerException
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.evorlor.gameofnim.Opening.listener(Opening.java:36)
01-07 21:18:20.403: E/AndroidRuntime(704):  at com.evorlor.gameofnim.Opening.onCreate(Opening.java:28)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:18:20.403: E/AndroidRuntime(704):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:18:20.403: E/AndroidRuntime(704):  ... 11 more
01-07 21:19:32.743: E/AndroidRuntime(737): FATAL EXCEPTION: main
01-07 21:19:32.743: E/AndroidRuntime(737): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evorlor.gameofnim/com.evorlor.gameofnim.Opening}: java.lang.NullPointerException
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.os.Looper.loop(Looper.java:123)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.main(ActivityThread.java:3683)
01-07 21:19:32.743: E/AndroidRuntime(737):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 21:19:32.743: E/AndroidRuntime(737):  at java.lang.reflect.Method.invoke(Method.java:507)
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-07 21:19:32.743: E/AndroidRuntime(737):  at dalvik.system.NativeStart.main(Native Method)
01-07 21:19:32.743: E/AndroidRuntime(737): Caused by: java.lang.NullPointerException
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.evorlor.gameofnim.Opening.listener(Opening.java:36)
01-07 21:19:32.743: E/AndroidRuntime(737):  at com.evorlor.gameofnim.Opening.onCreate(Opening.java:28)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 21:19:32.743: E/AndroidRuntime(737):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-07 21:19:32.743: E/AndroidRuntime(737):  ... 11 more
Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • Define "everything else." If you mean you want a background behind your entire activity or layout, then set a background in your RelativeLayout, LinearLayout, etc. – leenephi Jan 08 '13 at 02:13
  • i want to be able to change the background programmatically. that is why i do not want it in my xml as a background, but instead as an ImageView. – Evorlor Jan 08 '13 at 02:14
  • everything else means everything else going on in the activity. buttons, textviews, other images...everything else – Evorlor Jan 08 '13 at 02:15

1 Answers1

9

Use a RelativeLayout or a FrameLayout and define your ImageView as the first child. The first objects defined are the lowest in the z-order (i.e. they will be "below" the other views).

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • i want to be able to change the background programmatically. that is why i do not want it in my xml as a background, but instead as an ImageView. – Evorlor Jan 08 '13 at 02:15
  • Re-read my answer. Define your ImageView in XML, then acquire it programmatically with findViewById(), and set the background image there. – Kevin Coppock Jan 08 '13 at 02:16
  • You've already done it, you've posted the code above. Just make that view the first in your XML layout hierarchy. – Kevin Coppock Jan 08 '13 at 02:18
  • it causes my program to crash for some reason – Evorlor Jan 08 '13 at 02:19
  • i posted above the new xml. no idea why its crashing now. all i did was move the imageview from the bottom of the relativelayout to the top of it – Evorlor Jan 08 '13 at 02:20
  • its very strange...its not even getting to the activity which is changed before its crashing. i have no idea what the problem is – Evorlor Jan 08 '13 at 02:24
  • well i will assume your answer will fix it and ill ask a new question so that I can clarify the problem. thanks for your help! – Evorlor Jan 08 '13 at 02:28
  • No problem! As for your errors, somewhere you're trying to either cast a TextView to something else, or something else to a TextView. Double check your view IDs when you're using findViewById(). – Kevin Coppock Jan 08 '13 at 02:32