Android Forums

Go Back   TalkAndroid Forums > Android Coders > Android Development, Answers, Tutorials, and Code Snippets
Connect with Facebook

Click Here To Register!
 
 
LinkBack Thread Tools Display Modes
Old 10-11-2010, 09:42 AM   #1
New Member
 
Join Date: Oct 2010
Posts: 2
Friends: 0
View rivierablue's Profile   View rivierablue's Photo Album   View rivierablue's Blog   View Social Groups
I am loading a page via the XML and use the linear layout background shown below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/paper"
android:id="@+id/GameLayout"
>

How can I change this programmatically when I need to? Seems like a simple thing to do but I can't seem to find how. In my activity I set the layout and work with it and at some point I want to change the background.

Believe me I have done much of this already
rivierablue is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 10-23-2010, 12:54 PM   #2
New Member
 
Join Date: Sep 2010
Posts: 5
Friends: 0
View appbee's Profile   View appbee's Photo Album   View appbee's Blog   View Social Groups
Try the following code...
Code:
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.newImage); 
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.GameLayout);
linearLayout.setBackgroundDrawable(drawable);
Hope this helps.
appbee is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 10-24-2010, 11:32 AM   #3
New Member
 
Join Date: Oct 2010
Location: Pomona, CA
Posts: 1
Friends: 0
View cbuisr's Profile   View cbuisr's Photo Album   View cbuisr's Blog   View Social Groups
How do you tell the Resources to use a specific image? If you can put together a sample completed code for the above requested I would appreciate it.

Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.newImage);
cbuisr is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 10-25-2010, 12:06 PM   #4
New Member
 
Join Date: Sep 2010
Posts: 5
Friends: 0
View appbee's Profile   View appbee's Photo Album   View appbee's Blog   View Social Groups
The xml would be like this:

Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/currImage"
android:id="@+id/GameLayout"
/>
The currImage.png or any images reside in the res folder of the Project.
ProjectName/res/drawable/anyImage.png

You would then change the image of the layout in the java file by getting the resource handle.
Code:
public class mainActivity extends Activity 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); //resource handle
	Drawable drawable = res.getDrawable(R.drawable.newImage); //new Image that was added to the res folder
	LinearLayout linearLayout =   (LinearLayout)findViewById(R.id.GameLayout); 
	linearLayout.setBackgroundDrawable(drawable);
    }
}
I hope that answers your question.
appbee is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
Old 10-27-2010, 06:51 AM   #5
New Member
 
Join Date: Oct 2010
Posts: 2
Friends: 0
View rivierablue's Profile   View rivierablue's Photo Album   View rivierablue's Blog   View Social Groups
Sample code works perfect thank you and hope it helps others.
rivierablue is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in Technorati
 

Thread Tools
Display Modes



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change the background color of Tab widget in Android? deep007.mm Android Development, Answers, Tutorials, and Code Snippets 2 07-29-2010 10:47 PM
Fixing background littlebird Android Chat 1 02-14-2010 08:46 AM
Application not run in the background pramod.deore Android Development, Answers, Tutorials, and Code Snippets 0 02-01-2010 03:51 AM
Background lighting tom c Droid 3 12-07-2009 10:36 PM
Background in a view Wuque Android Development, Answers, Tutorials, and Code Snippets 1 06-09-2009 01:49 PM


Unlocked G1 Phones | Buy T-Mobile G1 | Google Phone

All times are GMT -6. The time now is 08:13 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.
Copyright (c) 2012 TalkAndroid.com. All rights reserved.