Reputation: 620
I need to change the backgound image of the same activity multiple times, I'd like to know how can I do that using codes, since XML won't help in this case (I guess)
here is the code of my activity, it's simple and there are few lines, if someone could change this code to explain better how i can do what i need, i'd be very thankfull. thank you guys.
public class forca_inicia extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forca_inicia);
Editable palavra_jogo;
EditText palavra = (EditText)findViewById(R.id.campo_palavra);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_forca_inicia, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 203
Reputation: 2668
In onCreate
get the root element from your activity layout and set color, drawable, resource. For example:
FrameLayout frame = (FrameLayout) findViewById(R.id.content_frame);
frame.setBackground(Drawable background);
frame.setBackgroundResource(int resId);
frame.setBackgroundColor(int color);
I would also recommend you to follow Java naming conventions. Class names should start with capital letter and should use CamelCase e.g.: ForcaInicia. And variables should start with small letter and use camelCase e.g. palavraJogo.
Upvotes: 1
Reputation: 3389
All you need to do is get access to your Activity's ViewGroup and set the background for that ViewGroup.
getWindow().getDecorView().setBackgroundResource(R.drawable.your_drawable);
Upvotes: 0