Reputation: 75
I'm trying to change images in an android app when an audio file is running in background but I'm getting a problem. Below is my code:
public class SongActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_song);
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.activity_song);
MediaPlayer mp = MediaPlayer.create(SongActivity.this,R.raw.thesong);
mp.start();
rel.setBackgroundResource(R.drawable.bday);
Runnable r = new Runnable() {
@Override
public void run() {
synchronized (this){
try {
wait(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
Thread test = new Thread(r);
test.start();
rel.setBackgroundResource(R.drawable.bday2);
}
}
Please advise if I can modify it or it needs a total overhaul. Thanks in advance.
Upvotes: 3
Views: 87
Reputation: 1291
As I'm seeing your code, you write like:
setContentView(R.layout.activity_song);
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.activity_song);
here problem is that activity_song
this is just a xml layout
file name but not a RelativeLayout
so change (RelativeLayout)findViewById(R.id.activity_song);
it with your RelativeLayout
id.then you will get your desire result.
Upvotes: 1
Reputation: 2962
In your xml add ID.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/linear_layout">
</LinearLayout>
In code just replace
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.activity_song);
with
final RelativeLayout rel = (RelativeLayout)findViewById(R.id.linear_layout);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.image);
rel.setBackground(drawable);
Upvotes: 2