Permian Permian - 4 months ago 24
Android Question

Changing the Background Color using onClick Method

I am trying to create a simple app for training purposes to swap between a white and black background upon clicking the app. I am trying to keep everything general, so as to allow for scalability to slightly other purposes. There are a number of similar ideas of this site, but none quite answers my question. I have borrowed some ideas from these posts however. So that it keeps general, I am using a function changeScreen() which could be easily changed for other purposes.

My problems are:

1) I am not sure what the argument(s) to changeScreen() should be or whether the function needs arguments at all.

2) How to define changeScreen(). The functions getColor and setBackgroundColor are guessed and dont compile.

3) Where should the function changeScreen() be defined. I would like to keep it as separate as possible to where it is used, for abstraction purposes.

4) I am not sure about the View passed to the onclick function with respect to the function changeScreen. Should it be passed or not?

Here is my java file, MainActivity.java:

package com.mycompany.myapplication;

import android.app.*;
import android.graphics.Color;
import android.support.v7.app.*;
import android.os.*;
import android.text.Layout;
import android.view.*;
import android.widget.*;
import java.util.*;

public class MainActivity extends Activity {

RelativeLayout MainActivity;

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

MainActivity = (RelativeLayout) findViewById(R.id.MainActivity);
MainActivity.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
MainActivity.changeScreen();
}
});
}

public void changeScreen(){
if (getColor()== "White"){
MainActivity.setBackgroundColor("Black");
} else {
MainActivity.setBackgroundColor("White");
}

}


This is the corresponding xml file, activity_main.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/MainActivity"
android:clickable="true"
tools:context=".MainActivity">
</RelativeLayout>


This is the colors xml file, (I know I need to define the color white in colors.xml), colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="black">#000000</color>
</resources>

Answer
    final RelativeLayout MainActivity = (RelativeLayout) findViewById(R.id.MainActivity);
    MainActivity.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if(((ColorDrawable) MainActivity.getBackground()).getColor() == getResources().getColor(R.color.WHITE))
            MainActivity.setBackgroundColor(getResources().getColor(R.color.BLACK));
        else
            MainActivity.setBackgroundColor(getResources().getColor(R.color.WHITE));
    }
});
Comments