Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change activity background from code

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.

like image 889
xander27 Avatar asked Jun 20 '12 07:06

xander27


People also ask

How do you get the background color on a view?

To get background color of a Layout: LinearLayout lay = (LinearLayout) findViewById(R. id. lay1); ColorDrawable viewColor = (ColorDrawable) lay.

How do I change my background on Kotlin?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.

How can change relative layout background color in android programmatically?

Programmatically using Java code. java file, then you can do it by using setBackgroundColor() method on your layout. To your Parent View Layout add an attribute @id/id_name and map it to a variable in your java file.


1 Answers

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);
like image 65
ρяσѕρєя K Avatar answered Oct 14 '22 04:10

ρяσѕρєя K