Reputation: 4396
I want to change the background color of my Main-View (not a Button or a Text-View) just the real background which is usually black... I got this code:
view.setBackgroundColor(0xfff00000);
This is inside an OnClickListener
, but it just changes the background of the Button.
Upvotes: 30
Views: 144980
Reputation: 1
Just go to the activity_main.xml file. You will see an attributes panel on the right hand side, there you will find an attribute called "background". You can set the color over there.
Upvotes: 0
Reputation: 1
You should write in xml where background i
android:background="@drawable/imgname"
Upvotes: -1
Reputation: 13
Try this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
</androidx.constraintlayout.widget.ConstraintLayout>
/>
Upvotes: -1
Reputation: 452
I just want to add my 2 cents. I had the same goal (to change the background color from the .java class). But none of the above methods worked for me.
Issue was, that I set the background color inside the layout .xml file with android:background="@color/colorGray"
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/colorGray">
So I just deleted particular line:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Now I (you) can set the color from the code with:
getWindow().getDecorView().setBackgroundColor(Color.GRAY);
Upvotes: 0
Reputation: 34370
First Method
View someView = findViewById(R.id.randomViewInMainLayout);// get Any child View
// Find the root view
View root = someView.getRootView()
// Set the color
root.setBackgroundColor(getResources().getColor(android.R.color.red));
Second Method
Add this single line after setContentView(...);
getWindow().getDecorView().setBackgroundColor(Color.WHITE);
Third Method
set background color to the rootView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF"
android:id="@+id/rootView"
</LinearLayout>
Important Thing
rootView.setBackgroundColor(0xFF00FF00); //after 0x the other four pairs are alpha,red,green,blue color.
Upvotes: 4
Reputation: 919
Just add this below one line code in the XML file of that corresponding activity:
android:background="@android:color/black"
it will help you for sure.
Upvotes: 6
Reputation: 802
i don't know if it's the answer to your question but you can try setting the background color in the xml layout like this. It is easy, it always works
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="0xfff00000"
>
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
You can also do more fancy things with backgrounds by creating an xml background file with gradients which are cool and semi transparent, and refer to it for other use see example below:
the background.xml layout
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:angle="90"
android:startColor="#f0000000"
android:endColor="#ff444444"
android:type="linear" />
</shape>
</item>
</selector>
your layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@layout/background"
>
<TextView
android:id="@+id/text_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
Upvotes: 10
Reputation: 56
You can also try and provide an Id for the main layout and change the background of that through basic manipulation and retrieval. E.g:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hello"
Which can then be followed by accessing through R.id.hello.... Pretty basic and I hope this does help :)
Upvotes: 1
Reputation: 1039
if you put your full code here so i can help you. if your setting the listener in XML and calling the set background color on View so it will change the background color of the view means it ur Botton so put ur listener in ur activity and then change the color of your view
Upvotes: -1
Reputation: 48871
Try creating a method in your Activity
something like...
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
Then call it from your OnClickListener passing in whatever colour you want.
Upvotes: 68