Rasmnev
Rasmnev

Reputation: 109

Change layout background color on button click

I'm getting an error when trying to set the background color of a layout I get a nullpointerexception and I don't know why. I've looked at various posts (like this Change background color on button click?) and my code seems correct, but still the layout doesn't seem be called. This is my activity's code

public class Color extends Activity implements OnClickListener { 


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

    findViewById(R.id.blue).setOnClickListener(this);
    findViewById(R.id.red).setOnClickListener(this);
    findViewById(R.id.pink).setOnClickListener(this);
    findViewById(R.id.green).setOnClickListener(this);
    findViewById(R.id.yellow).setOnClickListener(this);
    findViewById(R.id.light_blue).setOnClickListener(this);
    findViewById(R.id.button_blue).setOnClickListener(this);
    findViewById(R.id.button_red).setOnClickListener(this);
    findViewById(R.id.button_pink).setOnClickListener(this);
    findViewById(R.id.button_greend).setOnClickListener(this);
    findViewById(R.id.button_yellow).setOnClickListener(this);
    findViewById(R.id.button_light_blue).setOnClickListener(this);


}
public void onClick(View v){
    switch (v.getId()){
    case R.id.blue:

        LinearLayout l1 = (LinearLayout) findViewById(R.id.layout_call);
        l1.setBackgroundResource(R.color.blue);

and this is the logcat error

01-30 01:25:06.828: E/AndroidRuntime(4837): FATAL EXCEPTION: main
01-30 01:25:06.828: E/AndroidRuntime(4837): Process: com.example.primeirocasopratico, PID: 4837
01-30 01:25:06.828: E/AndroidRuntime(4837): java.lang.NullPointerException
01-30 01:25:06.828: E/AndroidRuntime(4837):     at com.example.primeirocasopratico.Color.onClick(Color.java:42)

01-30 01:25:06.828: E/AndroidRuntime(4837):     at android.view.View.performClick(View.java:4463)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at android.view.View$PerformClick.run(View.java:18770)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at android.os.Handler.handleCallback(Handler.java:808)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at android.os.Handler.dispatchMessage(Handler.java:103)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at android.os.Looper.loop(Looper.java:193)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at android.app.ActivityThread.main(ActivityThread.java:5327)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at java.lang.reflect.Method.invokeNative(Native Method)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at java.lang.reflect.Method.invoke(Method.java:515)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
01-30 01:25:06.828: E/AndroidRuntime(4837):     at dalvik.system.NativeStart.main(Native Method)

I have also tried to set the background this way:

findViewById(R.layout.city).setBackgroundColor( android.graphics.Color.parseColor("#0000FF"));

and a few other variations and nothing seems to work...

(EDIT 2) I seem to have found the problem. It seems to be related with me trying to edit layouts associated with other activities. Any Idea how to solve that?

Upvotes: 2

Views: 7522

Answers (3)

Dhinakaran Thennarasu
Dhinakaran Thennarasu

Reputation: 3356

Shared preferences could be your solution. Oncreate of every activity check for the variable and set the color.

public void onClick(View v){
        switch (v.getId()){
        case R.id.blue:
            backgroundColor("blue");
            break;
 }
}


private void backgroundColor(String color) {
        // TODO Auto-generated method stub
        SharedPreferences prefs = getSharedPreferences("BackgroundColor", MODE_PRIVATE);
          SharedPreferences.Editor editor = prefs.edit();
          editor.clear();
          editor.putString("Color", color);
          editor.commit();
    }

In other Activities

SharedPreferences prefs = getSharedPreferences("BackgroundColor",
                  MODE_PRIVATE); 
String bgcolor = prefs.getString("Color","Anydefaultcolor");

now you can set your layout to bgcolor

Upvotes: 2

herbertD
herbertD

Reputation: 10945

If you want to change another Activity's UI, send a message to it by Broadcast or EventBus. Like this

EventBus.getDefault().post(new EventUpdateUI());

Upvotes: 0

Alex K
Alex K

Reputation: 8338

You didn't give us quite enough information to give you a complete answer, but if what you gave us is the complete code, then your error is in here:

public void onClick(View v){
    switch (v.getId()){
    case R.id.blue:

        LinearLayout l1 = (LinearLayout) findViewById(R.id.layout_call);
        l1.setBackgroundResource(R.color.blue);

Specifically, those last two lines. Make sure that your LinearLayout is, in fact, called layout_call. The NullPointerException means that you probably either made a type in the name, or simply didn't give it a name.

EDIT: Now that you made it clear which line is the error, I can confirm that this is the error. R.id.layout_call doesn't exist.

Try this:

public class Color extends Activity implements OnClickListener { 

private LinearLayout linLay;    

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

    linLay = (LinearLayout) findViewById(R.id.layout_call);

    findViewById(R.id.blue).setOnClickListener(this);
    findViewById(R.id.red).setOnClickListener(this);
    findViewById(R.id.pink).setOnClickListener(this);
    findViewById(R.id.green).setOnClickListener(this);
    findViewById(R.id.yellow).setOnClickListener(this);
    findViewById(R.id.light_blue).setOnClickListener(this);
    findViewById(R.id.button_blue).setOnClickListener(this);
    findViewById(R.id.button_red).setOnClickListener(this);
    findViewById(R.id.button_pink).setOnClickListener(this);
    findViewById(R.id.button_greend).setOnClickListener(this);
    findViewById(R.id.button_yellow).setOnClickListener(this);
    findViewById(R.id.button_light_blue).setOnClickListener(this);


}
public void onClick(View v){
    switch (v.getId()){
    case R.id.blue:

        linLay.setBackgroundResource(R.color.blue);

Upvotes: 1

Related Questions