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");
}
}
}
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.
By default each activity in Android has a white background. To change the background color, first add a new color definition to the colors.
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.
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"));
}
}
RelativeLayout lLayout = (RelativeLayout) findViewById(R.layout.the_id);
lLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With