• Have Any Question?
  • contact@eitworld.com

write an application to change the background of the activity on the button click





In this application, we have used two buttons for changing the background of the activity. The xml file is as follows:

 

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:id=”@+id/relative”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” ><Button
android:id=”@+id/btnBlack”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/btnRed”
android:layout_alignBottom=”@+id/btnRed”
android:layout_alignParentRight=”true”
android:layout_marginRight=”55dp”
android:text=”Black” />

<Button
android:id=”@+id/btnRed”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_alignParentTop=”true”
android:layout_marginLeft=”44dp”
android:layout_marginTop=”134dp”
android:text=”Red” />

</RelativeLayout>

In this layout file we have used the relative layout and we have provided the id for the relative layout so that we can use this into the java file. Here we have defined two buttons for taking the action.

The java file is as follows:

 

package com.example.changebakground;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
Button red,black;
RelativeLayout layout;

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

red = (Button) findViewById(R.id.btnRed);
black = (Button) findViewById(R.id.btnBlack);
layout = (RelativeLayout) findViewById(R.id.relative);

red.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
layout.setBackgroundColor(Color.RED);
}
});

black.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
// TODO Auto-generated method stub
layout.setBackgroundColor(Color.BLACK);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

 

In this java file, we have used the following packages:

change-background Here android.graphics.Color is used to provide the colors. After that we have declared the button type and relative layout variables as follows:

change-background Now we have to register controls inside the onCretate() method and assign the value for these variables as follows:

change-background Inside this method we have assigned the value for the red, black and layout. After that we have used the setOnClickListener() method to perform the action on buttons. For red button, inside this method we have used the setBackgroundColor() method and pass the color name using the Color class as the parameter of this method by using the object of the RelativeLayout, and when we click on the red button then the background will be changed in to red color.

After that we have provided the functionality to the black button using the setOnClickListener() method. For black button, inside this method we have used the setBackgroundColor() method and pass the color name using the Color class as the parameter of this method by using the object of the RelativeLayout, and when we click on the black button then the background will be changed in to black color.

The output is as follows:

change-background When launched

change-background When click on red button

change-background when click on black button



Real Time Web Analytics