Question

I'm using eclipse to code my android app and I'm wondering how I can change the background image of my MainActivity when I click a button. I have img1.png and img2.png. The background is currently set on img1.png with the following xml code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/img1"
    tools:context=".MainActivity" >
<Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="55dp"
        android:background="@android:color/transparent"
        android:text="" />

</RelativeLayout>

I'm just unsure what java code I would use to change the background image on btn1 click.

Was it helpful?

Solution

This code can be used to set background image programmattically

RelativeLayout layout =(RelativeLayout)findViewById(R.id.relativelayout);
layout.setBackgroundResource(R.drawable.img1);

OTHER TIPS

This could be a solution.

In your layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/lyt_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img1"
tools:context=".MainActivity" >

In your Activity

RelativeLayout layout;
public void onCreate(Bundle savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(your_xml.xml);
layout = (RelativeLayout) findById(R.id.lyt_main);
button = (Button) findById(R.id.lyt_main);
button.setOnClickListener(new OnClickListener{
      public void onClick(View v) {
               layout.setBackgroundDrawable(your_image));
      }
});
}

Add android ID to Your Layout:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/img1"
        android:id="@+id/rlayout"
        tools:context=".MainActivity" >
...
</RelativeLayout>

Now In Your MainActivity.java :

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn = (Button) findViewById(R.id.btn1);

        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RelativeLayout rlayot = (RelativeLayout) findViewById(R.id.rlayout);
                rlayot.setBackgroundResource(R.drawable.img2);
            }
        });
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top