Reputation: 23
How to automatically switch the background
in layout
android:background="@drawable/image">
I have two pictures
I want to allow automatic switching
Upvotes: 2
Views: 488
Reputation: 34775
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
if(loadFirst){
loadFirst = false;
layout_reference.setBackgroundResource(R.drawable.imagename1);
}else{
loadFirst = true;
layout_reference.setBackgroundResource(R.drawable.imagename2);
}
}
};
timer.schedule(timerTask,0,10000);//fires every 10secs
try this code in code and also declare a boolean variable "boolean loadFirst = false" in your oncreate method()
Upvotes: 1
Reputation: 132992
try this way :
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.images1); //new Image that was added to the res folder
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.etxtLayout);
linearLayout.setBackgroundDrawable(drawable);
}
}
Upvotes: 0
Reputation: 917
Create a thread and assign the time of sleep and put the the layout.setBackgroundDrawable(drawable) with sleep method and can use a flag variable to switch between both the background drawables, if 1 set 2 and if 2 set 1.
or you can put the layout.setBackgroundDrawable(drawable) code into runOnUIThread.
Upvotes: 0