16

I am trying to programatically change the layout color but of a relative layout (tried Linear layout but didn't change), but cannot change it.

Also trying to debug the app doesn't help, there was not message related to my TAG.

the application stood still after layout was colored initially.

package com.test.intentdemo;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
//import android.os.SystemClock;
import android.widget.RelativeLayout;
import android.util.*;
import java.lang.Thread;

public class intentDemo extends Activity {
    /** Called when the activity is first created. */
    RelativeLayout lLayout;
    public static final String TAG="MyActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        lLayout = (RelativeLayout) findViewById(R.layout.main);
        if (Log.isLoggable(TAG,0))
        {
            Log.e(TAG,"ERROR BEFORE");
            Log.i(TAG,"INFO BEFORE");
            Log.d(TAG,"DEBUG BEFORE");

            lLayout.setBackgroundColor(Color.parseColor("#000000"));
            //SystemClock.sleep(2000);
            try
            {
                Thread.currentThread();
                Thread.sleep(2000);
            }
            catch (Exception e)
            {
                //e.message();
            }

            Log.e(TAG,"ERROR AFTER");
            Log.i(TAG,"INFO AFTER");
            Log.d(TAG,"DEBUG AFTER");
        }
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Sid
  • 1,145
  • 5
  • 17
  • 24

3 Answers3

25
lLayout = (RelativeLayout) findViewById(R.layout.main);

This is wrong. findViewById expects an id of a View. So, give an ID to RelativeLayout, for instance:

<RelativeLayout
    android:id="@+id/the_id"

Then:

lLayout = (RelativeLayout) findViewById(R.id.the_id);

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.RelativeLayout;

public class intentDemo extends Activity {
    public static final String TAG="MyActivity";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RelativeLayout lLayout = (RelativeLayout) findViewById(R.layout.the_id);
        lLayout.setBackgroundColor(Color.parseColor("#000000"));
    }
}
Cristian
  • 198,401
  • 62
  • 356
  • 264
  • @Cristian: Thanks for pointing out the mistake, the problem still persists. Can you recommend anything else? Thanks, Sid – Sid Feb 23 '11 at 19:03
  • Just to be sure... I added a code above... could you test it and tell me whether it works or not? – Cristian Feb 23 '11 at 19:34
  • 1
    maybe the if is not letting you get there? If that's the case, try to see why happens. In that case that was not your question... you asked how to change the background color, and now you know how to do it, don't you? – Cristian Feb 23 '11 at 20:04
  • yeah, but I wanted to know how to change the color of layout under a condition, I got rid of 'if'and it works but my the real question is to change the layout in a condition like if or for. otherwise the whole purpose is defeated. I hope I have been able to explain my situation clearly. Thanks. – Sid Feb 23 '11 at 20:42
  • 1
    Yes, that's fine. The problem here is that you are asking: *how to change the color of a layout*, and certainly you do know how to do it now. If it does not work as you expect is because of the condition; but the condition itself has nothing to do with the question. Your condition has the `isLoggable` method... have you taken a look at the documentation? Your problem now is different: you want to know how to make that `if` statement execute. Again, it has nothing to do with the original question. – Cristian Feb 24 '11 at 12:38
18
RelativeLayout lLayout = (RelativeLayout) findViewById(R.layout.the_id);       
lLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Jeekiran
  • 473
  • 3
  • 10
1

if you want to color code - Let's do

your_layout_name.setBackgroundColor(Color.parseColor("Color Name"));

Example:

linearLayoutInquiryYear.setBackgroundColor(Color.parseColor("#e3e3e3"));
Zoe
  • 27,060
  • 21
  • 118
  • 148