Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
        }
    }
}
like image 401
Sid Avatar asked Feb 23 '11 17:02

Sid


People also ask

How can change relative layout background color in android programmatically?

Programmatically using Java code. java file, then you can do it by using setBackgroundColor() method on your layout. To your Parent View Layout add an attribute @id/id_name and map it to a variable in your java file.

What is the default color of layout in Android?

By default each activity in Android has a white background. To change the background color, first add a new color definition to the colors.

How do you change the background color on Kotlin?

To set Android Button background color, we can assign android:backgroundTint XML attribute for Button in layout file with the required Color Value. To programmatically set or change Android Button background color, we may call pass the method Button.


2 Answers

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"));
    }
}
like image 184
Cristian Avatar answered Oct 20 '22 16:10

Cristian


RelativeLayout lLayout = (RelativeLayout) findViewById(R.layout.the_id);       
lLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
like image 28
Jeekiran Avatar answered Oct 20 '22 14:10

Jeekiran