Sid
Sid

Reputation: 1203

Programmatically change the layout color of layout

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");
        }
    }
}

Upvotes: 16

Views: 51896

Answers (3)

Om Prakash Sharma
Om Prakash Sharma

Reputation: 741

if you want to color code - Let's do

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

Example:

linearLayoutInquiryYear.setBackgroundColor(Color.parseColor("#e3e3e3"));

Upvotes: 1

Jeekiran
Jeekiran

Reputation: 473

RelativeLayout lLayout = (RelativeLayout) findViewById(R.layout.the_id);       
lLayout.setBackgroundColor(getResources().getColor(R.color.green_color));

Upvotes: 18

Cristian
Cristian

Reputation: 200090

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"));
    }
}

Upvotes: 25

Related Questions