Ethan Allen
Ethan Allen

Reputation: 14845

How do I set the background of an Activity with code?

I'm currently using the XML layout to set my Activity background. How can I do this in the Java code instead?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="@drawable/bg" >

Upvotes: 1

Views: 128

Answers (2)

zienkikk
zienkikk

Reputation: 2414

Before calling Activity.setContentView(View) use one of the setBackground...() methods on your main View.

Upvotes: 2

SSemashko
SSemashko

Reputation: 1497

Set an id attribute to your parent RelativeLayout. Than find it by id in activity code and use setBackgroundResource method.

Upvotes: 1

Related Questions