Change activity background from code
39,136
Solution 1
first add LinearLayout id as in your layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/linearLayoutid"
android:minWidth="25px"
android:minHeight="25px"
android:background="@drawable/background_fingerboard"
and in code part set background as::
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayoutid);
linearLayout.setBackgroundResource(R.drawable.background_fingerboard);
Solution 2
In java we do like this
Give id to LinearLayout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id=@+id/parentLayout
Then in code
LinearLayout layout=(LinearLayout)findViewById(R.id.parentLayout);
layout.setBackgroundColor(Color.BLUE); Or
layout.setBackgroundDrawable(d); Or
layout.setBackgroundResource(resid);
Solution 3
LinearLayout vi = (LinearLayout ) findViewById(<id>)
vi.setBackgroundResource(R.drawable.<id2>);
you need to provide the id to your LinearLayout in XML as well....
Author by
xander27
Updated on July 09, 2022Comments
-
xander27 almost 2 years
I have an activity with a background:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:minWidth="25px" android:minHeight="25px" android:background="@drawable/background_fingerboard" />
How can I change background image from code? I actually use mono, but Java samples will also be helpful.